我是 JAVA 技术的新手,尤其是 Servlet。我需要制作一个 Web 应用程序项目,该项目可以向/从服务器 (tomcat) 上传和下载文件。我已经有一个上传 servlet,工作正常。
我也有一个下载的servlet,在网上找到的。但问题是这个servlet只允许下载一个特定的文件,并且这个特定文件的路径在servlet中给出。我需要让客户看到我的上传文件夹的全部内容,并从这个文件夹中选择他想下载的文件。
下载servlet的代码是这样的:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private static final int BUFSIZE = 4096;
private String filePath;`
public void init() {
// the file data.xls is under web application folder
filePath = getServletContext().getRealPath("") + File.separator;// + "data.xls";
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
File file = new File(filePath);
int length = 0;
ServletOutputStream outStream = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(filePath);
// sets response content type
if (mimetype == null) {
mimetype = "application/octet-stream";
}
response.setContentType(mimetype);
response.setContentLength((int)file.length());
String fileName = (new File(filePath)).getName();
// sets HTTP header
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
byte[] byteBuffer = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(file));
// reads the file's bytes and writes them to the response stream
while ((in != null) && ((length = in.read(byteBuffer)) != -1))
{
outStream.write(byteBuffer,0,length);
}
in.close();
outStream.close();
}
}
JSP页面是这样的:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Download Servlet Test</title>
</head>
<body>
Click on the link to download: <a href="DownloadServlet">Download Link</a>
</body>
</html>
我搜索了很多 servlet,但它们都是这样的...它们只允许下载特定文件。 谁能帮我? 非常感谢!
最佳答案
由于您是在 doGet
方法上处理数据,因此您可以将参数传递给 servlet,在其中指定要下载的文件的名称。为此,您应该假设文件名存在于您的基本路径中。代码可以是这样的:
HTML
<body>
Click on the link to download:
<a href="DownloadServlet?fileName=data.xls">Download Link</a>
</body>
Java Servlet 代码:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//retrieving the parameter by its name
String fileName = request.getParameter("fileName"); //this will return `data.xls`
//using the File(parent, child) constructor for File class
File file = new File(filePath, fileName);
//verify if the file exists
if (file.exists()) {
//move the code to download your file inside here...
} else {
//handle a response to do nothing
}
}
请注意,由于代码现在使用 File(String parent, String child)
构造函数,您的 filePath
不应再包含分隔符(这将由 Java 处理):
public void init() {
// the file data.xls is under web application folder
filePath = getServletContext().getRealPath("");
}
关于java - 用于从特定文件夹下载文件的 Servlet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15765799/