Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

java - How to pass complex object with file data via REST

I need to download files from one source and pass this data to another application via rest. The file types are: .txt .csv and .zip for now. The file size could be up to 500Mb - 1 Gb.

What is the optimal way to do it. Should I convert java File objects to byte array at first? Will the Multipart content type be the most appropriate one for this purpose? I stacked a bit because the class I am going to transfer can contain different file types.

There is not code needed from your side just to have a clue how to handle it in a better way! ;)

The class below to transfer is:

public class FileEventsRequest {

private File originalFile;

private int rowCount;

private String md5;

private String cobDate;

private File controlFile; }
question from:https://stackoverflow.com/questions/65844387/how-to-pass-complex-object-with-file-data-via-rest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I worked on a task almost identical to the one you describe fairly recently! To download the file I used -

@PutMapping("/{originalFileName}")
public ResponseEntity<ImmutableDocument> send(@PathVariable String originalFileName, InputStream payload) {
    LOG.info("Receiving: {}", originalFileName);
    sendPayload(payload, originalFileName);
    return ResponseEntity.ok().build();
}

I chose InputStream as it's generally a bad idea to handle massive files 1GB in memory with a byte array, you could potentially blow the stack!

As for sending that file on to the target -

@Autowired
private RestTemplate sendTemplate;

private ResponseEntity<Void> sendPayload(final InputStream payload, final String originalFileName) throws IOException {
// You can send any other bits of information you need on the headers too
        HttpHeaders headers = new HttpHeaders();
        headers.put("originalFileName", originalFileName);
        headers.setContentType(asMediaType(MimeType.valueOf({desired mimetype})));
        headers.setAccept(singletonList(APPLICATION_OCTET_STREAM));
        HttpEntity<Resource> requestEntity = new HttpEntity<>(new InputStreamResource(payload), headers);
        UriComponentsBuilder builder = fromUriString({someurl});
        UriComponents uriComponents = builder.build().encode();
        return sendTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, requestEntity, Void.class);
    }
}

I also used my own Configuration class for the ResTemplate with a message converter for sending the binary InputStream -

@Configuration
public class RestClientConfiguration {

@Bean
public RestTemplate sendTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {
    return new RestTemplateBuilder()
        .requestFactory(() -> clientHttpRequestFactory)
        .messageConverters(new ResourceHttpMessageConverter())
        .build();
}

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...