2014-10-16 41 views
0

我嘗試將圖像(base64編碼)與其他值一起上傳到PHP網絡服務器,PHP網絡服務器接收此信息並執行一些進一步處理。使用HttPost和HttpClient上傳base64編碼圖像

以下功能是執行請求的AsyncTask的一部分。

我手動構建postData連接不同的值。

JsonUrlLook類幫助我找到發送數據的正確URL。我證實了它。它是正確的。

當我嘗試執行創建的HttpPost時,我的響應爲空。任何人有一個想法,爲什麼這是?

private String getPostData(String arrayString, String base64Image, String boundary) { 
    String remoteMethod = "uploadPic"; 
    String postData = ""; 

    // Add remote method name 
    postData += "--" + boundary + "\r\n"; 
    postData += "Content-Disposition: form-data; name=\"" + boundary + "[method]\"\r\n\r\n"; 
    postData += remoteMethod; 

    postData += "--" + boundary + "\r\n"; 
    postData += "Content-Disposition: form-data; name=\"" + boundary + "[params]\"\r\n\r\n"; 
    postData += arrayString; 

    postData += "--" + boundary + "\r\n"; 
    postData += "Content-Disposition: form-data; name=\"" + boundary + "\"\r\n\r\n"; 
    postData += "filename=\"image.jpg\"\r\n\r\n"; 
    postData += base64Image; 

    postData += "--" + boundary + "--"; 

    return postData; 
} 

private String uploadPicture(String arrayString, String base64Image) { 

    String boundary = "tx_fpoemobile_pi2"; 

    // Get post data 
    String postData = getPostData(arrayString, base64Image, boundary); 

    // Get webservice uri 
    JsonUrlLookup lookup = new JsonUrlLookup(context); 
    URL webserviceUrl = lookup.buildUrl(context.getResources().getString(R.string.page_uid_webservice)); 
    URI webserviceUri = null; 
    try { 
     webserviceUri = new URI(webserviceUrl.toString()); 
    } catch(Exception e) { 
     // TODO 
    } 

    // Build client and post 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(webserviceUri); 

    // Set header of post 
    httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary); 
    httpPost.setHeader("Content-Length", String.valueOf(postData.length())); 

    // Set body 
    try { 
     httpPost.setEntity(new StringEntity(postData)); 
    } catch (Exception e) { 
     // TODO 
    } 

    HttpResponse response = null; 
    try { 
     response = httpClient.execute(httpPost); 
    } catch (ClientProtocolException e) { 
     // TODO 
    } catch (IOException e) { 
     // TODO 
    } 

    return "uploadPictureSuccess"; 
} 

回答

0

你有這樣

public void connectForMultipart() throws Exception { 
    con = (HttpURLConnection) (new URL(url)).openConnection(); 
    con.setRequestMethod("POST"); 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setRequestProperty("Connection", "Keep-Alive"); 
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
    con.connect(); 
    os = con.getOutputStream(); 
} 

public void addFormPart(String paramName, String value) throws Exception { 
    writeParamData(paramName, value); 
} 

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception { 
    os.write((delimiter + boundary + "\r\n").getBytes()); 
    os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes()); 
    os.write(("Content-Type: application/octet-stream\r\n" ).getBytes()); 
    os.write(("Content-Transfer-Encoding: binary\r\n" ).getBytes()); 
    os.write("\r\n".getBytes()); 

    os.write(data); 

    os.write("\r\n".getBytes()); 
} 
public void finishMultipart() throws Exception { 
    os.write((delimiter + boundary + delimiter + "\r\n").getBytes()); 
} 

工作爲獲得響應

httpPost.setEntity(entity); 
HttpResponse response = httpClient.execute(httpPost); 

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 



String sResponse; 
while ((sResponse = reader.readLine()) != null) 
{ 
    s = s.append(sResponse); 
} 

if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
{ 
    return s.toString(); 
}else 
{ 
    return "{\"status\":\"false\",\"message\":\"Some error occurred\"}"; 
} 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

代替圖像可以發送任何東西的。

在這種情況下,你需要httpmime-4.2.1-1.jar

+0

我說,我需要將圖像傳送作爲base64編碼字符串,而不是二進制。但無論如何,我會嘗試你的解決方案。 – Klaus 2014-10-16 13:06:15

+0

你介意解釋一下函數「writeParamData」嗎? – Klaus 2014-10-16 13:20:38

+0

在你的文章「獲得回覆」中,你使用了一個httpClient和一個具有設置實體的httpPost。這與第一部分有很大不同。他們連接,還是完全不同的認同?我沒有明白。 – Klaus 2014-10-16 13:23:32