简体   繁体   English

如何通过apache-httpclient上传文件?

[英]How to upload a file via apache-httpclient?

I want to upload a file as described here: http://docs.octoprint.org/en/master/api/files.html#upload-file-or-create-folder 要按此处所述上传文件: http : //docs.octoprint.org/en/master/api/files.html#upload-file-or-create-folder

I use apache-httpclient to send a post connection, but whenever I start the method, nothing happens and the application got stuck without error, but just doesn't do anything. 我使用apache-httpclient发送后连接,但是每当我启动该方法时,都不会发生任何事情,并且应用程序被卡住而没有错误,但是什么也没做。

Other POST and GET requests are working and I don't need an API-Key because I turned off the authentication 其他POST和GET请求正在运行,并且我不需要API密钥,因为我关闭了身份验证

CloseableHttpClient posterClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://localhost:5002/api/files/local");
        post.setHeader("Host", "http://localhost:5002");
        post.setHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryDeC2E3iWbTv1PwMC");

        post.setHeader("X-Api-Key", "");


        HttpEntity entity = MultipartEntityBuilder
                .create()

                .addBinaryBody("file", new File("/Users/florian/eclipse-workspace/docker-Client/src/main/java/test/dog3.gcode"), ContentType.create("multipart/form-data"), "dog.gcode")
                .addTextBody("select", "true")
                .addTextBody("print", "true")
                .build();


        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        entity.writeTo(bytes);
        String content = bytes.toString();
        StringEntity entity2 = new StringEntity(content, ContentType.APPLICATION_OCTET_STREAM);
        entity2.setContentEncoding("application/octet-stream");
        post.setEntity(entity2);
        CloseableHttpResponse answer = posterClient.execute(post);

If I print the POST, I'll get (the gcode-instructions deleted): 如果我打印POST,我会得到(删除gcode指令):


RequestLine:POST http://localhost:5002/api/files/local HTTP/1.1
Header:boundary=----WebKitFormBoundaryDeC2E3iWbTv1PwMC;
Content:--JIDzHYxyG74480VXGugHYTI-7xe5OrZ8
Content-Disposition: form-data; name="file"; filename="dog.gcode"
Content-Type: multipart/form-data
Content-Transfer-Encoding: binary

M5
G90
G21
G1 F3000
G1  X18.0359 Y51.0881
M3 S90
G4 P0.3

--JIDzHYxyG74480VXGugHYTI-7xe5OrZ8
Content-Disposition: form-data; name="select"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

true
--JIDzHYxyG74480VXGugHYTI-7xe5OrZ8
Content-Disposition: form-data; name="print"
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

true
--JIDzHYxyG74480VXGugHYTI-7xe5OrZ8--

So... not exactly what I want... 所以...不是我想要的...

Okay by now my code looks like this and it works. 好的,现在我的代码看起来像这样并且可以正常工作。 To delete unnecessary lines like Content-Type-Encoding, you have to set the ode to "browser-compatible" 要删除不必要的行,例如Content-Type-Encoding,必须将ode设置为“浏览器兼容”

CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpEntity entity = MultipartEntityBuilder.create().setMode(
HttpMultipartMode.BROWSER_COMPATIBLE).setBoundary("----WebKitFormBoundaryDeC2E3iWbTv1PwMC").setContentType(
ContentType.MULTIPART_FORM_DATA)
                .addBinaryBody("file",
                        new File("PATH"),
                        ContentType.APPLICATION_OCTET_STREAM, "filename.gcode")
                .addTextBody("select", "true", ContentType.MULTIPART_FORM_DATA).addTextBody("print", "true", ContentType.MULTIPART_FORM_DATA)
                .build();

        HttpPost httpPost = new HttpPost("http://localhost:5002/api/files/local");
        httpPost.addHeader("Host", "http://localhost:5002");
        httpPost.addHeader("X-Api-Key", "88CC15F3B5864CCAB19981A8B67A4071");
        httpPost.addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryDeC2E3iWbTv1PwMC");
        httpPost.setEntity(entity);
        //betterPrint(httpPost);
        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity result = response.getEntity();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解码apache-httpclient生成的SSL流量? - How can I decode ssl traffic generated by apache-httpclient? 使用apache-httpClient,getResponseBodyAsStream结果不一样 - use apache-httpClient, getResponseBodyAsStream result is not the same GbaRequest:构造函数调用222 userAgent Apache-HttpClient / UNAVAILABLE解析XML文件 - GbaRequest: Constructor Called 222 userAgent Apache-HttpClient/UNAVAILABLE Parsing XML File apache HttpClient,通过表单上传文件:使用StringBody代替FileBody - apache HttpClient, upload a file via form: use StringBody instead of FileBody 如何从 Apache HttpClient 4.x 通过 HttpPut 上传 InputStream - How to upload InputStream via HttpPut from Apache HttpClient 4.x 如何使用Apache HttpClient 4获取文件上传的进度条? - How to get a progress bar for a file upload with Apache HttpClient 4? 使用邮递员上传文件有效,但使用 Apache HttpClient 上传失败 - File upload with postman works but fails with Apache HttpClient 使用 Apache HttpClient 上传 2GB 文件 - Upload 2GB file with Apache HttpClient 警告/错误含义 - GbaRequest - GbaRequest:构造函数调用222 userAgent Apache-HttpClient / UNAVAILABLE - Warning/Error meaning - GbaRequest - GbaRequest: Constructor Called 222 userAgent Apache-HttpClient/UNAVAILABLE Apache HttpClient POST 上传文件不适用于 MultipartEntityBuilder 上传 - Apache HttpClient POST Upload File won't work with MultipartEntityBuilder upload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM