文件上传和下载
作者:互联网
1:导入包:
1 <dependencies> 2 <!--文件上传--> 3 <dependency> 4 <groupId>commons-fileupload</groupId> 5 <artifactId>commons-fileupload</artifactId> 6 <version>1.3.3</version> 7 </dependency> 8 <!--servlet-api导入高版本的--> 9 <dependency> 10 <groupId>javax.servlet</groupId> 11 <artifactId>javax.servlet-api</artifactId> 12 <version>4.0.1</version> 13 </dependency> 14 </dependencies>
2:配置:
1 <!--文件上传配置--> 2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 --> 4 <property name="defaultEncoding" value="utf-8"/> 5 <!-- 上传文件大小上限,单位为字节(10485760=10M) --> 6 <property name="maxUploadSize" value="10485760"/> 7 <property name="maxInMemorySize" value="40960"/> 8 </bean>
3:实现代码:
1 @RestController 2 public class FileController { 3 4 //@RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象 5 //批量上传CommonsMultipartFile则为数组即可 6 @RequestMapping("/upload") 7 public String fileUpload(@RequestParam("file") CommonsMultipartFile file , HttpServletRequest request) throws IOException { 8 9 //获取文件名 : file.getOriginalFilename(); 10 String uploadFileName = file.getOriginalFilename(); 11 12 //如果文件名为空,直接回到首页! 13 if ("".equals(uploadFileName)){ 14 return "redirect:/index.jsp"; 15 } 16 System.out.println("上传文件名 : "+uploadFileName); 17 18 //上传路径保存设置 19 String path = request.getServletContext().getRealPath("/upload"); 20 //如果路径不存在,创建一个 21 File realPath = new File(path); 22 if (!realPath.exists()){ 23 realPath.mkdir(); 24 } 25 System.out.println("上传文件保存地址:"+realPath); 26 27 InputStream is = file.getInputStream(); //文件输入流 28 OutputStream os = new FileOutputStream(new File(realPath,uploadFileName)); //文件输出流 29 30 //读取写出 31 int len=0; 32 byte[] buffer = new byte[1024]; 33 while ((len=is.read(buffer))!=-1){ 34 os.write(buffer,0,len); 35 os.flush(); 36 } 37 os.close(); 38 is.close(); 39 return "redirect:/index.jsp"; 40 } 41 42 /* 43 * 采用file.Transto 来保存上传的文件 44 */ 45 @RequestMapping("/upload2") 46 public String fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException { 47 48 //上传路径保存设置 49 String path = request.getServletContext().getRealPath("/upload"); 50 File realPath = new File(path); 51 if (!realPath.exists()){ 52 realPath.mkdir(); 53 } 54 //上传文件地址 55 System.out.println("上传文件保存地址:"+realPath); 56 57 //通过CommonsMultipartFile的方法直接写文件(注意这个时候) 58 file.transferTo(new File(realPath +"/"+ file.getOriginalFilename())); 59 60 return "redirect:/index.jsp"; 61 } 62 63 @RequestMapping(value="/download") 64 public String downloads(HttpServletResponse response , HttpServletRequest request) throws Exception{ 65 //要下载的图片地址 66 String path = request.getServletContext().getRealPath("/upload"); 67 String fileName = "logo.png"; 68 69 //1、设置response 响应头 70 response.reset(); //设置页面不缓存,清空buffer 71 response.setCharacterEncoding("UTF-8"); //字符编码 72 response.setContentType("multipart/form-data"); //二进制传输数据 73 //设置响应头 74 response.setHeader("Content-Disposition", 75 "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8")); 76 77 File file = new File(path,fileName); 78 //2、 读取文件--输入流 79 InputStream input=new FileInputStream(file); 80 //3、 写出文件--输出流 81 OutputStream out = response.getOutputStream(); 82 83 byte[] buff =new byte[1024]; 84 int index=0; 85 //4、执行 写出操作 86 while((index= input.read(buff))!= -1){ 87 out.write(buff, 0, index); 88 out.flush(); 89 } 90 out.close(); 91 input.close(); 92 return null; 93 } 94 95 96 }
标签:文件,String,realPath,File,file,new,上传,下载 来源: https://www.cnblogs.com/nuyan/p/15490246.html