Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File upload progress bar using RestTemplate.postForLocation

I have a Java desktop client application that uploads files to a REST service.

All calls to the REST service are handled using the Spring RestTemplate class.

I'm looking to implement a progress bar and cancel functionality as the files being uploaded can be quite big.

I've been looking for a way to implement this on the web but have had no luck.

I tried implementing my own ResourceHttpMessageConverter and substituting the writeInternal() method but this method seems to be called during some sort of buffered operation prior to actually posting the request (so the stream is read all in one go before sending takes place).

I've even tried overriding the CommonsClientHttpRequestFactory.createRequest() method and implementing my own RequestEntity class with a special writeRequest() method but the same issue occurs (stream is all read before actually sending the post).

Am I looking in the wrong place? Has anyone done something similar.

A lot of the stuff I've read on the web about implementing progress bars talks about staring the upload off and then using separate AJAX requests to poll the web server for progress which seems like an odd way to go about it.

Any help or tips greatly appreciated.

like image 204
glidester Avatar asked Dec 14 '12 17:12

glidester


People also ask

How do I upload files to RestTemplate?

Uploading a Single File We need to create HttpEntitywith header and body. Set the content-type header value to MediaType. MULTIPART_FORM_DATA. When this header is set, RestTemplate automatically marshals the file data along with some metadata.

How show upload progress bar in asp net?

To do that, Open Visual Studio -> New Project -> A new dialog window will appear -> In left pane select C# ->select Web -> Select "ASP.NET MVC 4 Application" name your project and click ok. In this article we are going to learn how to implement jquery progress bar in File Upload control in ASP MVC.


Video Answer


1 Answers

This is an old question but it is still relevant.

I tried implementing my own ResourceHttpMessageConverter and substituting the writeInternal() method but this method seems to be called during some sort of buffered operation prior to actually posting the request (so the stream is read all in one go before sending takes place).

You were on the right track. Additionally, you also needed to disable request body buffering on the RestTemplate's HttpRequestFactory, something like this:

HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
clientHttpRequestFactory.setBufferRequestBody(false);
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

Here's a working example for tracking file upload progress with RestTemplate.

like image 75
ElectroBuddha Avatar answered Oct 19 '22 06:10

ElectroBuddha