How to send a file using HttpClient when I only have an input stream

I am trying to execute a POST file using HttpClient. However, I do not have access to the actual File, but I have access to it InputStream. Is there a way I can still send the file?

This is what I have done so far:

 public void sendFile (InputStream instream) {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod("http://localhost:8080/myservice/testupload");
    Part[] parts = new Part[] {
            //new FilePart("myFile", file.getName(), file)
    };
    method.setRequestEntity(
    new MultipartRequestEntity(parts, method.getParams()));
    client.executeMethod(method);
 }

as you can see, the file is FilePartneeded, but I have it InputStream. How can I post the input stream as a file?

+4
source share
1 answer

javadoc FilePart, , PartSource , PartSource, ByteArrayPartSource, byte[]; InputStream, .

+2

Source: https://habr.com/ru/post/1546495/


All Articles