java實現檔案上傳與下載
一、對於檔案上傳,瀏覽器在上傳的過程中是將檔案以流的形式提交到伺服器端的,Servlet獲取上傳檔案的輸入流然後再解析裡面的請求引數是比較麻煩。
JSP程式碼,POST請求,表單必須設定為enctype="multipart/form-data"
Servlet程式碼:<span style="font-size:14px;"><form action="upload3" method="post" enctype="multipart/form-data"> File to upload:<input type="file" name="upfile"><br><br> <input type="submit" value="Press"> to upload the file! </form></span>
<span style="font-size:14px;"><span style="white-space:pre"> </span>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); System.out.println("請求正文長度為--->" + request.getContentLength()); System.out.println("請求型別及表單域分割符--->" + request.getContentType()); //儲存檔案目錄 String savePath = "d:/temptemp"; File file = new File(savePath); //判斷上傳檔案目錄是否存在 if(!file.exists() && !file.isDirectory()){ //此目錄不存在 file.mkdir(); } String message = ""; try { InputStream is = request.getInputStream(); OutputStream os = new FileOutputStream(savePath + "/test.txt"); byte[] buffer = new byte[1024]; int len = 0; while((len=is.read(buffer))>0){ os.write(buffer,0,len); } is.close(); os.close(); message = "檔案上傳成功"; } catch (Exception e) { message = "檔案上傳失敗"; e.printStackTrace(); } request.setAttribute("message", message); request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request, response); }</span>
對於fireFox瀏覽器:
上傳表單為空時,request.getContentLength()為198
上傳檔案時,請求頭資訊為:
後臺輸出:
生成的上傳檔案:
注意:上傳的文字的字元編碼,否則filename或正文就會出現亂碼的可能。
對於chrome瀏覽器:
上傳表單為空時:request.getContentLength()為190
上傳檔案時,請求頭資訊為:
後臺輸出:
生成的檔案:
以上是採用servlet接收客戶端請求的方式來處理普通文字型別的檔案的。雖然可以通過分隔符以及其餘的一些固定的字串如filename,通過字串操作來獲取請求正文中需要的資料,但是操作過程也僅限於文字型別,畢竟對於資料流的解析是比較麻煩的。
二、採用Apache提供的用來處理檔案上傳的開源元件Commons-fileupload
檔案上傳時,enctype屬性必須設定為”multipart/data-form”。該屬性指定了提交表單時請求正文的MIME型別,預設值為application/x-www-form-urlencoded。
JSP程式碼:
<span style="font-size:14px;"><form action="upload2" method="post" enctype="multipart/form-data">
File to upload:<input type="file" name="upfile"><br><br>
Notes about the file:<input type="text" name="note"><br><br>
<input type="submit" value="Press"> to upload the file!
</form></span>
servlet程式碼:
<span style="font-size:14px;">public class FileUploadNew extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 得到上傳檔案的儲存目錄,將上傳的檔案存放於WEB-INF目錄下,不允許外界直接訪問,保證上傳檔案的安全
String savePath = this.getServletContext().getRealPath(
"/WEB-INF/upload");
// 上傳時生成的臨時檔案儲存目錄
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tempFile = new File(tempPath);
if (!tempFile.exists() && !tempFile.isDirectory()) {
// 建立臨時目錄
tempFile.mkdir();
}
String message = "";
try {
// 1、建立一個DiskFileItemFactory工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
// 設定工廠的緩衝區的大小,當上傳的檔案大小超過緩衝區的大小時,就會生成一個臨時檔案存放到指定的臨時目錄當中。
factory.setSizeThreshold(1024 * 100);// 設定緩衝區的大小為100KB,如果不指定,那麼緩衝區的大小預設是10KB
// 設定上傳時生成的臨時檔案的儲存目錄
factory.setRepository(tempFile);
// 2、建立一個檔案上傳解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 監聽檔案上傳進度
upload.setProgressListener(new ProgressListener() {
public void update(long pBytesRead, long pContentLength,
int arg2) {
System.out.println("檔案大小為:" + pContentLength + ",當前已處理:"
+ pBytesRead);
}
});
// 解決上傳檔名的中文亂碼
upload.setHeaderEncoding("UTF-8");
// 3、判斷提交上來的資料是否是上傳表單的資料
if (!ServletFileUpload.isMultipartContent(request)) {
// 按照傳統方式獲取資料
return;
}
// 設定上傳單個檔案的大小的最大值,目前是設定為1024*1024位元組,也就是1MB
upload.setFileSizeMax(1024 * 1024);
// 設定上傳檔案總量的最大值,最大值=同時上傳的多個檔案的大小的最大值的和,目前設定為10MB
upload.setSizeMax(1024 * 1024 * 10);
// 4、使用ServletFileUpload解析器解析上傳資料,解析結果返回的是一個List<FileItem>集合,每一個FileItem對應一個Form表單的輸入項
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
// 如果fileItem中封裝的是普通輸入項的資料
if (item.isFormField()) {
String name = item.getFieldName();
// 解決普通輸入項的資料的中文亂碼問題
String value = item.getString("UTF-8");
// value = new String(value.getBytes("iso8859-1"),"UTF-8");
System.out.println(name + "=" + value);
} else {// 如果fileItem中封裝的是上傳檔案
// 得到上傳的檔名稱,
String filename = item.getName();
System.out.println(filename);
if (filename == null || filename.trim().equals("")) {
continue;
}
// 注意:不同的瀏覽器提交的檔名是不一樣的,有些瀏覽器提交上來的檔名是帶有路徑的,如:
// c:\a\b\1.txt,而有些只是單純的檔名,如:1.txt
// 處理獲取到的上傳檔案的檔名的路徑部分,只保留檔名部分
filename = filename
.substring(filename.lastIndexOf("\\") + 1);
// 得到上傳檔案的副檔名
String fileExtName = filename.substring(filename
.lastIndexOf(".") + 1);
// 如果需要限制上傳的檔案型別,那麼可以通過檔案的副檔名來判斷上傳的檔案型別是否合法
System.out.println("上傳的檔案的副檔名是:" + fileExtName);
// 獲取item中的上傳檔案的輸入流
InputStream in = item.getInputStream();
// 得到檔案儲存的名稱
String saveFilename = makeFileName(filename);
// 得到檔案的儲存目錄
String realSavePath = makePath(saveFilename, savePath);
// 建立一個檔案輸出流
FileOutputStream out = new FileOutputStream(realSavePath
+ "\\" + saveFilename);
// 建立一個緩衝區
byte buffer[] = new byte[1024];
// 判斷輸入流中的資料是否已經讀完的標識
int len = 0;
// 迴圈將輸入流讀入到緩衝區當中,(len=in.read(buffer))>0就表示in裡面還有資料
while ((len = in.read(buffer)) > 0) {
// 使用FileOutputStream輸出流將緩衝區的資料寫入到指定的目錄(savePath + "\\"
// + filename)當中
out.write(buffer, 0, len);
}
// 關閉輸入流
in.close();
// 關閉輸出流
out.close();
// 刪除處理檔案上傳時生成的臨時檔案
// item.delete();
message = "檔案上傳成功!";
}
}
} catch (FileUploadBase.FileSizeLimitExceededException e) {
message = "單個檔案超出最大限制";
e.printStackTrace();
} catch (FileUploadBase.SizeLimitExceededException e) {
message = "上傳檔案超出數量限制";
e.printStackTrace();
} catch (Exception e) {
message = "檔案上傳失敗";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("/WEB-INF/message.jsp").forward(request,
response);
}
private String makeFileName(String filename) {
// 為防止檔案覆蓋的現象發生,要為上傳檔案產生一個唯一的檔名
return UUID.randomUUID().toString() + "_" + filename;
}
/**
*多目錄儲存
*/
private String makePath(String filename, String savePath) {
// 得到檔名的hashCode的值,得到的就是filename這個字串物件在記憶體中的地址
int hashcode = filename.hashCode();
int dir1 = hashcode & 0xf; // 0--15
int dir2 = (hashcode & 0xf0) >> 4; // 0-15
// 構造新的儲存目錄
String dir = savePath + "\\" + dir1 + "\\" + dir2; // upload\2\3
// upload\3\5
// File既可以代表檔案也可以代表目錄
File file = new File(dir);
// 如果目錄不存在
if (!file.exists()) {
// 建立目錄
file.mkdirs();
}
return dir;
}
}</span>
另:
1)DiskFileItemFactory
該工廠類用於建立FileItem的例項,對於較小的表單項,FileItem例項的內容儲存在記憶體中,對於大的表單項,FileItem例項的內容會儲存在硬碟中的一個臨時檔案中。大小的閾值和臨時檔案的路徑都可以配置。構造方法如下:
DiskFileItemFactory():檔案閾值大小為10KB,預設臨時檔案的路徑可以通過System.getProperty(“java.io.tmpdir”) 來獲得。
DiskFileItemFactory(int sizeThreshold, File repository)
注:處理檔案上傳時,自己用IO流處理,一定要在流關閉後刪除臨時檔案。
因此建議使用:FileItem.write(File file),會自動刪除臨時檔案。
2)亂碼問題
① 普通欄位的亂碼
解決辦法:FileItem.getString(String charset);編碼要和客戶端一致。
② 上傳的中文檔名亂碼
解決辦法:request.setCharacterEncoding(“UTF-8″);編碼要和客戶端一致。
3)檔案重名問題
當上傳的兩個檔案同名時,第二個檔案會覆蓋掉第一個檔案。
解決方法:a.txt –> UUID_a.txt,使得存入伺服器的檔名唯一。
String fileName = item.getName();
fileName = UUID.randomUUID().toString() + "_" + fileName;
4)資料夾中檔案過多問題
解決思路:分目錄儲存。下面提供兩種解決方案:
① 按照日期分目錄儲存:
//按日期分目錄儲存,程式中修改storePath,形如/files/2014/08/29
DateFormat df = new SimpleDateFormat("/yyyy/MM/dd");
String childDir = df.format(new Date());
//檔案存放路徑:位於專案根目錄下的files目錄,不存在則建立
String storePath = getServletContext().getRealPath("/files"+childDir);
② 按照檔名的hashCode計算儲存目錄(推薦)
//按檔名的hashCode分目錄儲存
String childDir = mkChildDir(fileName);
//檔案存放路徑:位於專案根目錄下的files目錄,不存在則建立
String storePath = getServletContext().getRealPath("/files"+childDir);
<span style="font-size:14px;"><span style="font-family:Microsoft YaHei;font-size:12px;">private String mkChildDir(String fileName) {
int hashCode = fileName.hashCode();
int dir1 = hashCode & 0xf;//取hashCode低4位
int dir2 = (hashCode & 0xf0) >> 4; //取hashCode的低5~8位
return "/" + dir1 + "/" + dir2;
}</span>
5)限制上傳檔案的大小和型別有時候需要對使用者上傳的檔案大小和型別作出限制,如上傳頭像、證件照時不會很大,且檔案MIME型別均為image/*,此時需要在伺服器端進行判斷。
① 限制大小
單檔案大小:ServletFileUpload.setFileSizeMax(2*1024*1024); //限制單檔案大小為2M
總檔案大小(多檔案上傳):ServletFileUpload.setSizeMax(6*1024*1024);
② 限制類型
一般判斷:根據副檔名進行判斷,缺點是如果更改了副檔名,如a.txt –> a.jpg,這種方法是無法檢測出來的。
稍微高階點的:根據檔案MIME型別進行判斷,缺點是隻對IE瀏覽器有效,對Chrome、FireFox無效,因為後者請求頭中檔案的MIME型別就是依據副檔名的。
6)伺服器安全問題
假設使用者知道你的上傳目錄,並上傳了一個含有Runtime.getRuntime().exec(“xxx”);指令碼的JSP檔案,就會嚴重威脅伺服器的安全。
解決方法:把存放檔案的目錄,放到WEB-INF下面。
三、檔案下載
1.列出所有檔案
java程式碼
<span style="font-size:14px;">/**
* 列出檔案目錄下的所有檔案
*/
public class ListAllFile extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//檔案目錄
String filePath = "d:/temp";
File file = new File(filePath);
//儲存要下載的檔案
Map<String,String> fileMap = new HashMap<String,String>();
//如果目錄存在,並且目錄是一個資料夾
if(file.exists() && file.isDirectory()){
//獲取所有檔案
this.getFiles(file,fileMap);
}else{
file.mkdir();
}
//將fileMap集合傳送到下載頁面
request.setAttribute("fileMap", fileMap);
request.getRequestDispatcher("/WEB-INF/download.jsp").forward(request,response);
}
/**
* 獲取目錄下的所有檔案
* @throws IOException
*/
public void getFiles(File file,Map<String,String> fileMap) throws IOException{
//目錄為資料夾
if(file.isDirectory()){
File[] files = file.listFiles();
//遍歷陣列
for(File f : files){
//遞迴
getFiles(f,fileMap);
}
}else{//檔案
//file.getAbsolutePath()獲取檔案絕對路徑,唯一值,如:d:\temp\aaa.txt,需要轉為d:/temp/aaa.txt
//file.getName()獲得檔名,如:aaa.txt
fileMap.put(file.getAbsolutePath().replace("\\","/"),file.getName());
}
}
}
</span>
JSP程式碼
<span style="font-size:14px;"><%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>download</title>
<script>
function myDownload(Obj,file){
uri = encodeURI(file);
Obj.href = "fileDownload?fileName="+encodeURI(uri); //兩次編碼
Obj.click();
}
</script>
</head>
<body>
<!-- 遍歷Map集合 -->
<c:forEach items="${fileMap }" var="file">
${file.value } <a href="javascript:void(0)" onclick="myDownload(this,'${file.key }')">download</a>
<br>
</c:forEach>
----------檔案下載,中文亂碼 -----------<br>
<c:forEach items="${fileMap }" var="file">
<c:url value="fileDownload" var="downloadUrl">
<c:param name="fileName" value="${file.key }"></c:param>
</c:url>
${file.value } <a href="${downloadUrl }">download</a><!-- 中文亂碼 -->
<br>
</c:forEach>
${message}
</body>
</html><</span>
2.下載檔案
java程式碼
<span style="font-size:14px;">protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//獲取下載檔案,絕對路徑
String fileName = request.getParameter("fileName");
//解碼
fileName = URLDecoder.decode(fileName, "UTF-8");
//檔名
String realName = fileName.substring(fileName.lastIndexOf("/")+1);
System.out.println(fileName+"--->"+realName);
File file = new File(fileName);
if(!file.exists()){
request.setAttribute("message", "資源已刪除");
request.getRequestDispatcher("/WEB-INF/download.jsp").forward(request, response);
return;
}
//設定響應頭,控制瀏覽器下載該檔案
realName = new String(realName.getBytes("UTF-8"),"ISO-8859-1");
response.setHeader("content-disposition", "attachment;filename=" + realName);
//response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realName, "UTF-8")); //編碼
//讀取要下載的檔案,儲存到檔案輸入流
FileInputStream in = new FileInputStream(fileName);
//輸出流
OutputStream out = response.getOutputStream();
//緩衝區
byte buffer[] = new byte[4096];
int len = 0;
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
out.close();
}</span>