資源簡介
JavaWeb實現文件上傳下載,使用Comm組件,簡單實現上傳、下載、展示。

代碼片段和文件信息
package?org.gyt.fileUploadDownload;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.IOException;
import?java.io.OutputStream;
import?java.net.URLEncoder;
import?javax.servlet.ServletException;
import?javax.servlet.annotation.WebServlet;
import?javax.servlet.http.HttpServlet;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
/**
?*?Servlet?implementation?class?DownloadServlet
?*/
@WebServlet(“/DownloadServlet“)
public?class?DownloadServlet?extends?HttpServlet?{
private?static?final?long?serialVersionUID?=?1L;
????private?static?String?filePath?=?“404“;
????/**
?????*?@see?HttpServlet#HttpServlet()
?????*/
????public?DownloadServlet()?{
????????super();
????????//?TODO?Auto-generated?constructor?stub
????}
/**
?*?@see?HttpServlet#doGet(HttpServletRequest?request?HttpServletResponse?response)
?*/
protected?void?doGet(HttpServletRequest?request?HttpServletResponse?response)?throws?ServletException?IOException?{
//?TODO?Auto-generated?method?stub
// response.getWriter().append(“Served?at:?“).append(request.getContextPath());
//?下載文件
System.out.println(“下載文件:“);
//?文件名
String?fileName?=?request.getParameter(“filename“);?//文件名_UUID
fileName?=?new?String(fileName.getBytes(“iso8859-1“)?“UTF-8“);
//?文件保存在/WEB-INF/file中
String?fileStorageRootPath?=?this.getServletContext().getRealPath(“/WEB-INF/file“);
//?通過文件名找出文件所在目錄?返回絕對路徑
findFileStoragePathByFileName(fileName?new?File(fileStorageRootPath));
System.out.println(“Download?File:?“?+?filePath);
//?得到該文件
if?(filePath.equals(“404“))?{
request.setAttribute(“message“?“錯誤“);
request.getRequestDispatcher(“/message.jsp“).forward(request?response);
}
File?file?=?new?File(filePath);
//?若不存在
if?(file?!=?null?&&?!file.exists())?{
request.setAttribute(“message“?“資源已被刪除!“);
request.getRequestDispatcher(“/message.jsp“).forward(request?response);
return?;
}
//?處理文件名
String?realName?=?fileName.substring(fileName.indexOf(“_“)+1);
//?設置相應頭,控制瀏覽器下載文件
response.setHeader(“content-disposition“?“attachment;filename=“?+?URLEncoder.encode(realName?“UTF-8“));
//?讀取要下載的文件,保存到輸入流
FileInputStream?inputStream?=?new?FileInputStream(filePath);
//?創建輸出流
OutputStream?outputStream?=?response.getOutputStream();
//?創建緩沖區
byte[]?buffer?=?new?byte[1024];
int?len?=?0;
while?((len=inputStream.read(buffer))?>?0)?{
//?輸出緩沖區內容到瀏覽器,實現下載
outputStream.write(buffer?0?len);
}
//?關閉流
inputStream.close();
outputStream.close();
}
/**
?*?
?*?@throws?IOException?
?*?@title:findFileStoragePathByFileName
?*?@Description:?根據文件名找到所在路徑?返回絕對路徑
?*?@Param:?@param?fileName
?*?@Param:?@param?fileStorageRootPath
?*?@Param:?@return?返回絕對路徑
?*?@Return:?String
?*/
private?void?findFileStoragePathByFileName(String?fileName?File?file)?throws?IOException?{
//?TODO?Auto-generated?method?stub
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1418??2018-12-04?23:16??FileUploadDownload\.classpath
?????文件????????918??2018-12-04?23:14??FileUploadDownload\.project
?????文件????????567??2018-12-04?23:14??FileUploadDownload\.settings\.jsdtscope
?????文件????????364??2018-12-04?23:14??FileUploadDownload\.settings\org.eclipse.jdt.core.prefs
?????文件????????506??2018-12-04?23:14??FileUploadDownload\.settings\org.eclipse.wst.common.component
?????文件????????345??2018-12-04?23:14??FileUploadDownload\.settings\org.eclipse.wst.common.project.facet.core.xm
?????文件?????????49??2018-12-04?23:14??FileUploadDownload\.settings\org.eclipse.wst.jsdt.ui.superType.container
?????文件??????????6??2018-12-04?23:14??FileUploadDownload\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????文件???????4035??2018-12-04?23:16??FileUploadDownload\build\classes\org\gyt\fileUploadDownload\DownloadServlet.class
?????文件???????3026??2018-12-04?23:22??FileUploadDownload\build\classes\org\gyt\fileUploadDownload\ListFileServlet.class
?????文件???????1252??2018-12-04?23:24??FileUploadDownload\build\classes\org\gyt\fileUploadDownload\UploadServlet$1.class
?????文件???????7083??2018-12-04?23:24??FileUploadDownload\build\classes\org\gyt\fileUploadDownload\UploadServlet.class
?????文件???????4352??2018-12-04?22:08??FileUploadDownload\src\org\gyt\fileUploadDownload\DownloadServlet.java
?????文件???????2994??2018-12-04?23:22??FileUploadDownload\src\org\gyt\fileUploadDownload\ListFileServlet.java
?????文件???????7599??2018-12-04?23:24??FileUploadDownload\src\org\gyt\fileUploadDownload\UploadServlet.java
?????文件???????6162??2018-12-04?23:12??FileUploadDownload\WebContent\home.jsp
?????文件?????????39??2018-12-04?23:14??FileUploadDownload\WebContent\me
?????文件??????53485??2018-12-04?18:49??FileUploadDownload\WebContent\static\css\animate.min.css
?????文件?????117154??2018-12-04?18:49??FileUploadDownload\WebContent\static\css\bootstrap.min.css
?????文件??????15121??2018-12-04?18:49??FileUploadDownload\WebContent\static\css\st
?????文件???????1097??2018-12-04?18:49??FileUploadDownload\WebContent\static\css\st
?????文件??????10038??2018-12-04?18:49??FileUploadDownload\WebContent\static\css\vegas.min.css
?????文件?????823845??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq1.jpg
?????文件?????940115??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq2.jpg
?????文件????1447656??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq3.jpg
?????文件?????152207??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq4.jpg
?????文件?????619853??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq5.jpg
?????文件?????228356??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq6.jpg
?????文件?????499486??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\bannerq7.jpg
?????文件????????213??2018-12-04?18:49??FileUploadDownload\WebContent\static\img\overlay.png
............此處省略39個文件信息
評論
共有 條評論