谷歌,QQ浏览器,搜狗浏览器,亲测可用
public String download(HttpServletRequest request,HttpServletResponse response){
String url = "F:\\新建文本文档.ini";
File fileurl = new File(url);
//浏览器下载后的文件名称showValue,
String showValue = "123.txt";
//将需要的文件转成byte数组,为bytes
InputStream sbs = new ByteArrayInputStream(bytes);
try{
//根据条件得到文件路径
System.out.println("===========文件路径==========="+fileurl);
//将文件读入文件流
InputStream inStream = new ByteArrayInputStream(bytes);
//获得浏览器代理信息
final String userAgent = request.getHeader("USER-AGENT");
//判断浏览器代理并分别设置响应给浏览器的编码格式
String finalFileName = URLEncoder.encode(showValue,"UTF8");//其他浏览器
//设置HTTP响应头
response.reset();//重置 响应头
response.setContentType("application/x-download");//告知浏览器下载文件,而不是直接打开,浏览器默认为打开
response.addHeader("Content-Disposition" ,"attachment;filename=\"" +finalFileName+ "\"");//下载文件的名称
// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
while ((len = inStream.read(b)) > 0){
response.getOutputStream().write(b, 0, len);
}
inStream.close();
response.getOutputStream().close();
}catch(Exception e) {
e.printStackTrace();
}
return "";
}
弱弱的说一句,忘记从谁的文章里复制的了,原创看到的话,介意的话,给我留言,谢谢.........