我有一个servlet,它正在从指定位置下载文件。 目前,我的网址是http://localhost:8080/DownloadFromservlet/download?file=c://tmp/DownloadFileServlet.pdf 它应该像:http://localhost:8080/fileserver/download?file=c:/tmp/DownloadFileServlet.pdf
我需要用文件服务器替换DownloadFromservlet。这是我的代码
@WebServlet("/download")
public class download extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sourceFile = request.getParameter("file");
sourceFile = sourceFile != null ? sourceFile.trim() : "";
File fo = null;
if (sourceFile.length() > 0)
fo = new File(sourceFile);
response.setContentType("application/octet-stream");
ServletOutputStream servletOutputStream = response.getOutputStream();
BufferedInputStream bufferedInoutStream = new BufferedInputStream(new FileInputStream(fo));
byte[] buffer = new byte[8192];
int bytesRead = 0;
// ;
while (bytesRead != -1) {
bytesRead = bufferedInoutStream.read(buffer);
servletOutputStream.write(buffer);
}
servletOutputStream.flush();
if (servletOutputStream != null) {
try {
servletOutputStream.close();
} catch (Throwable tx) {
}
}
if (bufferedInoutStream != null) {
try {
bufferedInoutStream.close();
} catch (Throwable tx) {
}
}
}
}
您能帮我如何更改它吗?