Java - 发布将文件上传到URL的请求

时间:2014-01-15 22:24:32

标签: java post file-upload upload

我正在寻找使用POST请求将文件上传到服务器的可能性。

我一直在尝试使用restTemplates,但不起作用,然后我尝试使用此代码:

HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    String urlParam = "http://myURL";

    HttpPost httppost = new HttpPost(urlParam);
    File file = new File(filename);

    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(file, "image/jpeg");
    mpEntity.addPart("file", cbFile);

    ArrayList<NameValuePair> postParameters;
    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("id_category", "0"));
    postParameters.add(new BasicNameValuePair("token", "c4c533d2825f8791a07265d812d62d90"));
    postParameters.add(new BasicNameValuePair("tab", "AP"));
    postParameters.add(new BasicNameValuePair("action", "addImage"));
    postParameters.add(new BasicNameValuePair("action", "addImage"));
    postParameters.add(new BasicNameValuePair("qqfile", "IMG_0599.jpg"));

    httppost.setEntity(new UrlEncodedFormEntity(postParameters));

    Header headers2 = new BasicHeader("Cookie", cookieNew);

    httppost.setEntity(mpEntity);
    httppost.setHeader(headers2);
    System.out.println("executing request " + httppost.getRequestLine());
    HttpResponse responseX = httpclient.execute(httppost);
    HttpEntity resEntity = responseX.getEntity();

System.out.println(responseX.getStatusLine());
    if (resEntity != null) {
        System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
        resEntity.consumeContent();
    }

    httpclient.getConnectionManager().shutdown();

但我知道,该文件未上传到服务器,临时文件为空。

我的回答是:

 DEBUG: org.apache.http.wire -  >> "[0x1]@([0x5][0x0][0xa0][0x14][0x2][0x80]P[\n]"
