您的位置:首页 > 游戏 > 手游 > 看男科一般花多少钱_logo艺术字转换器_ 今日头条_典型十大优秀网络营销案例

看男科一般花多少钱_logo艺术字转换器_ 今日头条_典型十大优秀网络营销案例

2024/10/5 20:20:23 来源:https://blog.csdn.net/quokka56/article/details/142100831  浏览:    关键词:看男科一般花多少钱_logo艺术字转换器_ 今日头条_典型十大优秀网络营销案例
看男科一般花多少钱_logo艺术字转换器_ 今日头条_典型十大优秀网络营销案例

文章目录

  • 关于Springboot 文件上传下载问题解决方案
    • 注意事项
    • 文件上传
    • 文件下载
    • 文件删除
    • 文件在线打开
    • 在写练习的时候,发现了一些小小的问题,已经在 上述代码中体现。
      • ① 代码路径碰到中文的时候,会有乱码,需要转换(内容中已解决)
      • ② 在下载文件的时候,无需转跳问题(内容中已解决)

关于Springboot 文件上传下载问题解决方案

我觉得最好的办法就是将这些代码先写一遍出来,然后再琢磨一下是什么原理,虽然代码有些繁琐

注意事项

在这里插入图片描述

我们可以注意到,这个文件上传 的 enctype 格式必须设置成 “multipart / from-data” 哦。

文件上传

先把需求罗列出来
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

代码如下:

    @PostMapping("/addFile")public String addFile(MultipartFile file,HttpSession request) throws IOException {// 获得旧文件名称String originalName = file.getOriginalFilename();// 获取文件的后缀String ext = "." + FilenameUtils.getExtension(originalName);// 获得新文件名称String newName = new SimpleDateFormat("yyyyMMdd").format(new Date()) + UUID.randomUUID().toString().replace("-","")+ext;// 获得储存的路径String path = ResourceUtils.getURL("classpath:").getPath()+"static/test/";String path1 = URLDecoder.decode(path, "utf-8");// 获取新的文件夹,要是不存在,就默认创建java.io.File files = new java.io.File(path1 + newName);if(!files.exists()) {boolean b = files.mkdirs();};// 获取文件的大小String size = file.getSize() + "kb";// 上传文件file.transferTo(files);File f = new File();f.setId(UUID.randomUUID().toString());f.setOldName(originalName);f.setNewName(newName);f.setExt(ext);f.setPath(path1);f.setSize(size);String houZhui = FilenameUtils.getExtension(originalName);if(houZhui.equals("png") || houZhui.equals("img") || houZhui.equals("jpg")){f.setStyle("是");}else{f.setStyle("否");}f.setImg(path1+newName);f.setDownCounts("0");f.setDownTime(new SimpleDateFormat("yyMMddHHmmss").format(new Date()));com.xiao.entity.User user = (com.xiao.entity.User) request.getAttribute("user");f.setUid(user.getId());filempl.addFiles(f);return "redirect:/selectFiles";}

文件下载

在这里插入图片描述

代码如下:

    @GetMapping("/download")public void downloadFile(@Param("id")String id , HttpServletResponse response) throws IOException {File file = filempl.queryFile(id);// 获取要下载文件的 urlString paths = ResourceUtils.getURL("classpath:").getPath()+"/static/test";//获取文件输入流FileInputStream is = new FileInputStream(new java.io.File(paths,file.getNewName()));// 附件下载response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode(file.getOldName(),"utf8"));// 获取输出响应流ServletOutputStream os = response.getOutputStream();IOUtils.copy(is,os);// 关闭流IOUtils.closeQuietly(is);IOUtils.closeQuietly(os);//        问题二:不需要返回,因为是下载,不存在页面转跳问题
//        return "forward:/selectFiles";

文件删除

在这里插入图片描述

代码如下:

    @GetMapping("/delFile")public String delFile(@Param("id")String id) throws IOException {// 根据id查找到当前的User对象File file = filempl.queryFile(id);String id1 =file.getNewName();// 如果文件存在,则删除文件boolean del = Files.deleteIfExists(Paths.get("D:\\javaPorject\\thirdTest\\target\\classes\\static\\test\\"+id1));// mapper 删除文件filempl.delFiles(id);return "forward:/selectFiles";}

文件在线打开

在这里插入图片描述

仅仅多了一个 “ inline; ”,就变成了在线打开。

在写练习的时候,发现了一些小小的问题,已经在 上述代码中体现。

① 代码路径碰到中文的时候,会有乱码,需要转换(内容中已解决)

② 在下载文件的时候,无需转跳问题(内容中已解决)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com