• hutool实现文件上传与下载


    1. <dependency>
    2. <groupId>cn.hutoolgroupId>
    3. <artifactId>hutool-allartifactId>
    4. <version>5.8.16version>
    5. dependency>

    文件上传需要创建一个表

    1. @Autowired
    2. private SysFileInfoMapper sysFileInfoMapper;
    3. @Value("${ty.profile}")
    4. private String profile;
    5. @PostMapping("/add")
    6. public AjaxResult addSave( MultipartFile file, SysFileInfo fileInfo) throws IOException
    7. {
    8. //获取文件名2.txt
    9. String originalFilename = file.getOriginalFilename();
    10. //给文件名添加前缀年月日时分秒毫秒,防止文件名称重复给顶掉
    11. String fileName = DateUtils.parseDateToStr("yyyyMMddHHmmssSSS", DateUtils.getNowDate()) +"_"+ originalFilename;
    12. //获取文件路径,主路径+文件名
    13. String path = profile+"/"+fileName;
    14. //使用huTool工具类实现文件上传
    15. FileUtil.writeBytes(file.getBytes(),path);
    16. //将文件名和文件路径存入表中
    17. fileInfo.setFileName(fileName);
    18. fileInfo.setFilePath(path);
    19. sysFileInfoMapper.insert(fileInfo);
    20. return AjaxResult.success("成功");
    21. }
    22. /**
    23. * 本地资源通用下载
    24. */
    25. @GetMapping("/common/download/resources")
    26. public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response)
    27. throws Exception
    28. {
    29. try {
    30. ServletOutputStream outputStream = response.getOutputStream();
    31. //根据文件名称查询文件路径
    32. LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
    33. lambdaQueryWrapper.eq(SysFileInfo::getFileName,resource);
    34. SysFileInfo sysFileInfo = sysFileInfoMapper.selectOne(lambdaQueryWrapper);
    35. if (ObjectUtil.isNotNull(sysFileInfo)){
    36. String filePath = sysFileInfo.getFilePath();
    37. response.setCharacterEncoding("utf-8");
    38. response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(sysFileInfo.getFileName(), "UTF-8"));
    39. response.setContentType("application/octet-stream");
    40. //通过文件的路径读取文件字节流,通过response的输出流返回文件
    41. outputStream.write(FileUtil.readBytes(filePath));
    42. outputStream.flush();
    43. outputStream.close();
    44. }
    45. }catch (Exception e){
    46. throw new SecurityException("文件下载失败");
    47. }
    48. }
    49. //实现pdf预览
    50. @GetMapping("/getPdfView")
    51. public void pdf(HttpServletRequest request, HttpServletResponse response) {
    52. try {
    53. File file = new File("C:\\hzx\\uploadPath\\2023\\10\\12\\打车去高铁站发票_20231012112938A001.pdf");
    54. FileInputStream fileInputStream = new FileInputStream(file);
    55. response.setHeader("Content-Type", "application/pdf");
    56. OutputStream outputStream = response.getOutputStream();
    57. IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
    58. } catch(Exception e) {
    59. e.printStackTrace();
    60. }
    61. }
    
                    
  • 相关阅读:
    IT运维全面数字化|芯片设计行业领跑打造运维流程闭环
    docker常用命令:docker制作镜像过程中常用的命令
    linux/CentOS 服务器中cpu、内存、磁盘及中间件监听,钉钉机器人告警
    nvidia-smi详解
    MCE | 曲贝替定——来自海洋的抗软组织肿瘤化合物
    力扣 -- 279. 完全平方数(完全背包问题)
    Vue--整合SVG Icon图标--方法/实例
    初识 Linux Shell
    Java网络编程
    无胁科技-TVD每日漏洞情报-2022-8-8
  • 原文地址:https://blog.csdn.net/DreamStat/article/details/133793270