java下载文件

File file = new File("d:/attfile/a.txt");
response.reset();
response.setContentType("application/octet-stream; charset=UTF-8");
String fileName ;
if(request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0){
	// 处理IE下中文
	fileName = URLEncoder.encode(line.getDocname(), "UTF-8");
}else{
	// 其它浏览器
	fileName = new String(line.getDocname().getBytes("UTF-8"),"ISO-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");  

InputStream in = null;
try {
	in = new FileInputStream(file);
	OutputStream os = response.getOutputStream();
	byte[] buffer = new byte[1024];
	int len;
	while((len = in.read(buffer)) > 0){
		os.write(buffer, 0, len);
	}
	os.flush();
	in.close();
} catch (FileNotFoundException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}


你可能感兴趣的:(java下载文件)