You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
final SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
// Important so rest template does not read request into memory so we get sensible progress back.
simpleClientHttpRequestFactory.setBufferRequestBody(false);
final RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory);
final CallbackFormHttpMessageConverter formConverter = new CallbackFormHttpMessageConverter(tracker);
restTemplate.getMessageConverters().add(0, formConverter);
public class CallbackFormHttpMessageConverter extends FormHttpMessageConverter {
public CallbackFormHttpMessageConverter(final FileUploadProgressTracker callback) {
super();
final List<HttpMessageConverter<?>> partConverters = new ArrayList<>();
partConverters.add(new CallbackResourceHttpMessageConverter(callback));
// Continue to add in default for FormHttpMessageConverter.
partConverters.add(new ByteArrayHttpMessageConverter());
final StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false);
partConverters.add(stringHttpMessageConverter);
partConverters.add(new ResourceHttpMessageConverter());
setPartConverters(partConverters);
}
}
public class CallbackResourceHttpMessageConverter extends ResourceHttpMessageConverter {
private final FileUploadProgressTracker callback;
public CallbackResourceHttpMessageConverter(final FileUploadProgressTracker callback) {
this.callback = callback;
}
@Override
protected void writeInternal(final Resource resource, final HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
final InputStream in = resource.getInputStream();
try {
final long fileSize = resource.contentLength();
final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE];
long count = 0;
int n = 0;
int percentange = 0;
while (-1 != (n = in.read(buffer))) {
outputMessage.getBody().write(buffer, 0, n);
count += n;
percentange = (int) ((count * 100) / fileSize);
callback.trackProgress(percentange);
}
} finally {
try {
in.close();
} catch (final IOException ex) {
}
}
outputMessage.getBody().flush();
}
}
public interface FileUploadProgressTracker {
void trackProgress(int progress);
}
Implement the interface with your own class - in my example its called 'tracker'
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
No description provided.
The text was updated successfully, but these errors were encountered: