你的位置:首页 > 软件开发 > Java > SpringMVC文件上传下载

SpringMVC文件上传下载

发布时间:2016-09-10 21:00:07
在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/qixiaoyizhan/p/5819392.html今天我们来讲讲spring ...

Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/qixiaoyizhan/p/5819392.html

今天我们来讲讲spring mvc中的文件上传和下载的几种方法。

首先附上文件目录->我们需要配置的我做了记号->

SpringMVC文件上传下载

 

一、文件上传


首先为了方便后续的操作,以及精简代码,我们在Utils包下封装一个文件上传下载的帮助类: Files_Helper_DG

SpringMVC文件上传下载

 1 package Utils; 2  3 import org.springframework.web.multipart.MultipartFile; 4  5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 import java.io.*; 8 import java.text.SimpleDateFormat; 9 import java.util.Date;10 import java.util.UUID;11 12 /**13  * Author:qixiao14  * Time:2016年9月2日23:47:5115 */16 public final class Files_Helper_DG {17   /**18    * 私有构造方法,限制该类不能被实例化19   */20   private Files_Helper_DG() {21     throw new Error("The class Cannot be instance !");22   }23 24   /**25    * spring mvc files Upload method (transferTo method)26    * spring mvc 中的文件上传方法 trasferTo 的方式上传,参数为MultipartFile27    *28    * @param request    HttpServletRequest29    * @param multipartFile MultipartFile(spring)30    * @param filePath   filePath example "/files/Upload"31    * @return32   */33   public static String FilesUpload_transferTo_spring(HttpServletRequest request, MultipartFile multipartFile, String filePath) {34     //get Date path35     String DatePath = new SimpleDateFormat("yyyyMMdd").format(new Date());36     //get server path (real path)37     String savePath = request.getSession().getServletContext().getRealPath(filePath) + File.separator + DatePath;38     // if dir not exists , mkdir39     System.out.println(savePath);40     File saveDir = new File(savePath);41     if (!saveDir.exists() || !saveDir.isDirectory()) {42       //create dir43       saveDir.mkdir();44     }45     if (multipartFile != null) {46       //get files suffix47       String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));48       //use UUID get uuid string49       String uuidName = UUID.randomUUID().toString() + suffix;// make new file name50       //filePath+fileName the complex file Name51       String fileName = savePath + File.separator + uuidName;52       //return relative Path53       String relativePath = filePath + File.separator + DatePath + File.separator + uuidName;54       try {55         //save file56         multipartFile.transferTo(new File(fileName));57         //return relative Path58         return relativePath;59       } catch (IOException e) {60         e.printStackTrace();61         return null;62       }63     } else64       return null;65   }66 67   /**68    * @param request HttpServletRequest69    * @param response HttpServletResponse70    * @param filePath example "/filesOut/Download/mst.txt"71    * @return72   */73   public static void FilesDownload_servlet(HttpServletRequest request, HttpServletResponse response, String filePath) {74     //get server path (real path)75     String realPath = request.getSession().getServletContext().getRealPath(filePath);76     File file = new File(realPath);77     String filenames = file.getName();78     InputStream inputStream;79     try {80       inputStream = new BufferedInputStream(new FileInputStream(file));81       byte[] buffer = new byte[inputStream.available()];82       inputStream.read(buffer);83       inputStream.close();84       response.reset();85       // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名86       response.addHeader("Content-Disposition", "attachment;filename=" + new String(filenames.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));87       response.addHeader("Content-Length", "" + file.length());88       OutputStream os = new BufferedOutputStream(response.getOutputStream());89       response.setContentType("application/octet-stream");90       os.write(buffer);// 输出文件91       os.flush();92       os.close();93     } catch (Exception e) {94       e.printStackTrace();95     }96   }97 }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:SpringMVC文件上传下载

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录