use SpringMVC + mongodb to upload and download files.
downloaded files cannot be opened except TXT (garbled code).
1. Suspected to be a problem with springmvc messageconverter configuration, tried to configure byteArrayConverter
2. Suspected to be uploaded to mongodb file corruption, Google and Baidu did not find the relevant content
3. I have tried to use Response < InputstreamResource > to return the file, and the result is the same
download and upload the code as follows. Ask the boss to check what the problem is
.:ResponseEntity<byte[]>springmvc ByteArrayConverter
:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>image/jpeg</value>
<value>image/png</value>
<value>application/pdf</value>
<value>image/gif</value>
<value>application/octet-stream</value>
<value>text/plain</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
rest:
/**
*
*
* @param fileId ID
* @return
*/
@CrossOrigin
@RequestMapping(value = "/download/{fileId}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<byte[]> fileDownload(@PathVariable String fileId) throws Exception {
//MongoDB
GridFSDBFile file = fileService.getFileById(fileId);
String fileType = file.getContentType();
//ContentType
MediaType mediaType = FileUtil.getEnableStr(fileType);
//
byte[] outByte = FileUtil.gridFSBFileToByte(file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(mediaType);
headers.setContentDispositionFormData("attachment",file.getFilename()+"."+fileType);
//
return new ResponseEntity<>(outByte, headers, HttpStatus.OK);
}
rest2:
/**
*
*
* @param fileId ID
* @return
*/
@CrossOrigin
@RequestMapping(value = "/download2/{fileId}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> fileDownload2(@PathVariable String fileId) throws Exception {
//MongoDB
GridFSDBFile file = fileService.getFileById(fileId);
String fileType = file.getContentType();
//ContentType
MediaType mediaType = FileUtil.getEnableStr(fileType);
InputStreamResource resource = new InputStreamResource(file.getInputStream());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(mediaType);
headers.setContentDispositionFormData("attachment",file.getFilename()+"."+fileType);
//
return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
service:
@Override
public GridFSDBFile getFileById(String fileId) throws RestServiceException {
GridFSDBFile file =
gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
if (file == null) {
throw new RestServiceException("fileId");
}
return file;
}
upload part:
rest:
/**
* MongoDB
*
* @param file
* @return
*/
@CrossOrigin
@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ReturnInfo fileUpload(@RequestParam MultipartFile file) throws Exception {
ReturnInfo<Map> info = new ReturnInfo<>();
if (!file.isEmpty()) {
BufferedInputStream inputStream = new BufferedInputStream(file.getInputStream());
String fileId = fileService.fileUpload(inputStream);
info.setCode(ReturnInfo.OK);
info.setMessage("");
Map<String, String> resultMap = new HashMap<>();
resultMap.put("fileId", fileId);
info.setData(resultMap);
return info;
} else {
info.setMessage("");
info.setCode(ReturnInfo.ERROR);
return info;
}
}
service:
/**
*
*
* @param fileInput
* @return mongodbid
*/
@Override
public String fileUpload(InputStream fileInput) throws RestServiceException {
String fileType = FileUtil.getFileType(fileInput);
System.out.println(fileType);
//
if (!FileUtil.ENABLE_TYPES.contains(fileType)) {
throw new RestServiceException("");
}
GridFSFile uploadFile = gridFsTemplate.store(fileInput, StringUtil.getUUID(), fileType);
return uploadFile.getId().toString();
}