2011-12-14 218 views
0

我正在使用MIME多部分上傳圖像到服務器。有時圖像會變形。如何解決這個問題? 注:扭曲的手段,一些像素丟失。 我使用下面的代碼上傳:圖像在上傳時被扭曲

File file = new File(filePath[0]); 
HttpClient httpClient = new DefaultHttpClient(); 
HttpContext localContext = new BasicHttpContext(); 
HttpPost httpPost = new HttpPost("serverurl"); 

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
entity.addPart("Content-Type",new StringBody("multipart/form-data;charset=utf-8")); 
entity.addPart("Content-Length", new StringBody(String.valueOf(file.length()))); 
entity.addPart("UploadContentPostD", new FileBody(file));     
entity.addPart("DocumentName", new StringBody(file.getName())); 

httpPost.setEntity(entity); 
HttpResponse response = httpClient.execute(httpPost,localContext); 
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 

圖像失真是: enter image description here

+0

請準確解釋你的意思是扭曲?不良的像素/畫面比例改變/像素損壞? – Elemental 2011-12-14 14:25:11

+0

像素丟失,所以圖像看起來像重疊其他部分 – Sandy 2011-12-14 14:27:24

回答

1

我用Apache Commons上傳這裏是我上傳的代碼,每一次完美的作品...

public Integer uploadByteArray(String remoteFilename, byte[] bitmapdata){  
     HttpClient client = new HttpClient(); 
     PostMethod filePost = new PostMethod(URL_PATH); 

     Integer ret = null; 

     try {   
      Part[] parts = new Part[2]; 
      parts[0] = new StringPart("file_name" ,remoteFilename); 

      ByteArrayPartSource ps = new ByteArrayPartSource("file", bitmapdata); 
      parts[1] = new FilePart("file", ps); 

      filePost.setRequestEntity(new MultipartRequestEntity(parts, 
        filePost.getParams())); 

     } catch (Exception e) { 
      Log.d(TAG, e.toString()); 
     } 

     try { 
      ret = client.executeMethod(filePost); 
      Log.d(TAG, "statusCode>>>" + ret); 
      if(ret != 200){ 
       Log.d(TAG, "Error:" + ret + " from server. Please try again later."); 
      }else{ 
       responseBody = filePost.getResponseBodyAsString(); 
       Log.d(TAG, filePost.getResponseBodyAsString()); 
      } 
     } catch (Exception e) { 
      Log.d(TAG, e.toString()); 
     } 

     filePost.releaseConnection(); 

     return ret; 
    } 

如果你繼續看到你的問題不斷出現,我會在服務器上獲得的文件上使用MD5,並將該值發送回resp onse並將其與您發送的文件的本地MD5進行比較。如果他們不一樣,你就知道出了問題。