简体   繁体   English

如何在Java中对InputStream进行HTTP POST

[英]How to do HTTP POST for InputStream in Java

I have a JAX RS method that accepts the uploaded file as follows 我有一个接受上载文件的JAX RS方法,如下所示

@POST
@Path("/entity/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail)   {
  // Upload this file to another remote API again on secret server
}

Can someone suggest how can I use InputStream to forward this file to another server that has similar consumer ? 有人可以建议我如何使用InputStream将此文件转发到具有类似使用者的另一台服务器吗?

I tried this, which did not work. 我试过了,但是没有用。 Something is missing 缺了点什么

// Using com.ning.http.client.AsyncHttpClient
final FluentCaseInsensitiveStringsMap map = new   FluentCaseInsensitiveStringsMap();
map.add("file", fileDetail.getFileName());
map.add("Content-Type","multipart/form-data; boundary=" + boundary);
AsyncHttpClient.BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(postURL);
Response response = requestBuilder.setBody(IOUtils.toByteArray(uploadedInputStream)).setHeaders(map).execute().get();

I'm guessing you're using Jersey. 我猜您正在使用Jersey。 In that case just use the Jersey client API. 在这种情况下,只需使用Jersey客户端API。 You already have the multipart support dependency. 您已经具有多部分支持依赖性。 You just need to use the correct APIs. 您只需要使用正确的API。 For example 例如

FormDataMultiPart multiPart = new FormDataMultiPart()
        .field("file", uploadedInputStream, MediaType.MULTIPART_FORM_DATA);
Client client = ClientBuilder.newClient();
String url = "...";
Response response = client.target(url).request()
        .post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
String responseAsString = response.readEntity(String.class);

See more information 查看更多信息

Not sure what Jersey version you are using, but the above is the Jersey 2.x client API. 不知道您使用的是哪个Jersey版本,但是上面是Jersey 2.x客户端API。 If you are using Jersey 1.x, the API is a little different. 如果您使用的是Jersey 1.x,则API会有所不同。 See here for example 例如看这里

Try this 尝试这个

import com.ning.http.client.*;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
    @Test
        public void post() throws IOException, InterruptedException {
            File tmpFile = File.createTempFile("textbytearray", ".txt");
            AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
            AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.preparePost("http://xxxxxxxxx");
            String BOUNDARY = "----WebKitFormBoundaryiDGnV9zdZA1eM1yL";
            builder.addHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            builder.setBodyEncoding("UTF-8");
            FilePart part = new FilePart("audio", tmpFile, "", "");
            builder.addBodyPart(part);
            builder.addBodyPart(new StringPart("from", "1"))
                    .execute(new AsyncCompletionHandler<Response>() {

                        @Override
                        public Response onCompleted(Response response) throws Exception {
                            System.out.println(response.getResponseBody());
                            return response;
                        }

                        @Override
                        public void onThrowable(Throwable t) {
                            System.out.println(t);
                        }
                    });

            Thread.sleep(1000000);
        }

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

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