java下载文件

 /**
*
* @param file
* @param response
*/
private void downLoad(File file, HttpServletResponse response,
String browser) throws IOException {
InputStream is = new FileInputStream(file);
try { String fileName = file.getName().replaceAll(" ", "");
OutputStream os = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);
BufferedOutputStream bos = new BufferedOutputStream(os); if (browser.indexOf("msie") != -1) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "GBK");
} else if (browser.indexOf("Firefox") != -1) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} else if (browser.indexOf("Safari") != -1) {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
} bos.flush();
bis.close();
bos.close(); is.close(); os.close(); } catch (Exception ex) { log.info(""); } finally {
is.close();
}
}
上一篇:centos中安装chromium和flash


下一篇:Natas Wargame Level 3 Writeup 与 robots.txt