javaweb文件上传下载jsp和servlet完整代码
时间: 2023-09-03 10:05:53 浏览: 119
以下是文件上传和下载的完整代码,包括JSP和Servlet:
上传文件的JSP页面(upload.jsp):
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<h2>文件上传</h2>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<label>请选择文件:</label>
<input type="file" name="file"><br/><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
```
上传文件的Servlet(UploadServlet.java):
```
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UploadServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 上传文件保存路径
String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
// 创建保存路径的文件夹
File file = new File(savePath);
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath + "目录不存在,需要创建");
file.mkdir();
}
// 获取上传的文件
Part part = request.getPart("file");
// 获取上传的文件名
String fileName = part.getSubmittedFileName();
// 上传的文件保存到服务器的完整路径
String filePath = savePath + File.separator + fileName;
// 将上传的文件保存到服务器指定的路径中
part.write(filePath);
out.println("文件上传成功!");
}
}
```
下载文件的Servlet(DownloadServlet.java):
```
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DownloadServlet")
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DownloadServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取要下载的文件名
String fileName = request.getParameter("filename");
// 设置文件MIME类型
response.setContentType(getServletContext().getMimeType(fileName));
// 设置Content-Disposition
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// 读取目标文件,通过response将目标文件写到客户端
// 获取目标文件的绝对路径
String fullFileName = getServletContext().getRealPath("/WEB-INF/upload/" + fileName);
// 读取文件
FileInputStream in = new FileInputStream(fullFileName);
OutputStream out = response.getOutputStream();
// 写文件
int len = 0;
byte[] buffer = new byte[1024];
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
```
在web.xml文件中添加以下配置:
```
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/DownloadServlet</url-pattern>
</servlet-mapping>
```
在JSP页面中,可以通过以下方式链接到下载Servlet:
```
<a href="DownloadServlet?filename=xxx.txt">下载xxx.txt文件</a>
```
其中,xxx.txt是要下载的文件名。
阅读全文