2014-10-28 101 views
1

下面的代码将图像成功发送到服务器。如何在httpurlconnection中添加参数android

但我需要的是必须添加像数据库的图像描述和图像日期参数。

我不知道如何在HttURLconnection中添加参数。

String fileName = sourceFileUri; 

    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 
    File sourceFile = new File(sourceFileUri); 


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

      dos = new DataOutputStream(conn.getOutputStream()); 

      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" 
        + fileName + "\"" + lineEnd); 

      dos.writeBytes(lineEnd); 

      // create a buffer of maximum size 
      bytesAvailable = fileInputStream.available(); 

      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      buffer = new byte[bufferSize]; 

      // read file and write it into form... 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      while (bytesRead > 0) { 

       dos.write(buffer, 0, bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      } 

      // send multipart form data necesssary after file data... 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // Responses from the server (code and message) 
      serverResponseCode = conn.getResponseCode(); 
      String serverResponseMessage = conn.getResponseMessage(); 

      Log.i("uploadFile", "HTTP Response is : " 
        + serverResponseMessage + ": " + serverResponseCode); 

      if (serverResponseCode == 200) { 

       runOnUiThread(new Runnable() { 
        public void run() { 
         String msg = "File Upload Completed.\n\n See uploaded file here : \n\n" 
           + " c:/wamp/www/echo/uploads"; 
         messageText.setText(msg); 
         Toast.makeText(MainActivity.this, 
           "File Upload Complete.", Toast.LENGTH_SHORT) 
           .show(); 
        } 
       }); 
      } 

      // close the streams // 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 

而且PHPCODE:

<?php 
    error_reporting(0); 
    $file_path = "uploads/"; 

    $file_path = $file_path . basename($_FILES['uploaded_file']['name']); 
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) { 
     echo "success"; 
    } else{ 
     echo "fail"; 
    } 
?> 

上面的代码上传代码服务器运作良好。

必须在db中存储图像描述。

如何为上述代码添加参数。

以及如何在PHP服务器端接收它。

任何相关的教程或文档。

在此先感谢。

+0

添加这样在下面conn.setRequestProperty( “uploaded_file”,文件名)机器人的代码; conn.setRequestProperty(“image_description”,imageName); conn.setRequestProperty(“image_date”,imageDate); 并在你的php文件中添加一个标签类似 – Naufal 2014-10-28 12:36:40

+0

试试这个教程http://www.tutorialspoint.com/android/android_network_connection.htm – Naufal 2014-10-28 12:43:51

+0

在android端添加了这段代码。 conn.setRequestProperty(“image_description”,imageName); conn.setRequestProperty(“image_date”,imageDate);并在PHP端如何获取这个参数值。 $ _FILES [ 'uploaded_file'] [ '名称']);这段代码是获取图像细节。如何获取参数值_Post ['image_description']和_Post ['image_date']是否正确?我混淆了setRequestProperty(“id”,“value”);我在单个请求中将其设置为多部分和文本。 – John 2014-10-29 09:49:47

回答

0

检查此link

的Android代码:

字符串参数= “值”;

 dos.writeBytes("Content-Disposition: form-data; name=\"parameter\"" + lineEnd); 
     //dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd); 
     //dos.writeBytes("Content-Length: " + parameter.length() + lineEnd); 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(parameter); // mobile_no is String variable 
     dos.writeBytes(lineEnd); 

PHP代码:

$parameter =$_POST['parameter']; 
+0

是否需要添加“dos.writeBytes(lineEnd);” “内容处理”和“参数”行之间? – 2016-03-01 05:32:42

相关问题