的Android通过HttpURLConnection的发送HTTP文件参数和非文件

问题描述:

我有上传文件的代码通过http:的Android通过HttpURLConnection的发送HTTP文件参数和非文件

FileInputStream fileInputStream = new FileInputStream(selectedFile); 
      URL url = new URL(SERVER_URL); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true);//Allow Inputs 
      connection.setDoOutput(true);//Allow Outputs 
      connection.setUseCaches(false);//Don't use a cached Copy 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Connection", "Keep-Alive"); 
      connection.setRequestProperty("ENCTYPE", "multipart/form-data"); 
      connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
      connection.setRequestProperty("uploaded_file",selectedFilePath); 



      //creating new dataoutputstream 
      dataOutputStream = new DataOutputStream(connection.getOutputStream()); 

      //writing bytes to data outputstream 
      dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd); 
      dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" 
        + selectedFilePath + "\"" + lineEnd); 

      dataOutputStream.writeBytes(lineEnd); 

,但我需要发送用户信息太像邮件/密码

我想这样但只有文件上传和用户信息转为空:

FileInputStream fileInputStream = new FileInputStream(selectedFile); 
      URL url = new URL(SERVER_URL); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true);//Allow Inputs 
      connection.setDoOutput(true);//Allow Outputs 
      connection.setUseCaches(false);//Don't use a cached Copy 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Connection", "Keep-Alive"); 
      connection.setRequestProperty("ENCTYPE", "multipart/form-data"); 
      connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
      connection.setRequestProperty("uploaded_file",selectedFilePath); 
      connection.setRequestProperty("user",user); 

我加入connection.setRequestProperty(“用户”,用户);但在服务器端它来空

$user = $_POST['user']; 

我怎么可以在同一httpurlconnection发送文件和用户信息?

我解决把这个代码:

dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd); 
      dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"myparameter\"" + lineEnd); 
      dataOutputStream.writeBytes(lineEnd); 
      dataOutputStream.writeBytes(myvalue); 

其中myparameter是参数和myvalue的是参数的值