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

news/2024/5/18 12:57:55 标签: Java, Servlet, 文件的下载, JSTL, Tomcat

文章目录

      • Servlet后台处理请求
      • JSP处理前端页面显示
      • 效果显示

Servlet_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)]


http://www.niftyadmin.cn/n/1658111.html

相关文章

oracle没有pfile和spfile,Pfile和Spfile配置解释(转载)

摘抄自这里&#xff0c;仅用来记录&#xff0c;以便查阅。Certain files in the database can completely change the way your Oracle 12c database behaves. They can influence everything from performance and tuning as well as troubleshooting. Maintaining and config…

linux 公平 队列_Linux的公平调度(CFS)原理

1、CFS的基本思路在CFS算法引入之前&#xff0c;Linux使用过几种不同的调度算法&#xff0c;一开始的调度器是复杂度为O(n)的始调度算法(实际上每次会遍历所有任务&#xff0c;所以复杂度为O(n)), 这个算法的缺点是当内核中有很多任务时&#xff0c;调度器本身就会耗费不少时间…

df命令能够实现linux,Android系统shell中的df命令实现

当我们在shell中敲击df这条命令的时候&#xff0c;会看到:rootAndroid:/ # dfFilesystem Size Used Free Blksize/dev 446.8M 36.0K 446.8M 4096/mnt/secure 446.8M 0.0 K 446.8M 4096/mnt/asec 446.8M 0.0 K 446.…

parameter、Part的区别 - HttpServletRequest

html页面展示 html页面的代码 <body><form action"FileServlet" method"post" enctype"multipart/form-data"><input type"file" name"newFile"/><br><br><input type"text" …

新鲜新奇事物_探索无文体写作的业余作者——“去世界底部,阅读日常事物。”...

“人类没有什么彼岸&#xff0c;只有一些日常事物&#xff0c;世界的底部就是怯魅之后的日常事物&#xff0c;沉默地存在于次级知识的泡沫深处。”——李盆 《羊呆住了》《羊呆住了》撰文&#xff5c;小灯写歌的时候谁也不知道自己的作品是否能拿奖不同的人对一份作品的看法是多…

注解Annotation - Java

文章目录1. 注解分类1.1. 元注解Target&#xff1a;注解修饰的位置Retention&#xff1a;注解对象的存活时间1.2 JDK内置常用注解1.3 自定义注解2. 注解源码分析2.1 Annotation - 注解接口2.2 AnnotationInvocationHandler - 注解真正起作用的对象 - 代理对象、并利用反射理解注…

时间转为毫秒数_liteos时间管理(九)

1. 时间管理1.1 概述1.1.1 概念时间管理以系统时钟为基础。时间管理提供给应用程序所有和时间有关的服务。系统时钟是由定时/计数器产生的输出脉冲触发中断而产生的&#xff0c;一般定义为整数或长整数。输出脉冲的周期叫做一个“时钟滴答”。系统时钟也称为时标或者Tick。一个…

linux 内核 scsi,Linux_Linux 内核 SCSI IO 子系统分析,概述     LINUX 内核 - phpStudy

Linux 内核 SCSI IO 子系统分析概述LINUX 内核中 SCSI 子系统由 SCSI 上层&#xff0c;中间层和底层驱动模块 [1] 三部分组成&#xff0c;主要负责管理 SCSI 资源和处理其他子系统&#xff0c;如文件系统&#xff0c;提交到 SCSI 子系统中的 IO 请求。因此&#xff0c;理解 SCS…