2010-06-30 134 views
1

我正在開發一個Android應用程序,我試圖從客戶端將圖像上傳到我們的服務器。當我進入服務器查看圖像時,它有時會損壞(底部灰色條)。我不知道爲什麼會發生這種情況。任何人都可以幫我解決這個問題,或者指導我如何做到這一點?以下是上傳圖片的代碼:Android開發圖像上傳損壞

try { 
    url = new URL(SERVER_URL); 
    connection = (HttpURLConnection)url.openConnection(); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.setRequestProperty("Content-Type", "image/jpeg"); 
    connection.setChunkedStreamingMode(STREAM_CHUNK_SIZE_KB * 1024); 
    connection.connect(); 
    outputStream = new DataOutputStream(connection.getOutputStream()); 

    // Write file header (userId; Id; contentId; MediaType; size) 
    outputStream.writeLong(mUserId); 
    outputStream.writeLong(mId); 
    outputStream.writeLong(file.getId()); 
    outputStream.writeUTF(MediaType.getMediaType(file.getMediaType())); 
    outputStream.writeInt(file.getSize()); 

    // Write file data to stream 
    int maxBufferSize = (8 * 1024); 
    FileInputStream fileInputStream = new FileInputStream(file.getImageFile()); 
    int bytesAvailable = fileInputStream.available(); 
    int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    byte[] buffer = new byte[bufferSize]; 
    int 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.flush(); 

    // Check response 
    int responseCode = connection.getResponseCode(); 
    if (responseCode == HttpURLConnection.HTTP_OK) { 
     uploadStatus = true; 
     InputStream responseStream = connection.getInputStream(); 
     BufferedReader responseReader = new BufferedReader(new 
      InputStreamReader(responseStream)); 
     char[] response = new char[4]; 
     responseReader.read(response, 0, 4); 
     responseReader.close(); 
     int responseValue = 0; 
     for (int b = 0; b < 4; b++) { 
      responseValue |= response[b] & 0xFF; 
      if (b < 3) { 
       responseValue <<= 8; 
      } 
     } 

     switch (responseValue) { 
      case SAVED_SUCCESSFULLY: 
       Log.d("FileUploader::upload -> Server response: Upload successful"); 
      break; 
      case ERROR_SAVING_FILE: 
       Log.d("FileUploader::upload -> Server response: Upload failed"); 
      break; 
      case FILE_MORE_THAN_ALLOWED_SIZE: 
       Log.d("FileUploader::upload -> Server response: Upload failed, exceeded 
        allowed file size"); 
      break; 
     } 
    } 

    else { 
     Log.d("FileUploader::upload -> responseCode = " + responseCode); 
     checkErrorStream(connection.getErrorStream()); 
    } 
} 

catch(Exception e) { 
    Log.e(Log.getStackTraceString(e)); 
} 

finally { 
    try { 
     if (outputStream != null) { 
      outputStream.close(); 
     } 
    } 

    catch(Exception e) { 
     Log.e(Log.getStackTraceString(e)); 
    } 

    if (connection != null) { 
     connection.disconnect(); 
    } 
} 

回答

0

請嘗試通過USB傳輸圖像,而不是通過網絡傳輸圖像。該圖像是否可以或者會被打破?

0

我不能輕易告訴你爲什麼圖像無需建立一個測試應用程序並運行你的代碼的破壞,但我可以給你一個提示:

保存原始圖像文件。抓取通過網絡傳輸的圖像。用十六進制編輯器打開它們,然後看看它們有什麼不同。 (這大概接近文件末尾)

這應該告訴你實際的腐敗包含了什麼,反過來又會告訴你在代碼中尋找什麼。

(例如,你可能會被截斷你的文件,或追加一個換行符是不應該存在,或任意數量的細微錯誤的。)

0

您可能有一個錯誤在你的代碼:

outputStream.write(buffer, 0, bufferSize); 

應該

outputStream.write(buffer, 0, bytesRead);