java服务器下载文件浏览器无反应

本文介绍了一个使用Java在HTTP响应中实现文件流下载的例子。通过设置响应头和字符集编码,可以将指定的内容作为文件下载到客户端。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用google浏览器作的测试
需求:
将指定内容以文件流的形式下载

代码:

@RequestMapping("saveSort")
public void saveSort(String[] id, HttpServletResponse response){
if (ArrayUtils.isNotEmpty(id)){
OutputStream out = null;
try {
String formatdata = tvSourceService.createConfigLine(id);
if (StringUtils.isBlank(formatdata)){
return ;
}
//下载
String fileName ="IPTV.txt";
response.reset();
response.setHeader("Content-Disposition","attachment; fileName="+URLEncoder.encode(fileName,"UTF-8"));
// response.setHeader("Cache-Control","max-age=0");
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");

out = response.getOutputStream();
out.write(formatdata.getBytes("UTF-8"));
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


原因:
浏览器的下载插件影响(如迅雷)

解决:
方法1:停用浏览器下载插件
方法2:重置浏览器设置,见[url=http://jingyan.baidu.com/album/c33e3f48a1445fea15cbb537.html?picindex=1]百度经验[/url]

[img]http://dl2.iteye.com/upload/attachment/0118/2197/8bba3fe7-676b-3fce-80fb-16948570dd0f.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值