FileServiceImpl.java 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package cn.seecoder.courselearning.serviceimpl.file;
  2. import cn.seecoder.courselearning.exception.MyException;
  3. import cn.seecoder.courselearning.service.file.FileService;
  4. import cn.seecoder.courselearning.util.Constant;
  5. import cn.seecoder.courselearning.util.FileHelper;
  6. import cn.seecoder.courselearning.vo.file.FileInfoVO;
  7. import cn.seecoder.courselearning.vo.ResultVO;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. import java.nio.charset.StandardCharsets;
  19. @Service
  20. public class FileServiceImpl implements FileService {
  21. private static final Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
  22. @Value("${web.file-upload-path}")
  23. private String path;
  24. @Override
  25. public ResultVO<FileInfoVO> uploadFile(MultipartFile file) {
  26. try {
  27. FileInfoVO fileInfoVO = FileHelper.save(path, file);
  28. return new ResultVO<>(Constant.REQUEST_SUCCESS, "文件上传成功", fileInfoVO);
  29. } catch (MyException myException){
  30. logger.error("用于存放上传文件的文件夹不存在或创建失败,请检查路径是否有效", myException);
  31. } catch (IOException ioException){
  32. logger.error("文件复制时出错", ioException);
  33. }
  34. return new ResultVO<>(-1, "服务器错误,请联系网站管理员。");
  35. }
  36. @Override
  37. public void downloadFile(String originName, String newName, HttpServletResponse response) {
  38. InputStream inputStream = null;
  39. OutputStream outputStream = null;
  40. response.setContentType("application/x-msdownload");
  41. try {
  42. Resource resource = FileHelper.loadFileAsResource(path, originName);
  43. if(resource == null)
  44. return;
  45. inputStream = resource.getInputStream();
  46. //1.设置文件ContentType类型
  47. response.setContentType("application/octet-stream;charset=UTF-8");
  48. outputStream = response.getOutputStream();
  49. //2.转码 UTF_8为传入的newName编码的格式 ISO_8859_1为浏览器默认编码
  50. String convertName = new String(newName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
  51. //3.设置 header Content-Disposition
  52. response.setHeader("Content-Disposition", "attachment; filename=" + convertName);
  53. int b = 0;
  54. byte[] buffer = new byte[2048];
  55. while (b != -1) {
  56. b = inputStream.read(buffer);
  57. if (b != -1) {
  58. outputStream.write(buffer, 0, b);
  59. }
  60. }
  61. } catch (IOException | MyException e) {
  62. logger.error("文件下载时出错", e);
  63. } finally {
  64. try {
  65. if(inputStream != null)
  66. inputStream.close();
  67. if (outputStream != null)
  68. outputStream.close();
  69. } catch (IOException e) {
  70. logger.error("输入流或输出流关闭时出错!", e);
  71. }
  72. }
  73. }
  74. }