简体   繁体   中英

sample code for a rest file upload client using AsyncHttpClient

I need some help creating a java client using com.ning.http.client.AsyncHttpClient that will upload a file using the rest service below

I tried using a client with this cod, but the FileInput stream reach to the server is null:

FilePart filePart = new FilePart("File", file, null, null);

builder.setBody(new FileInputStream(file)); Or builder.addBodyPart(filePart);

Service code is: @Path("/file") public class 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);

}

}

builder.setHeader("Content-Type", javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA); builder.addBodyPart(new FilePart("file", file, "application/octet-stream", UTF_8.name())); final Response response = httpClient.executeRequest(builder.build()).get(10, TimeUnit.SECONDS);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM