图片上传下载
1 package com.ddz.util; 2 3 4 import java.io.BufferedInputStream; 5 import java.io.BufferedOutputStream; 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.FileNotFoundException; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 import java.net.URLEncoder; 13 import java.util.UUID; 14 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 18 import org.springframework.web.multipart.MultipartFile; 19 20 public class FileUpDownLoad { 21 22 public static String uploadImg(MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{ 23 String path=""; 24 String fileName=""; 25 if(file.getSize()>0){ 26 path=request.getSession().getServletContext().getRealPath("")+"/upload/"; 27 fileName=UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); 28 File file2 = new File(path+fileName); 29 if(!file2.exists()){ 30 file2.mkdirs(); 31 } 32 file.transferTo(file2); 33 } 34 return "upload/"+fileName; 35 } 36 37 public static String download(String filepath,HttpServletRequest request, HttpServletResponse response) throws Exception{ 38 39 BufferedInputStream bis = null; 40 BufferedOutputStream bos = null; 41 OutputStream fos = null; 42 InputStream fis = null; 43 44 try { 45 // 如果是从服务器上取就用这个获得系统的绝对路径方法。 46 //String filepath = request.getRealPath(filepatha);//方法过时了 47 String filepathall = request.getSession().getServletContext().getRealPath(filepath); 48 49 File uploadFile = new File(filepathall); 50 51 //图片对象流 52 fis = new FileInputStream(uploadFile); 53 bis = new BufferedInputStream(fis); 54 fos = response.getOutputStream(); 55 bos = new BufferedOutputStream(fos); 56 57 //得到文件名 58 String filename = filepath.substring(filepath.lastIndexOf("\\")+1); 59 60 // 这个就就是弹出下载对话框的关键代码 61 response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename, "utf-8")); 62 63 int bytesRead = 0; 64 // 用输出流去写,缓冲输入输出流 65 byte[] buffer = new byte[8192]; 66 while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) { 67 bos.write(buffer, 0, bytesRead); 68 } 69 70 } catch (FileNotFoundException e) { 71 e.printStackTrace(); 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } catch (NumberFormatException e) { 75 e.printStackTrace(); 76 } finally { 77 try { 78 if (bos != null) { 79 bos.flush(); 80 } 81 if (fis != null) { 82 fis.close(); 83 } 84 if (bis != null) { 85 bis.close(); 86 } 87 if (fos != null) { 88 fos.close(); 89 } 90 if (bos != null) { 91 bos.close(); 92 } 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 } 97 return null; 98 } 99 }
在springMvc.xml中配置:
1 <!--上传下载 --> 2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 3 <!-- 指定上传的字符集 --> 4 <property name="defaultEncoding" value="utf-8"></property> 5 <!-- 最大上传文件大小 --> 6 <property name="maxUploadSize" value="10485760000"></property> 7 <!-- 最大占用内存空间大小 --> 8 <property name="maxInMemorySize" value="40960"></property> 9 </bean>