SpringBoot实现文件上传下载详解

网友投稿 715 2022-11-29

SpringBoot实现文件上传-详解

SpringBoot实现文件上传-详解

文件上传模块:

思路: MultipartFile用来接受前台传来的文件 transferTo方法:把这个文件路径所指向的文件上传到对应的目录下。

首先,需要设置一个存放上传文件的目录,可以在接口中指定,也可以在配置文件中提前设置好。通过拼接的方式获取该目录的绝对路径,将此路径转换为抽象路径名来创建一个File实例(这里的file并非是真实的文件,下图为官方的解释)。

最后,将前端传来的文件保存在该目录下,整个上传功能到此就结束了。

@PostMapping("/upload") public String upload(@RequestParam("file") MultipartFile[] file, HttpSession session) throws Exception { for (MultipartFile multipartFile : file) { // 文件大小 long size = multipartFile.getSize(); // 老文件名字 String originalFilename = multipartFile.getOriginalFilename(); System.out.println("+++++++++文件的完整名称:" + originalFilename); // 文件类型 不是文件的扩展名 String contentType = multipartFile.getContentType(); System.out.println("++++++++文件类型" + contentType); // 获取文件后缀名 String[] split = contentType.split("/"); String ext = "." + split[1]; // ResourceUtils工具类获取classes目录绝对路径 输出/G:/javaAllCode/file_upload_and_download/target/classes // 后面static/upload是上传目录 这个整体组成将获取-路径url String s = ResourceUtils.getURL("classpath:").getPath() + "static/upload"; System.out.println("++++++++++++-路径" + s); // 对字符串进行URL解码 String url = URLDecoder.decode(s); System.out.println("++++++++解码url" + url); // 创建新的文件 Files files = new Files(); // 设置文件类型的扩展名 files.setExt(ext); System.out.println("+++++++扩展名" + ext); // 创建新文件名字 日期格式化+唯一识别码+文件类型的扩展名 String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + UUID.randomUUID().toString().replace("-", "") + ext; System.out.println("+++++新文件名字" + newFileName); // 新文件名 files.setNewfilename(newFileName); // 旧文件名 files.setOldfilename(originalFilename); files.setSize(size); // 上传时间 files.setUploadTime(new Date()); // 格式化日期 String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); // 目录名称 String catalogName = url + "/" + format; // 存储名称 files.setPath("/upload/" + format); System.out.println("+++++++存储路径"+url+format); // 设置文件的类型 files.setType(contentType); // 得到用户的信息 User user_info = (User) session.getAttribute("USER_INFO"); files.setUserId(user_info.getId()); // 初始化这个值为0 files.setDowncounts(0); // 判断是不是图片 if (multipartFile.getContentType().indexOf("image") != -1) { // 判断是不是图片 files.setIsimg("是"); } else { files.setIsimg("否"); } // 创建目录 File fileDir = new File(catalogName); if (!fileDir.exists()) { fileDir.mkdirs(); } System.out.println("+++++++++++-目录" + fileDir.getAbsolutePath()); // 将文件-到 目录里面写入文件 -主要用的是这个方法 // Creates a new File instance from a parent pathname string and a child pathname string. // 通过父目录的路径民称和新文件名来创建一个file实例 将前端的文件 // 写进去复制进去 multipartFile.transferTo(new File(catalogName, newFileName)); // 保存到数据库 filesService.insert(files); } return "redirect:/file/showALL"; }

文件-模块

思路:用IOUtils中的copy方法,通过ioutil 对接输入输出流,实现文件-。参考​​IOUtils实现文件-​​

获取文件的输入流,创建文件的输出流对象,通过缓冲区的方式从输入流中读入数据,再写到输出流中,知道缓冲区不再有数据。最后,先关闭输出流,再关闭输入流。注意:当请求-的文件是中文名称时,需要对文件名的编码转为utf-8格式,不然后端接受的文件名是乱码,导致无法成功-。

RequestMapping("/dowload") public void dowload(HttpServletResponse response, int id) throws IOException { // 通过id查询到这个文件 Files files = filesService.queryById(id); System.out.println("++++++++++"+files); // 得到文件的路径 String Path = ResourceUtils.getURL("classpath:").getPath() + "/static" + files.getPath(); System.out.println("++++++文件的-路径" + Path); String decode = URLDecoder.decode(Path); System.out.println("+++++++++路径" + decode); // 通过文件的路径 得到需要-的文件 为这个文件创建输入流 FileInputStream in = new FileInputStream(new File(URLDecoder.decode(Path), files.getNewfilename())); String oldfilename = files.getOldfilename(); System.out.println(oldfilename); // 设置返回值 response.setHeader( "Content-Disposition", "attachment;filename=" + URLEncoder.encode(files.getOldfilename(), "UTF-8"));// // servlet输出流 ServletOutputStream os = response.getOutputStream(); // copy 这个方法将内容按字节从一个InputStream对象复制到一个OutputStream对象,并返回复制的字节数。 //通过ioutil 对接输入输出流,实现文件- IOUtils.copy(in, os); // closeQuietly它将无条件的关闭一个可被关闭的对象而不抛出任何异常。 // 它也有很多版本去支持关闭所有的InputStream、OutputStream、Reader和Writer。 IOUtils.closeQuietly(in); IOUtils.closeQuietly(os); // 设置-次数+1 files.setDowncounts(files.getDowncounts() + 1); // 更新数据库中的数据 filesService.update(files); }

参考:​​传入HttpServletResponse对象response 和 文件保存位置pathpublic static void downloadFile(HttpServletResponse response, String path) { try { response.setCharacterEncoding("UTF-8"); File file = new File(path); //如果文件不存在 if (file == null || !file.exists()) { ResponseState responseState = new ResponseState(); responseState.setMsg("文件不存在!"); PrintWriter out = response.getWriter(); out.write(JSON.toJSONString(responseState)); out.flush(); out.close(); } String simpleName = file.getName().substring(file.getName().lastIndexOf("/") + 1); String newFileName = new String(simpleName.getBytes(), "utf-8"); response.setHeader("Content-disposition", "attachment;filename=" + newFileName); BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream( response.getOutputStream()); byte[] buffer = new byte[1024]; int length; while ((length = bis.read(buffer)) != -1) { bos.write(buffer, 0, length); } if (bis != null){ bis.close(); } if (bos != null){ bos.close(); } } catch (Exception e) { // TODO: handle exception }}

Postman测试

传入文件path即可进行-

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Postman配置token进行测试
下一篇:MyBatis详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~