[英]How to upload a file via apache-httpclient?
我要按此處所述上傳文件: http : //docs.octoprint.org/en/master/api/files.html#upload-file-or-create-folder
我使用apache-httpclient發送后連接,但是每當我啟動該方法時,都不會發生任何事情,並且應用程序被卡住而沒有錯誤,但是什么也沒做。
其他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);
如果我打印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--
所以...不是我想要的...
好的,現在我的代碼看起來像這樣並且可以正常工作。 要刪除不必要的行,例如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.