|
如题,服务端的代码 是:
@RequestMapping(value = "downland/abc")
public void baseDataExtraction(HttpServletResponse response ,HttpServletRequest request) throws Exception
{
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName="+"123.txt");
try {
File file=new File("D:\123.txt"); ///
FileInputStream inputStream=new FileInputStream(file);
OutputStream os=response.getOutputStream();
byte[] b=new byte[1024];
int length;
while((length=inputStream.read(b))>0)
{
os.write(b,0,length);
}
inputStream.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
遇到的问题是:在客户端点击下载按钮的时候,浏览器没有弹出下载提示框让用户下载,函数可以正常执行,但是在客户端那边却没有任何反应,也没有下载。 |
|
80分 |
改为以下试:
response.setContentType("application/x-download");
另外加上以下代码避免缓存:
response.addHeader("pragma","NO-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expries",0);
|
|
加了还是不行,我前台是Dart写的,不知道会不会有影响。一个同事用Grails,运行我的代码可以正常运行,在客户端点击下载时可以响应,我就不可以,感觉很奇怪 |
|
|
springmvc下载 百度一大堆 吧
|
|
|
换浏览器试试看看。
|
|