上傳和下載(批量圖片)
檔案上傳頁面和訊息提示頁面
<title>檔案上傳</title>
<body>
<form action="${pageContext.request.contextPath}/servlet/UploadFileBetter" enctype="multipart/form-data" method="post">
上傳使用者:<input type="text" name="username"><br/>
上傳檔案
上傳檔案2:<input type="file" name="file2"><br/>
<input type="submit" value="提交">
</form>
message.jsp的程式碼如下
<body>
${message}<!--訊息提示-->
</body>
--------------------------------------------------------------------------------------------------------------------------------------------後臺程式碼
package file;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
/**
* Created by OnlyOne on 2016/3/3.
*/
public class UploadFile extends HttpServlet {
/**
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*//防止中文亂碼,與頁面字符集一致
req.setCharacterEncoding("UTF-8");*/
//得到上傳檔案的儲存目錄,將上傳的檔案存放於WEB-INF目錄下,不允許外界直接訪問,保證上傳檔案的安全
String savePath = req.getServletContext().getRealPath("/WEB-INF/upload");
// this.getServletContext().getRealPath("/WEB-INF/upload");
//建立儲存目錄的檔案
File saveFile = new File(savePath);
//判斷儲存目錄檔案是否存在,不存在則建立一個資料夾
if(!saveFile.exists()){
System.out.println("檔案目錄建立中。。。");
saveFile.mkdir();
}
//訊息提示
String message = "";
//使用Apache檔案上傳元件處理檔案上傳步驟:
try {
//1、建立一個DiskFileItemFactory工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.建立一個檔案上傳解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//解決上傳檔名的中文亂碼
upload.setHeaderEncoding("UTF-8");
//3.判斷提交上來的資料是否是上傳表單的資料
if(!ServletFileUpload.isMultipartContent(req)){
//按照傳統方式獲取資料
return;
}
//4.使用ServletFileUpload解析器解析上傳資料,解析結果返回的是一個List<fileItem>集合,
// 每一個FileItem對應一個Form表單的輸入項
List<FileItem> list = upload.parseRequest(req);
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*/
//處理獲取到的上傳檔案的檔名的路徑部分,只保留檔名部分.如果傳上來的檔名沒有帶路徑,則lastIndexOf返回-1
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
//獲取item中的上傳輸入流
BufferedInputStream bis = new BufferedInputStream(item.getInputStream());
//建立一個檔案輸出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(savePath + "\\" + fileName));
//建立一個緩衝區
byte[] buffer = new byte[1024*8];
//迴圈將緩衝輸入流讀入到緩衝區當中
while(true){
//迴圈將緩衝輸入流讀入到緩衝區當中
int length = bis.read(buffer);
//判斷是否讀取到檔案末尾
if(length == -1){
break;
}
//使用BufferedOutputStream緩衝輸出流將緩衝區的資料寫入到指定的目錄(savePath + "\\" + filename)當中
bos.write(buffer,0,length);
}
//關閉輸入流
bis.close();
//關閉輸出流
bos.close();
//刪除處理檔案上傳時生成的臨時檔案
item.delete();
message = "檔案上傳成功!";
}
}
} catch (FileUploadException e) {
message= "檔案上傳失敗!";
}
req.setAttribute("message",message);
req.getRequestDispatcher("/message.jsp").forward(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Web.xml檔案中註冊UploadFile
<servlet>
<servlet-name>UploadFile</servlet-name>
<servlet-class>file.UploadFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile</servlet-name>
<url-pattern>/servlet/UploadFile</url-pattern>
</servlet-mapping>
檔案上傳成功之後,上傳的檔案儲存在了WEB-INF目錄下的upload目錄,如下圖所示:
2.3、檔案上傳的細節
上述的程式碼雖然可以成功將檔案上傳到伺服器上面的指定目錄當中,但是檔案上傳功能有許多需要注意的小細節問題,以下列出的幾點需要特別注意的
1、為保證伺服器安全,上傳檔案應該放在外界無法直接訪問的目錄下,比如放於WEB-INF目錄下。
2、為防止檔案覆蓋的現象發生,要為上傳檔案產生一個唯一的檔名。
3、為防止一個目錄下面出現太多檔案,要使用hash演算法打散儲存。
4、要限制上傳檔案的最大值。
5、要限制上傳檔案的型別,在收到上傳檔名時,判斷後綴名是否合法。
針對上述提出的5點細節問題,我們來改進一下UploadFile_better,改進後的程式碼如下:
package file;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
/**
* Created by OnlyOne on 2016/3/3.
*/
public class UploadFile_better extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*//防止中文亂碼,與頁面字符集一致
req.setCharacterEncoding("UTF-8");*/
//得到上傳檔案的儲存目錄,將上傳的檔案存放於WEB-INF目錄下,不允許外界直接訪問,保證上傳檔案的安全
String savePath = req.getServletContext().getRealPath("/WEB-INF/upload");
// this.getServletContext().getRealPath("/WEB-INF/upload");
//上傳時生成的臨時檔案儲存目錄
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
//建立儲存目錄的檔案
File tempFile = new File(tempPath);
//判斷儲存目錄檔案是否存在,不存在則建立一個資料夾
if(!tempFile.exists()){
System.out.println("檔案目錄建立中。。。");
tempFile.mkdir();
}
//訊息提示
String message = "";
//使用Apache檔案上傳元件處理檔案上傳步驟:
try {
//1、建立一個DiskFileItemFactory工廠
DiskFileItemFactory factory = new DiskFileItemFactory();
//設定工廠的緩衝區的大小,當上傳的檔案大小超過緩衝區的大小時,就會生成一個臨時檔案存放到指定的臨時目錄當中。
//設定緩衝區的大小為100KB,如果不指定,那麼緩衝區的大小預設是10KB
factory.setSizeThreshold(1024 * 100);
//2.建立一個檔案上傳解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//監聽檔案上傳進度
upload.setProgressListener(new ProgressListener() {
@Override
public void update(long pBytesRead, long pContentLength, int arg2) {
System.out.println("檔案大小為:" + pContentLength + ",當前已處理:" + pBytesRead);
}
});
//解決上傳檔名的中文亂碼
upload.setHeaderEncoding("UTF-8");
//3.判斷提交上來的資料是否是上傳表單的資料
if(!ServletFileUpload.isMultipartContent(req)){
//按照傳統方式獲取資料
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(req);
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*/
//處理獲取到的上傳檔案的檔名的路徑部分,只保留檔名部分.如果傳上來的檔名沒有帶路徑,則lastIndexOf返回-1
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
//得到上傳檔案的副檔名
String fileExtName = fileName.substring(fileName.lastIndexOf(".")+1);
//如果需要限制上傳的檔案型別,那麼可以通過檔案的副檔名來判斷上傳的檔案型別是否合法
System.out.println("上傳的檔案的副檔名是:"+fileExtName);
//獲取item中的上傳輸入流
BufferedInputStream bis = new BufferedInputStream(item.getInputStream());
//得到檔案儲存的名稱
String saveFilename = makeFileName(fileName);
//得到檔案的儲存目錄
String realSavePath = makePath(saveFilename, savePath);
//建立一個檔案輸出流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(realSavePath + "\\" + saveFilename));
//建立一個緩衝區
byte[] buffer = new byte[1024*8];
//迴圈將緩衝輸入流讀入到緩衝區當中
while(true){
//迴圈將緩衝輸入流讀入到緩衝區當中
int length = bis.read(buffer);
//判斷是否讀取到檔案末尾
if(length == -1){
break;
}
//使用BufferedOutputStream緩衝輸出流將緩衝區的資料寫入到指定的目錄(savePath + "\\" + filename)當中
bos.write(buffer,0,length);
}
//關閉輸入流
bis.close();
//關閉輸出流
bos.close();
//刪除處理檔案上傳時生成的臨時檔案
item.delete();
message = "檔案上傳成功!";
}
}
} catch (FileUploadBase.FileSizeLimitExceededException e) {
message = "單個檔案超出最大值!!!";
e.printStackTrace();
System.out.println("單個檔案超出最大值!!!");
}catch (FileUploadBase.SizeLimitExceededException e) {
message = "上傳檔案的總的大小超出限制的最大值!!!";
e.printStackTrace();
System.out.println("上傳檔案的總的大小超出限制的最大值!!!");
}catch (Exception e) {
message= "檔案上傳失敗!";
e.printStackTrace();
}
System.out.println("執行跳轉");
req.setAttribute("message",message);
req.getRequestDispatcher("/message.jsp").forward(req,resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
/**
* @Method: makeFileName
* @Description: 生成上傳檔案的檔名,檔名以:uuid+"_"+檔案的原始名稱
* @Anthor:OnlyOne
* @param filename 檔案的原始名稱
* @return uuid+"_"+檔案的原始名稱
*/
private String makeFileName(String filename){ //2.jpg
//為防止檔案覆蓋的現象發生,要為上傳檔案產生一個唯一的檔名
return OrderIdBuilder.createOrderId().toString() + "_" + filename;
}
/**
* 為防止一個目錄下面出現太多檔案,要使用hash演算法打散儲存
* @Method: makePath
* @Anthor:OnlyOne
* @param filename 檔名,要根據檔名生成儲存目錄
* @param savePath 檔案儲存路徑
* @return 新的儲存目錄
*/
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;
}
}
三、檔案下載
我們要將Web應用系統中的檔案資源提供給使用者進行下載,首先我們要有一個頁面列出上傳檔案目錄下的所有檔案,當用戶點選檔案下載超連結時就進行下載操作,編寫一個ListFileServlet,用於列出Web應用系統中所有下載檔案。
ListFileServlet的程式碼如下:
package file;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by OnlyOne on 2016/3/4.
* @ClassName: ListFileServlet
* @Description: 列出Web系統中所有下載檔案
*
*/
public class ListFileServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取上傳檔案的目錄
String uploadFilePath = req.getServletContext().getRealPath("/WEB-INF/upload");
//儲存要下載的檔名
Map<String, String> fileNameMap = new HashMap<String, String>();
//地鬼遍歷filePath目錄下的所有檔案和目錄,將檔案的檔名儲存到map集合中
listfile(new File(uploadFilePath),fileNameMap);
req.setAttribute("fileNameMap", fileNameMap);
req.getRequestDispatcher("/listFile.jsp").forward(req, resp);
}
/**
* Created by OnlyOne on 2016/3/4.
* @Method: listfile
* @Description: 遞迴遍歷指定目錄下的所有檔案
* @param file 即代表一個檔案,也代表一個檔案目錄
* @param map 儲存檔名的Map集合
*/
private void listfile(File file, Map<String,String> map){
//如果file代表的不是一個檔案,而是一個目錄
if(!file.isFile()){
//列出該目錄下的所有檔案和目錄
File files[] = file.listFiles();
//遍歷files[]陣列
for(File f : files){
//遞迴
listfile(f,map);
}
}else{
/**
* 處理檔名,上傳後的檔案是以uuid_檔名的形式去重新命名的,去除檔名的uuid_部分
file.getName().indexOf("_")檢索字串中第一次出現"_"字元的位置,如果檔名類似於:9349249849-88343-8344_阿_凡_達.avi
那麼file.getName().substring(file.getName().indexOf("_")+1)處理之後就可以得到阿_凡_達.avi部分
*/
String realName = file.getName().substring(file.getName().indexOf("_")+1);
//file.getName()得到的是檔案的原始名稱,這個名稱是唯一的,因此可以作為key,realName是處理過後的名稱,有可能會重複
map.put(file.getName(), realName);
}
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
這裡簡單說一下ListFileServlet中listfile方法,listfile方法是用來列出目錄下的所有檔案的,listfile 方法內部用到了遞迴,在實際開發當中,我們肯定會在資料庫建立一張表,裡面會儲存上傳的檔名以及檔案的具體存放目錄,我們通過查詢表就可以知道檔案的具 體存放目錄,是不需要用到遞迴操作的,這個例子是因為沒有使用資料庫儲存上傳的檔名和檔案的具體存放位置,而上傳檔案的存放位置又使用了雜湊演算法打散存 放,所以需要用到遞迴,在遞迴時,將獲取到的檔名存放到從外面傳遞到listfile方法裡面的Map集合當中,這樣就可以保證所有的檔案都存放在同一 個Map集合當中。
在Web.xml檔案中配置ListFileServlet
<servlet>
<servlet-name>ListFileServlet</servlet-name>
<servlet-class>file.ListFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ListFileServlet</servlet-name>
<url-pattern>/servlet/ListFileServlet</url-pattern>
</servlet-mapping>
展示下載檔案的listfile.jsp頁面如下:
<html>
<head>
<title>下載檔案顯示頁面</title>
</head>
<body>
<!-- 遍歷Map集合 -->
<c:forEach var="me" items="${fileNameMap}">
<c:url value="/servlet/DownLoadFile" var="downurl">
<c:param name="fileName" value="${me.key}"></c:param>
</c:url>
${me.value}<a href="${downurl}">下載</a>
<br/>
</c:forEach>
</body>
</html>
訪問ListFileServlet,就可以在listfile.jsp頁面中顯示提供給使用者下載的檔案資源,如下圖所示:
3.2、實現檔案下載
編寫一個用於處理檔案下載的Servlet,DownLoadFile的程式碼如下:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
/**
* Created by OnlyOne on 2016/3/4.
*/
public class DownLoadFile extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//得到要下載的檔名
req.setCharacterEncoding("UTF-8");
String fileName = req.getParameter("fileName");
// fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
//上傳的檔案都是儲存在/WEN-INF/upload目錄黨徽宗
String fileSaveRootPath = req.getServletContext().getRealPath("/WEB-INF/upload");
//銅鼓檔名找出檔案的所在目錄
String path = findFileSavePathByFileName(fileName, fileSaveRootPath);
//得到要下載的檔案
File file = new File(path + "//" + fileName);
//如果檔案不存在
if(!file.exists()){
req.setAttribute("message", "資源已被刪除!");
req.getRequestDispatcher("/message.jsp").forward(req, resp);
}
//處理檔名
String realName = fileName.substring(fileName.indexOf("_")+1);
//設定響應頭,控制瀏覽器下載該檔案
resp.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(realName, "UTF-8"));
//讀取要下載的檔案,儲存到檔案輸入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path + "\\" + fileName));
//建立輸出流
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
//建立一個緩衝區
byte[] buffer = new byte[1024*8];
//迴圈將緩衝輸入流讀入到緩衝區當中
while(true){
//迴圈將緩衝輸入流讀入到緩衝區當中
int length = bis.read(buffer);
//判斷是否讀取到檔案末尾
if(length == -1){
break;
}
//使用BufferedOutputStream緩衝輸出流將緩衝區的資料寫入到指定的目錄(savePath + "\\" + filename)當中
bos.write(buffer,0,length);
}
//關閉檔案輸入流
bis.close();
//重新整理此輸入流並強制寫出所有緩衝的輸出位元組數
bos.flush();
//關閉檔案輸出流
bos.close();
}
/**
* Created by OnlyOne on 2016/3/4.
* @Method: findFileSavePathByFileName
* @Description: 通過檔名和儲存上傳檔案根目錄找出要下載的檔案的所在路徑
* @param filename 要下載的檔名
* @param saveRootPath 上傳檔案儲存的根目錄,也就是/WEB-INF/upload目錄
* @return 要下載的檔案的儲存目錄
*/
private String findFileSavePathByFileName(String filename,String saveRootPath){
int hashcode = filename.hashCode();
int dir1 = hashcode&0xf; //0--15
int dir2 = (hashcode&0xf0)>>4; //0-15
String dir = saveRootPath + "\\" + dir1 + "\\" + dir2; //upload\2\3 upload\3\5
File file = new File(dir);
if(!file.exists()){
//建立目錄
file.mkdirs();
}
return dir;
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
點選【下載】超連結,將請求提交到DownLoadServlet就行處理就可以實現檔案下載了,執行效果如下圖所示: