Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JAVA中通过POST上传文件_Java_Rest_Post_File Upload_Httpurlconnection - Fatal编程技术网

如何在JAVA中通过POST上传文件

如何在JAVA中通过POST上传文件,java,rest,post,file-upload,httpurlconnection,Java,Rest,Post,File Upload,Httpurlconnection,我正试图用JAVA中的POST上传一个文本文件。 但是我发送的数据包缺少包含文件内容的正文 如果我使用的是POSTMAN chrome ext,我会收到以下数据包: POST http://localhost:8080/storm-webapp/load-scripts/ HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 205 Cache-Control: no-cache Origin: chrome-e

我正试图用JAVA中的POST上传一个文本文件。 但是我发送的数据包缺少包含文件内容的正文

如果我使用的是POSTMAN chrome ext,我会收到以下数据包:

POST http://localhost:8080/storm-webapp/load-scripts/ HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 205
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryq3Ss6jb9i23grqZA
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: CoreI18n=en-US

------WebKitFormBoundaryq3Ss6jb9i23grqZA
Content-Disposition: form-data; name="file"; filename="HelloWorld.txt"
Content-Type: text/plain

This is a test text to upload
------WebKitFormBoundaryq3Ss6jb9i23grqZA--
如果我正在通过java代码创建上传,我将获得下一个原始数据:

POST http://localhost:8888/storm-webapp/load-scripts/ HTTP/1.1
Content-Type: multipart/form-data; boundary=----13f7a4f68b9
User-Agent: Java/1.7.0_17
Host: localhost:8888
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 0
如你所见,我失去了身体的整个部分。。。 我使用的代码是:

    BufferedReader reader = null;
    File file = new File("HelloWorld.txt");

    String boundary = "WebKitFormBoundary" + Long.toHexString(System.currentTimeMillis());
    HttpURLConnection conn = (HttpURLConnection) new URL(rest).openConnection();
    /*************Header*****************/
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----" + boundary);
    /*********Body**************/
    PrintWriter writer = null;
    writer = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
    writer.println("------" + boundary);
    writer.println("Content-Disposition: form-data; name=\"file\"; filename=\"HelloWorld.txt\"");
    writer.println("Content-Type: text/plain");
    writer.println();

    reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    for (String line; (line = reader.readLine()) != null;) {
        writer.println(line);
    }
    writer.println("------" + boundary + "--");
    /*******************************************************/

    reader.close();

    int responseCode = conn.getResponseCode();

    System.out.println("ResponseCode is : " + responseCode);
响应代码是400

知道我做错了什么吗?
谢谢

您没有设置实际为文件大小的内容长度?通过file.length()获取它,使用file.length()将内容长度添加到连接中(文件大小以字节为单位)。在有意义地调用conn.getResponseCode()之前,您是否也不需要调用writer.close()?