我已经看过使用HttpClient和FileUpload的上传代码段。但我找不到任何演示HttpClient + FileUpload下载的片段:(如果你知道链接甚至一些演示项目请分享
非常感谢有用的评论:)
安德鲁
答案 0 :(得分:1)
在网络环境中,您可以使用ServletOutputStream。这里资源路径信息作为HTTP上的额外路径信息传递。
final ServletOutputStream out = res.getOutputStream();
res.setContentType("application/octet-stream");
String file = req.getPathInfo();
if (file == null) {
out.println("Extra path info was null; should be a resource to view");
return;
}
// Convert the resource to a URL
URL url = getServletContext().getResource(file);
if (url == null) {
out.println("Resource " + file + " not found");
return;
}
//Serve the file
InputStream in = url.openStream();
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}