2016-01-18 80 views
0

使用httpurlconnection將圖像發送到我的服務器時出現問題。我已閱讀Android documentation和另一個HttpUrlconnection implementation但我不知道自從我得到HTTP_BAD_REQUEST錯誤代碼(400)以來,它在做錯了什麼。我在下面的代碼中缺少什麼重要的東西?使用HttpUrlConnection發送圖像文件時發生HTTP_BAD_REQUEST錯誤

我的回答的代碼始終返回400,但我的鏈接是好的,因爲我能夠使用的HttpClient

link = "my link.com"; 

try { 
    URL url = new URL(link); 

    connection = (HttpURLConnection)url.openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 
    connection.setChunkedStreamingMode(0); 
    connection.setRequestProperty("Content-Type", "image/jpeg"); 

    BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); 
    FileInputStream stream = new FileInputStream(file); 
    byte[] buffer = new byte[1024]; 
    int bytesRead; 
    while ((bytesRead =stream.read(buffer ,0 ,buffer.length)) != -1){ 
      outputStream.write(buffer); 
      outputStream.flush(); 
    } 
    outputStream.flush(); 

    responseCode = connection.getResponseCode(); 
+0

可以在服務器處理的Content-Type =圖像/ JPEG ?嘗試使用某個控制檯或REST客戶端來測試您的請求,以確定您的請求是否正確。我認爲它應該是Content-Type multipart/form-data – CyberAleks

+0

看到這篇文章http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work – CyberAleks

+0

@Cyber​​Aleks我的服務器可以處理,因爲上午能夠使用相同的內容類型使用httpclient –

回答

1

我認爲這個問題是圖像是如何被添加到輸出流實現這一目標。所有的連接配置步驟都很好。

我最近嘗試這種方法,效果不錯:

https://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

它也是一個的AsyncTask包裝好的做法。我注意到MultipartEntity現在已被棄用,但您可以用MultipartEntityBuilder替換。

更新

要收聽文件上傳事件和更新您的進度,您可以覆蓋任何HttpEntity實施writeTo方法,因爲他們被寫入到輸出流數字節。

DefaultHttpClient httpclient = new DefaultHttpClient(); 
try { 
    HttpPost httppost = new HttpPost("http://www.google.com/sorry"); 

    MultipartEntity outentity = new MultipartEntity() { 

    @Override 
    public void writeTo(final OutputStream outstream) throws IOException { 
     super.writeTo(new CoutingOutputStream(outstream)); 
    } 

    }; 
    outentity.addPart("stuff", new StringBody("Stuff")); 
    httppost.setEntity(outentity); 

    HttpResponse rsp = httpclient.execute(httppost); 
    HttpEntity inentity = rsp.getEntity(); 
    EntityUtils.consume(inentity); 
} finally { 
    httpclient.getConnectionManager().shutdown(); 
} 

static class CoutingOutputStream extends FilterOutputStream { 

    CoutingOutputStream(final OutputStream out) { 
     super(out); 
    } 

    @Override 
    public void write(int b) throws IOException { 
     out.write(b); 
     System.out.println("Written 1 byte"); 
    } 

    @Override 
    public void write(byte[] b) throws IOException { 
     out.write(b); 
     System.out.println("Written " + b.length + " bytes"); 
    } 

    @Override 
    public void write(byte[] b, int off, int len) throws IOException { 
     out.write(b, off, len); 
     System.out.println("Written " + len + " bytes"); 
    } 

} 

更新 如果您想更新基於HTTP進度的進度條該鏈接提供了一個很好的例子

Link

+0

我已經結束了實施httpclient,雖然我從來沒有想過。我關心的是如何聽取上傳進度?我如何使用這個http客戶端更新我的進度條? –

+0

你想要一個確定的或不確定的進度條嗎?如果你在AsyncTask中使用onPreExecute和onPostExecute方法,你可以很容易地啓動和停止一個不確定的進度條 – BenJ

+0

我想要一個定義一個,並在我的'HttpPut put = new HttpPut(link)中使用'ByteArrayEntity'實例作爲我的實體。 ''put.setEntity(bytearrayEntity)' –

相關問題