Answer the question
In order to leave comments, you need to log in
How to properly check the file download service?
Good afternoon.
There is a service for downloading a file and data at the same time in one method (maybe that's wrong).
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, produces = "application/json", consumes="multipart/form-data")
@ResponseBody
public ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile multipartFile,
@RequestBody Users users) {
if (multipartFile == null)
return new ResponseEntity<String>("Error", HttpStatus.NOT_FOUND);
String strPath = Utils.createDirectoryToUser(users);
if (strPath == null) {
return new ResponseEntity<String>("Error", HttpStatus.INTERNAL_SERVER_ERROR);
}
try {
int read = 0;
File newFile = new File(strPath + "/" + multipartFile.getName());
FileOutputStream fos = new FileOutputStream(newFile);
CountingOutputStream out = new CountingOutputStream(fos);
byte[] bytes = new byte[1024];
while ((read = multipartFile.getInputStream().read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return new ResponseEntity(strPath + "/" + multipartFile.getName(), HttpStatus.OK);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question