【问题标题】:How to upload a file via apache-httpclient?如何通过 apache-httpclient 上传文件?
【发布时间】:2019-08-28 21:20:12
【问题描述】:

我想上传一个文件,如下所述:http://docs.octoprint.org/en/master/api/files.html#upload-file-or-create-folder

我使用 apache-httpclient 发送一个 post 连接,但是每当我启动该方法时,什么都没有发生,应用程序卡在没有错误的情况下,但什么也没做。

其他 POST 和 GET 请求正在运行,我不需要 API-Key,因为我关闭了身份验证

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--

所以...不是我想要的...

【问题讨论】:

  • 目标文件夹是否存在?如果有,您是否有权在上面写字?
  • 是的,该文件夹存在并且我有写入权限(因为我有管理员权限由于禁用身份验证)。我想我在构建请求时遗漏了一些细节......
  • 我正在阅读代码并与文档进行比较,到目前为止我没有发现任何问题。是的,在这种情况下,我会打印请求以检查带有文档的标题。
  • 我看到你的文件路径中有一个 docker-client。你在使用码头工人吗?如果是这样,您可以尝试不使用容器吗?首先在本地尝试以避免任何容器配置问题。
  • 如果我打印(发布),只会得到“POST localhost:5002/api/files/local HTTP/1.1”...

标签: java apache-httpclient-4.x octoprint


【解决方案1】:

好的,现在我的代码看起来像这样并且它可以工作。要删除 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-14
    • 2017-11-05
    • 2021-02-21
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多