DEBUG: org.apache.http.wire -  >> "[0x1]@([0x5][0x0][0xa0][0x14][0x7][0xe5][0xc0][0xea]E[0x0][0xb8][0xfa][0x8f][0xe6]([0x5][0xc7][0xd4]1@.>[0xa3][0xf9][0x8a][0x1]q[0xf5][0x1f][0xcc]P[0xb][0x8f][0xa8][0xfe]b[0x80]\}G[0xf3][0x14][0x2][0xe3][0xea]?[0x98][0xa0][0x17][0x1f]Q[0xfc][0xc5][0x0][0xb8][0xfa][0x8f][0xe6]([0x5][0xc7][0xd4]1@.>[0xa3][0xf9][0x8a][0x1]q[0xf5][0x1f][0xcc]P[0xb][0x8f][0xa8][0xfe]b[0x80]\}G[0xf3][0x14][0x2][0xe3][0xea]?[0x98][0xa0][0x17][0x1f]Q[0xfc][0xc5][0x0][0xb8][0xfa][0x8f][0xe6]([0x5][0xc7][0xd4]1@.>[0xa3][0xf9][0x8a][0x1]q[0xf5][0x1f][0xcc]P[0xb][0x8f][0xa8][0xfe]b[0x80]\}G[0xf3][0x14][0x2][0xe3][0xea]?[0x98][0xa0]?[0xb]([0xf5][0x1f][0xef][0xff][0x0]u[0x1]o>TQ#1p,57[0xa1][0xca]U1[0xfa][0x83][0x17]~[0xdf]u?[0xfb]{[0xd9][0xea]:[0xfe][0xdf][0xdc][0xeb][0xf5][0xfc]+[0x1e]$I[0xf4][0xdf][0xb0][0xff][0xd9]"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  >> "--"
DEBUG: org.apache.http.wire -  >> "9mJHoR7Kcwrv0CH0yvzVfgQiI94oiVXLoNUz3"
DEBUG: org.apache.http.wire -  >> "--"
DEBUG: org.apache.http.wire -  >> "[\r][\n]"
DEBUG: org.apache.http.wire -  << "HTTP/1.1 200 OK[\r][\n]"
DEBUG: org.apache.http.wire -  << "Date: Wed, 15 Jan 2014 22:19:23 GMT[\r][\n]"
DEBUG: org.apache.http.wire -  << "Server: Apache/2.2.15 (CentOS)[\r][\n]"
DEBUG: org.apache.http.wire -  << "Vary: Host[\r][\n]"
DEBUG: org.apache.http.wire -  << "X-Powered-By: PHP/5.3.3[\r][\n]"
DEBUG: org.apache.http.wire -  << "Set-Cookie: 761e67b124cfb51e47e25b2f56688acd=syQsU8WIwHlT4Y%2FT8JkAJFFq9FXwd1oGF70KSqJwVyZUL%2B5N45u6%2B85q7ac%2FJeVWdZXkV8Ck6EAfxHpPHJ5MYQHKx%2BysN%2FzbSjYjbKCuL4uBRVhdoGcjhqrhu86tR3E89TJbRn3%2FMwP%2B6yPR9P%2FPFwnnw%2F%2BL%2BDbK0evNWGu%2BLe%2FyvDmd4%2BLtXC2CNfvVDyJkNdsJF21dZMolf7dFV4UC5TeXgTx5va1T00i%2Bv2M5Eh%2F7Hjpoe%2FXoHD87xH17j5Ld%2BDzCYpLMM0VXfjgQt4Hf43itZNPN2l4iLwBLwwohgf8%3D000220; expires=Tue, 04-Feb-2014 22:19:24 GMT; path=/; domain=domain; httponly[\r][\n]"
DEBUG: org.apache.http.wire -  << "Content-Length: 111[\r][\n]"
DEBUG: org.apache.http.wire -  << "Connection: close[\r][\n]"
DEBUG: org.apache.http.wire -  << "Content-Type: text/html; charset=utf-8[\r][\n]"
DEBUG: org.apache.http.wire -  << "[\r][\n]"
DEBUG: org.apache.http.impl.conn.DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
DEBUG: org.apache.http.headers - << HTTP/1.1 200 OK
DEBUG: org.apache.http.headers - << Date: Wed, 15 Jan 2014 22:19:23 GMT
DEBUG: org.apache.http.headers - << Server: Apache/2.2.15 (CentOS)
DEBUG: org.apache.http.headers - << Vary: Host
DEBUG: org.apache.http.headers - << X-Powered-By: PHP/5.3.3
DEBUG: org.apache.http.headers - << Set-Cookie: 761e67b124cfb51e47e25b2f56688acd=syQsU8WIwHlT4Y%2FT8JkAJFFq9FXwd1oGF70KSqJwVyZUL%2B5N45u6%2B85q7ac%2FJeVWdZXkV8Ck6EAfxHpPHJ5MYQHKx%2BysN%2FzbSjYjbKCuL4uBRVhdoGcjhqrhu86tR3E89TJbRn3%2FMwP%2B6yPR9P%2FPFwnnw%2F%2BL%2BDbK0evNWGu%2BLe%2FyvDmd4%2BLtXC2CNfvVDyJkNdsJF21dZMolf7dFV4UC5TeXgTx5va1T00i%2Bv2M5Eh%2F7Hjpoe%2FXoHD87xH17j5Ld%2BDzCYpLMM0VXfjgQt4Hf43itZNPN2l4iLwBLwwohgf8%3D000220; expires=Tue, 04-Feb-2014 22:19:24 GMT; path=/; domain=domain; httponly
DEBUG: org.apache.http.headers - << Content-Length: 111
DEBUG: org.apache.http.headers - << Connection: close
DEBUG: org.apache.http.headers - << Content-Type: text/html; charset=utf-8
DEBUG: org.apache.http.client.protocol.ResponseProcessCookies - Cookie accepted: "[version: 0][name: 761e67b124cfb51e47e25b2f56688acd][value: syQsU8WIwHlT4Y%2FT8JkAJFFq9FXwd1oGF70KSqJwVyZUL%2B5N45u6%2B85q7ac%2FJeVWdZXkV8Ck6EAfxHpPHJ5MYQHKx%2BysN%2FzbSjYjbKCuL4uBRVhdoGcjhqrhu86tR3E89TJbRn3%2FMwP%2B6yPR9P%2FPFwnnw%2F%2BL%2BDbK0evNWGu%2BLe%2FyvDmd4%2BLtXC2CNfvVDyJkNdsJF21dZMolf7dFV4UC5TeXgTx5va1T00i%2Bv2M5Eh%2F7Hjpoe%2FXoHD87xH17j5Ld%2BDzCYpLMM0VXfjgQt4Hf43itZNPN2l4iLwBLwwohgf8%3D000220][domain: dev123d.emmagallery.pl][path: /][expiry: Tue Feb 04 23:19:24 CET 2014]". 
HTTP/1.1 200 OK
DEBUG: org.apache.http.wire -  << "{"error":"An error occurred during the image upload"}"
DEBUG: org.apache.http.impl.conn.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@7bed48b2
DEBUG: org.apache.http.impl.conn.DefaultClientConnection - Connection 0.0.0.0:63044<->IP:80 shut down
{"error":"An error occurred during the image upload"}

你能帮助我吗?

现在我看到,这个代码添加到服务器上创建的新文件类似于:

    --IgW39GW2Czn20AXX5ENlZ-xA8j-fOMM2^M
Content-Disposition: form-data; name="file"^M
Content-Type: image/jpeg^M
Content-Transfer-Encoding: binary^M
^M

你知道为什么,以及如何避免它?

2 个答案:

答案 0 :(得分:0)

实体设置两次

httppost.setEntity(new UrlEncodedFormEntity(postParameters)); //it is overwritten by next one
httppost.setEntity(mpEntity);

具有不同的值 - 因此使用最后一个。您应该向multipart实体添加参数。

答案 1 :(得分:0)

尝试这样的事情:

HttpURLConnection conn = (HttpURLConnection) new URL("http://something").openConnection();
OutputStream outputStream = conn.getOutputStream();
// Write your object to the output stream
InputStream inputStream = conn.getInputStream();