2017-10-16 98 views
0

我試圖上傳圖片使用AsyncHttpClient,我的圖像sucsessfully上傳,但它無法從我的服務器打開,他們說:「這不是一個有效的位圖文件或其格式不支持「。以前我用AsyncHttpClient庫,我可以上傳並看到在我的服務器我的形象,這是我上傳我的圖片前:使用AsyncHttpClient庫上傳multipart/form-data

public static String POSTUpload(String url, String file) { 
    int status = 0; 
    String result = ""; 

    PostMethod method = new PostMethod(url); 
    try { 
     HttpClient httpClient = new HttpClient(); 
     httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(300000); 
     httpClient.getHttpConnectionManager().getParams().setSoTimeout(300000); 

     method.setRequestEntity(new FileRequestEntity(new File(file), "multipart/form-data")); 

     status = httpClient.executeMethod(method); 
     result = String.valueOf(status); 
     System.out.println("HTTP status " + method.getStatusCode() + " creating con\n\n"); 
    } catch (ConnectTimeoutException e) { 
     Log.e("Timeout Exception: ", e.toString()); 
    } catch (SocketTimeoutException ste) { 
     Log.e("Timeout Exception: ", ste.toString()); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     method.releaseConnection(); 
    } 
    return result; 
} 

,這是我如何使用AsyncHttpClient上傳我的圖片:

private void POSTUpload(String url_upload, String file) { 
    RequestParams params = new RequestParams(); 
    try { 
     params.put("uploaded_file", new File(file)); 
     params.setForceMultipartEntityContentType(true); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    AsyncHttpClient client = new AsyncHttpClient(); 
    client.setMaxConnections(100); 
    client.post(url_upload, params, new AsyncHttpResponseHandler() { 

     @Override 
     public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 
      System.out.println("statusCode " + statusCode); 
     } 

     @Override 
     public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 
     } 
    }); 
} 

我不明白爲什麼當我使用這個庫時我的圖片無法顯示。我希望有人能幫助我解決我的問題。非常感謝你。

回答

相關問題