2014-10-28 59 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

相關問題