【发布时间】:2013-03-23 20:19:43
【问题描述】:
我是 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,但它们都是这样的……它们只允许下载特定的文件。 谁能帮我? 非常感谢!
【问题讨论】:
-
你试过什么?您是否对下载的代码进行了任何修改?你做了哪些改变?什么有效?什么没有?除了寻找可以下载的完整解决方案之外,您是否进行过任何研究?只是从互联网上复制和粘贴代码不会让你走得太远,有时你必须自己做。
-
@Adrian 感谢您的回复。正如我所说,上面的代码仅适用于在 Servlet 中下载作为其路径的文件。在此示例中,在 init() 函数中。我试图为 filePath 属性提供一个路径作为字符串。例如:filepath = "C://Apache//Application//data//",但我收到一个错误:访问被拒绝。我尝试了其他方法:使用文件属性和 listFiles 方法在我的 JSP 中创建文件夹内容列表,然后我编写 '
' 但这仅适用于 IE将目标另存为。 -
@user2236267 尝试使用不同的路径,例如 C:\\external\\path。此外,请确保您的用户有足够的权限写入该文件夹。我没有包含该信息,因为我假设您已经知道了。
-
我对 JAVA 编程非常陌生。我尝试了很多方法来解决我的问题,但我没有找到任何解决方案。我会尝试更多,如果我能找到任何解决方案,我会在这里发布。
标签: java servlets upload download