下載文件
- 創(chuàng)建獲取文件列表Servlet炬转,首次訪問(wèn)時(shí)request中沒(méi)有目錄參數(shù)蹬昌,需要給個(gè)默認(rèn)目錄艘策。遍歷該目錄下文件秉馏,創(chuàng)建對(duì)應(yīng)的文件對(duì)象(包含文件名,文件/文件夾唆涝,絕對(duì)路徑等屬性)找都,添加到List中,并把List保存到域中廊酣。轉(zhuǎn)發(fā)到文件列表jsp中
private static List<FileJavaClass> fileList = new ArrayList<>();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取參數(shù)中的filePath
String filePath = request.getParameter("filePath");
//如果參數(shù)中沒(méi)有filePath檐嚣,說(shuō)明是首次訪問(wèn),設(shè)置filePath為根目錄
if (filePath == null || filePath.isEmpty()) {
filePath = "D:/download";
}
//判斷是否是根目錄啰扛,并保存在域中,用于判斷是否顯示返回按鈕
if (filePath.equalsIgnoreCase("D:/download") || filePath.equalsIgnoreCase("D:\\download")) {
request.setAttribute("isRoot", true);
}else {
request.setAttribute("isRoot", false);
}
File file = new File(filePath);
//在域中保存上級(jí)目錄嗡贺,用于返回上級(jí)目錄
request.setAttribute("parent", file.getParentFile());
File[] files = file.listFiles();
//先清空List再想List中添加該目錄下遍歷出來(lái)的文件
fileList.clear();
if (files != null && files.length > 0) {
for (File file2 : files) {
//創(chuàng)建自定義的保存文件關(guān)鍵屬性的對(duì)象
FileJavaClass fileJavaClass = new FileJavaClass();
//保存文件的名字
fileJavaClass.setName(file2.getName());
//保存文件的絕對(duì)路徑
fileJavaClass.setPath(file2.getPath());
//保存文件是文件或文件夾隐解,用于判斷文件的操作是打開(kāi)還是下載
fileJavaClass.setIsFile(file2.isFile());
//添加到List中
fileList.add(fileJavaClass);
}
}
//List保存到域中
request.setAttribute("fileList", fileList);
//轉(zhuǎn)發(fā)到文件列表jsp中
request.getRequestDispatcher("/downloadlist.jsp").forward(request, response);
}
- 創(chuàng)建展示文件列表jsp
<body>
<!-- 判斷是否是根目錄,決定是否顯示返回按鈕 -->
<c:choose>
<c:when test="${isRoot==false}">
<a href="${pageContext.request.contextPath}/DownloadListServlet?filePath=${parent}">返回</a>
</c:when>
</c:choose>
<!-- 列表 -->
<table border="1" width="500">
<tr>
<th>編號(hào)</th>
<th>文件名</th>
<th>操作</th>
</tr>
<!-- 遍歷域中保存的List -->
<c:forEach items="${fileList}" var="v" varStatus="vs">
<tr>
<td>${vs.count}</td>
<td>${v.name}</td>
<c:choose>
<!-- 如果是文件就顯示下載并超鏈接到下載Servlet诫睬,參數(shù)為文件的絕對(duì)路徑和文件名 -->
<c:when test="${v.isFile}">
<td>
<a href="${pageContext.request.contextPath}/DownloadServlet?filePath=${v.path}&fileName=${v.name}">下載</a>
</td>
</c:when>
<!-- 如果是文件夾就顯示打開(kāi)并超鏈接到文件列表Servlet煞茫,參數(shù)為文件夾的絕對(duì)路徑 -->
<c:otherwise>
<td>
<a href="${pageContext.request.contextPath}/DownloadListServlet?filePath=${v.path}">打開(kāi)</a>
</td>
</c:otherwise>
</c:choose>
</tr>
</c:forEach>
</table>
</body>
- 創(chuàng)建下載Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取文件路徑參數(shù)
String filePath = request.getParameter("filePath");
//獲取文件名,用于給響應(yīng)頭設(shè)置默認(rèn)文件名
String fileName = request.getParameter("fileName");
//根據(jù)文件的路徑創(chuàng)建輸入流
InputStream in = new FileInputStream(filePath);
//處理文件名
fileName = URLEncoder.encode(fileName, "utf8");
//下載文件需要給響應(yīng)頭設(shè)置參數(shù)
response.setHeader("content-disposition", "attachment;fileName=" + fileName);
//獲取輸出流
ServletOutputStream out = response.getOutputStream();
byte[] buff = new byte[1024];
Integer length = -1;
while((length = in.read(buff)) != -1) {
out.write(buff, 0, length);
}
in.close();
out.close();
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者