2016-09-22 151 views
1

我使用PHP上傳從Android的一些圖像服務器。 圖片文件正在上傳,但是當我試圖查看該文件時,它顯示的是破碎的圖片,不知道究竟是什麼問題。 上傳在服務器上顯示的的AsyncTask 的Android代碼圖片上傳問題,從機器人到PHP服務器

FileInputStream fileInputStream; 
    HttpURLConnection connection; 
    URL url; 
    DataOutputStream outputStream; 
    int bytesRead,bytesAvailable,bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 
    File selectedFile = new File(selectedImagePath); 
try 
     { 
      fileInputStream = new FileInputStream(selectedFile); 
      url = new URL("The url of my server"); 
      connection= (HttpURLConnection)url.openConnection(); 
      Log.v(TAG,"connection established"); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Connection","Keep-Alive"); 
      connection.setRequestProperty("ENCTYPE","multipart/form-data"); 
      connection.setRequestProperty("Content-Type","multipart/form-data;boundary=*****"); 
      connection.setRequestProperty("uploaded_file",selectedImagePath); 

      outputStream = new DataOutputStream(connection.getOutputStream()); 

      outputStream.writeBytes("--*****\r\n"); 

      outputStream.writeBytes("Content-Disposition:form-data;name=\"uploaded_file\";filename=\""+selectedImagePath+"\""+"\r\n"); 
      outputStream.writeBytes("\r\n"); 
      outputStream.writeBytes("--*****\r\n"); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable,maxBufferSize); 
      buffer = new byte[bufferSize]; 

      bytesRead = fileInputStream.read(buffer,0,bufferSize); 
      while (bytesRead>0) 
      { 
       outputStream.write(buffer,0,bufferSize); 
       bytesAvailable = fileInputStream.available(); 
       bufferSize = Math.min(bytesAvailable,maxBufferSize); 
       bytesRead = fileInputStream.read(buffer,0,bufferSize); 
      } 
      outputStream.writeBytes("\r\n"); 
      outputStream.writeBytes("--*****\r\n"); 
      int serverResponseCode = connection.getResponseCode(); 
      String serverResponseMessage = connection.getResponseMessage(); 

      if(serverResponseCode == HttpURLConnection.HTTP_OK) { 
       String line; 
       StringBuilder sb = new StringBuilder(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
       while ((line = reader.readLine()) != null) { 
        sb.append(line); 
       } 


      } 
      fileInputStream.close(); 
      outputStream.flush(); 
      outputStream.close(); 
PHP Code: 
$file_path = basename($_FILES['uploaded_file']['name']); 
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) 
{ 
    echo "success"; 
} else 
{ 
    echo "fail"; 
} 

圖片完成: enter image description here

+0

'outputStream.write(緩衝液,0,緩衝區大小);'。這應該是'outputStream.write(buffer,0,bytesRead);'。 – greenapps

+0

請比較原始文件和上傳文件的大小。 – greenapps

+0

感謝您的回覆,但現在已完成!問題在於關閉文件塊。 – Akki

回答

0

要寫入的字節數組或Base64格式爲圖像,而不是多部分將是很容易的解決方案。

檢查:Uploading image from android to php server

+0

感謝您將頭後您的快速回復,但其顯示相同的破碎的形象@Jai – Akki

+0

是啊,因爲你逝去的內容類型「多」,從客戶端。使用base64格式而不是multipart。這將很容易圖像上傳過程 – Jai

+1

感謝您的答覆。但我現在使用multipart得到了解決方案和它的工作。實際上,問題是我使用這個'outputStream.writeBytes(「 - ***** \ r \ n」)結束了圖像塊;'在發送字節數組之前 – Akki