HttpClient + FileUpload how to download file from servlet to my app?
I have watched the upload snippets which use HttpClient and FileUpload. But I couldn't find any snippet which demo the HttpClient+FileUpload download :( If you know 开发者_Python百科links or even some demo projects share please
Much appreciated useful comments :)
Andrew
Within the web context, you can use a ServletOutputStream. Here resource path info is passed as the extra path info on the 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);
}
精彩评论