文件的下载、文件的显示、 - Tomcat

42 阅读1分钟

文章目录

Servlet后台处理请求

@WebServlet( urlPatterns= {"/FileDownloadServlet"} )
public class FileDownloadServlet extends HttpServlet{
	
    // 默认的资源路径
	private String dirPath = "C:\\Users\\lrc\\Desktop\\Java-Web项目";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 获取当前目录
		String parent = req.getParameter("fileName");
		
		// 用来存储当前点击的文件
		String currentPath = dirPath;
		
		// 用来标记是否需要返回到上一级文件夹
		String lastCatalog = req.getParameter("lastCatalog");
		
		// 1. 判断当前的点击是否是返回上级目录
		if("1".equals(lastCatalog)) {
			parent = parent.substring(0, parent.lastIndexOf("/"));
			System.out.println(parent);
			currentPath = currentPath + "/" + parent;
			File file =  new File(currentPath);
			
			req.setAttribute("files", file.listFiles());
			req.setAttribute("parent", parent);
		
			// 2. 判断当前点击是否有传当前目录路径、没有则直接访问已经设置好的资源路径
		}else if(parent==null || "".equals(parent.trim())) {
			
			File file =  new File(currentPath);
			
			File[] files = file.listFiles();
			
			req.setAttribute("files", files);
		
		// 3. 判断是进入下一级目录还是下载当前的文件
		}else {
			
			// 3.1 先获取文件在服务器中的绝对路径
			currentPath = currentPath + "/" + parent;
			File file = new File(currentPath);
			
			// 3.2 判断该文件是否是文件,如果是文件,则直接下载文件,不进行页面跳转
			if(file.isFile()) {
				InputStream is = new FileInputStream(file);
				System.out.println(file.getName());
				resp.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
				OutputStream os = resp.getOutputStream();
				
				byte[] buffer = new byte[1024];
				int len = -1;
				
				while((len = is.read(buffer))!=-1) {
					os.write(buffer, 0, len);
				}
				return;
				// 3.3 当前文件是目录,则返回当前目录下的文件列表
			}else {
				File[] files = file.listFiles();
				req.setAttribute("files", files);
				req.setAttribute("parent", parent);
			}
			
			
		}
		
		req.getRequestDispatcher("/FIleDownload.jsp").forward(req, resp);
		
	}
	
}

JSP处理前端页面显示

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
<!-- 统一设置相对路径以base路径为标准 -->
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/" />
<title>Insert title here</title>
</head>
<body>
	<table border="1">
		<thead>
			<tr>
				<th>资源名</th>
				<th>资源大小</th>
			</tr>
		<thead>
		
		<tbody>
			<c:if test="${not empty parent}">
				<tr>
					<td colspan="2" align="center"><a href="FileDownloadServlet?fileName=${parent}&lastCatalog=1">...上一级</a></td>
				</tr>
			</c:if>
			<c:forEach items="${files}" var="file">
				<tr>
					<td><a href="FileDownloadServlet?fileName=${parent}/${file.name}">${file.name}</a></td>
					<td>${file.length()}字节</td>
				</tr>
			</c:forEach>
		</tbody>
		
	</table>
</body>
</html>

效果显示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B3eUg4cn-1576003320444)(en-resource://database/12387:1)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UnaWXJ1l-1576003320447)(en-resource://database/12389:1)]