求助: springmvc 上传至 mongodb 后下载文件无法打开

使用SpringMVC + mongodb 做文件的上传和下载,
下载的文件除TXT(乱码)外均无法打开。
图片描述
图片描述

1.怀疑是springmvc messageconverter配置的问题,尝试配置了byteArrayConverter
2.怀疑是上传存储到mongodb时文件损坏,谷歌和百度均未找到相关内容
3.尝试过使用Response<InputstreamResource>来返回文件,结果还是一样

下载和上传的代码如下,求大佬帮忙检查一下问题出在哪

下载部分:使用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;

}

上传部分:

    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 mongodb对应文件id

*/

@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();

}

以上是 求助: springmvc 上传至 mongodb 后下载文件无法打开 的全部内容, 来源链接: utcz.com/p/174222.html

回到顶部