如何从服务器下载文件?
问题描述:
我已成功上传多个文件到服务器使用apache common-fileupload和common-io.Uploaded位置是服务器上的一个目录,例如它可能是c:\ uploaded如何从服务器下载文件?
现在如何从服务器下载文件?特别是从服务器上的目录
答
我认为最简单的方法是从你的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();
}
}
}
假设,当然,你已经设置content-type
头部等之前。有优化的空间,但这是总体思路。