WEB項目:
后台代碼:
1 package com.achong.controller; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.io.OutputStream; 11 import java.util.ArrayList; 12 import java.util.List; 13 import java.util.UUID; 14 15 import javax.servlet.ServletContext; 16 import javax.servlet.ServletException; 17 import javax.servlet.http.HttpServletRequest; 18 import javax.servlet.http.HttpServletResponse; 19 import javax.servlet.http.HttpSession; 20 21 import org.apache.tools.zip.ZipEntry; 22 import org.apache.tools.zip.ZipOutputStream; 23 import org.springframework.http.HttpHeaders; 24 import org.springframework.http.HttpStatus; 25 import org.springframework.http.ResponseEntity; 26 import org.springframework.stereotype.Controller; 27 import org.springframework.web.bind.annotation.RequestMapping; 28 import org.springframework.web.bind.annotation.RequestParam; 29 import org.springframework.web.multipart.MultipartFile; 30 31 @Controller 32 public class HelloHandler { 33 34 @RequestMapping("/hello") 35 public String hello(){ 36 return "success"; 37 } 38 39 /** 40 * javaWeb: 41 * 1、commons-fileupload.jar commons-io.jar 42 * 2、解析當前請求 List<FileItem> items = servletFileUpload.parse(request); 43 * 3、遍歷每一個items,文件項,普通項 44 * 4、如果是文件項,寫流的上傳方法 45 * 46 * SpringMVC: 47 * 1、文件上傳也是使用commons-fileupload.jar 48 * 2、配置好文件上傳解析器; 49 * 3、 50 * 51 * @return 52 */ 53 @RequestMapping("/upload") 54 public String upload(@RequestParam("fileName")String fileName, 55 @RequestParam("photo")MultipartFile[] file){ 56 //保存 57 System.out.println("普通項的值:"+fileName); 58 for (MultipartFile multipartFile : file) { 59 if(multipartFile!=null&&!multipartFile.isEmpty()){ 60 File file2 = new File("C:/Users/Administrator/Desktop/上傳下載測試/"+multipartFile.getOriginalFilename()); 61 try { 62 multipartFile.transferTo(file2); 63 } catch (IllegalStateException | IOException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 } 68 } 69 return "success"; 70 } 71 72 @RequestMapping("/singleDownload") 73 public ResponseEntity<byte[]> downloadImg(HttpSession session) throws IOException{ 74 //=============造響應體============= 75 //1、創建一個ResponseEntity對象。這個對象里面既有響應頭還有響應體; 76 ServletContext servletContext = session.getServletContext(); 77 //1、獲取到圖片的流,直接交給瀏覽器;ServletContext.可以從當前項目下獲取資源 78 //2、獲取到圖片的流 79 InputStream is = servletContext.getResourceAsStream("/Desert.jpg"); 80 //創建一個和流一樣多的數組 81 byte[] body = new byte[is.available()]; 82 //3、將流的數據放在數組里面 83 is.read(body); 84 is.close(); 85 86 //==============造響應頭================ 87 HttpHeaders headers = new HttpHeaders(); 88 //文件下載的響應頭 89 //按照以前亂碼的解決方式; 90 91 //文件名亂碼解決 92 String filename="單個圖片.jpg"; 93 filename = new String(filename.getBytes("GBK"),"ISO8859-1"); 94 headers.add("Content-Disposition", "attachment; filename="+filename); 95 //第一個參數代表給瀏覽器的響應數據(響應體) 96 //第二個參數代表當前響應的響應頭(定制響應頭)MultiValueMap 97 //第三個參數代表當前響應狀態碼(statusCode)HttpStatus 98 ResponseEntity<byte[]> re = new ResponseEntity<byte[]>(body, headers, HttpStatus.OK); 99 100 return re; 101 } 102 103 /** 104 * 批量打包下載文件生成zip文件下載 105 * 106 */ 107 @RequestMapping("/multDownloadZip") 108 public void downloadFiles(HttpServletRequest request, HttpServletResponse response) 109 throws ServletException, IOException { 110 //獲取頁面上需要批量下載的鏈接 111 List<File> files = new ArrayList<File>(); 112 String outFilePath = request.getSession().getServletContext().getRealPath("/") 113 + "upload"; 114 System.out.println(outFilePath); 115 File Allfile = new File(outFilePath); 116 System.out.println("Allfile: " + Allfile.getPath()); 117 if (Allfile.exists()) { 118 //得到項目跟路徑upload下所有的文件和目錄的絕對路徑 119 File[] fileArr = Allfile.listFiles(); 120 for (File file2 : fileArr) { 121 files.add(file2); 122 } 123 } 124 125 String fileName = UUID.randomUUID().toString() + ".zip"; 126 // 在服務器端創建打包下載的臨時文件 127 System.out.println("outFilePath: " + outFilePath); 128 createFile(outFilePath, fileName); 129 File file = new File(outFilePath + "\\" + fileName); 130 // 文件輸出流 131 FileOutputStream outStream = new FileOutputStream(file); 132 /* 133 * 壓縮流 134 * 需要導包:import org.apache.tools.zip.ZipOutputStream; 135 */ 136 ZipOutputStream toClient = new ZipOutputStream(outStream); 137 toClient.setEncoding("gbk"); 138 zipFile(files, toClient); 139 toClient.close(); 140 outStream.close(); 141 this.downloadFile(file, response, true); 142 } 143 144 // 創建文件 145 public void createFile(String path, String fileName) { 146 // path表示你所創建文件的路徑, fileName為文件名 147 File file = new File(path, fileName); 148 System.out.println(file.getPath()); 149 if (!file.exists()) { 150 try { 151 file.createNewFile(); 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } 155 } 156 157 } 158 159 /** 160 * 壓縮文件列表中的文件 161 * 162 * @param files 163 * @param outputStream 164 * @throws IOException 165 */ 166 public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException { 167 try { 168 int size = files.size(); 169 // 壓縮列表中的文件 170 for (int i = 0; i < size; i++) { 171 File file = (File) files.get(i); 172 zipFile(file, outputStream); 173 } 174 } catch (IOException e) { 175 throw e; 176 } 177 } 178 179 /** 180 * 將文件寫入到zip文件中 181 * 182 * @param inputFile 183 * @param outputstream 184 * @throws Exception 185 */ 186 public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException { 187 try { 188 if (inputFile.exists()) { 189 if (inputFile.isFile()) { 190 FileInputStream inStream = new FileInputStream(inputFile); 191 BufferedInputStream bInStream = new BufferedInputStream(inStream); 192 /* 193 * 需要導包:import org.apache.tools.zip.ZipEntry; 194 */ 195 ZipEntry entry = new ZipEntry(inputFile.getName()); 196 outputstream.putNextEntry(entry); 197 198 final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流為10M 199 long streamTotal = 0; // 接受流的容量 200 int streamNum = 0; // 流需要分開的數量 201 int leaveByte = 0; // 文件剩下的字符數 202 byte[] inOutbyte; // byte數組接受文件的數據 203 204 streamTotal = bInStream.available(); // 通過available方法取得流的最大字符數 205 streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分開的數量 206 leaveByte = (int) streamTotal % MAX_BYTE; // 分開文件之后,剩余的數量 207 208 if (streamNum > 0) { 209 for (int j = 0; j < streamNum; ++j) { 210 inOutbyte = new byte[MAX_BYTE]; 211 // 讀入流,保存在byte數組 212 bInStream.read(inOutbyte, 0, MAX_BYTE); 213 outputstream.write(inOutbyte, 0, MAX_BYTE); // 寫出流 214 } 215 } 216 // 寫出剩下的流數據 217 inOutbyte = new byte[leaveByte]; 218 bInStream.read(inOutbyte, 0, leaveByte); 219 outputstream.write(inOutbyte); 220 outputstream.closeEntry(); // Closes the current ZIP entry 221 // and positions the stream for 222 // writing the next entry 223 bInStream.close(); // 關閉 224 inStream.close(); 225 } 226 } else { 227 throw new ServletException("文件不存在!"); 228 } 229 } catch (IOException e) { 230 throw e; 231 } 232 } 233 234 /** 235 * 下載文件 236 * 237 * @param file 238 * @param response 239 */ 240 public void downloadFile(File file, HttpServletResponse response, boolean isDelete) { 241 try { 242 // 以流的形式下載文件。 243 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); 244 byte[] buffer = new byte[fis.available()]; 245 fis.read(buffer); 246 fis.close(); 247 // 清空response 248 response.reset(); 249 OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); 250 response.setContentType("application/octet-stream"); 251 response.setHeader("Content-Disposition", 252 "attachment;filename=" + new String(file.getName().getBytes("UTF-8"), "ISO-8859-1")); 253 toClient.write(buffer); 254 toClient.flush(); 255 toClient.close(); 256 if (isDelete) { 257 file.delete(); // 是否將生成的服務器端文件刪除 258 } 259 } catch (IOException ex) { 260 ex.printStackTrace(); 261 } 262 } 263 }
index頁面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 <% 9 pageContext.setAttribute("ctx", request.getContextPath()); 10 %> 11 </head> 12 <body> 13 <a href="hello">Hello</a><br/> 14 <!--相對路徑容易出問題,我們都推薦寫絕對路徑。 15 base:為所有相對路徑指定新的標准; --> 16 <hr/> 17 <!-- 18 1、上傳文件的表單enctype="multipart/form-data" 19 --> 20 <form action="${ctx }/upload" method="post" enctype="multipart/form-data"> 21 <input type="file" name="photo"/> 22 <input type="file" name="photo"/> 23 <input type="file" name="photo"/> 24 <input type="file" name="photo"/> 25 描述:<input type="text" name="fileName"/> 26 <input type="submit" value="上傳"/> 27 </form> 28 <a href="${ctx }/singleDownload">下載單個</a><br/> 29 <a href="${ctx }/multDownloadZip">批量下載</a> 30 </body> 31 </html>
success頁面:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>成功!</h1> 11 </body> 12 </html>
WebContent目錄結構: