Jsp 如何从服务器下载文件?
我已使用apache common fileupload和common-io成功地将多个文件上载到服务器。上载位置是服务器上的目录,例如它可能是c:\UploadJsp 如何从服务器下载文件?,jsp,jakarta-ee,Jsp,Jakarta Ee,我已使用apache common fileupload和common-io成功地将多个文件上载到服务器。上载位置是服务器上的目录,例如它可能是c:\Upload 现在如何从服务器下载文件?特别是从服务器上的目录中我认为最简单的方法是从servlet中执行。类似这样的代码应该可以实现以下功能: public void sendFile(HttpServletResponse response, File file) throws IOException { Inpu
现在如何从服务器下载文件?特别是从服务器上的目录中我认为最简单的方法是从servlet中执行。类似这样的代码应该可以实现以下功能:
public void sendFile(HttpServletResponse response, File file)
throws IOException {
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
while (int val = fis.read() >= 0) {
response.getOutputStream().write(val);
}
} finally {
if( is != null ) {
is.close();
}
}
}
当然,假设您已经在前面设置了内容类型
标题等等。有优化的余地,但这是总体思路