【问题标题】:sample code for a rest file upload client using AsyncHttpClient使用 AsyncHttpClient 的 REST 文件上传客户端的示例代码
【发布时间】:2019-08-21 17:51:38
【问题描述】:

我需要一些帮助来使用 com.ning.http.client.AsyncHttpClient 创建一个 java 客户端,它将使用下面的其余服务上传文件

我尝试使用带有此代码的客户端,但到达服务器的 FileInput 流为空:

FilePart filePart = new FilePart("文件", file, null, null);

builder.setBody(new FileInputStream(file)); 或者 builder.addBodyPart(filePart);

服务代码是: @Path("/文件") 公共类 UploadFileService {

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {

        //Given that I have ‘uploadedInputStream’ can I just pass this  
        //directly into the second call, below?

        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource wr = client.resource(baseURI);
        ClientResponse response = wr.type("image/*")
                                    .entity(uploadedInputStream)   //legal??
                                    .post(ClientResponse.class);

}

}

【问题讨论】:

    标签: java file upload client


    【解决方案1】:

    builder.setHeader("内容类型", javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA); builder.addBodyPart(new FilePart("file", file, "application/octet-stream", UTF_8.name())); 最终响应 response = httpClient.executeRequest(builder.build()).get(10, TimeUnit.SECONDS);

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2012-01-06
      • 2017-08-21
      • 1970-01-01
      相关资源
      最近更新 更多