错误
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
可能原因一
没有设置全局跨域,检查代码,加上全局跨域
可能原因二
public static void downloadFile(HttpServletResponse resp, String name, String downloadPath) throws IOException {
String filePath = null;
try {
filePath= URLDecoder.decode(downloadPath,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//String realPath = "D:" + File.separator + "apache-tomcat-8.5.15" + File.separator + "files";
String realPath=filePath;//项目路径下你存放文件的地方
String path = realPath + File.separator + name;//加上文件名称
File file = new File(path);
if(!file.exists()){
throw new IOException("文件不存在");
}
name = new String(name.getBytes("GBK"), "ISO-8859-1");
resp.reset();
/*
* 跨域配置
* */
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
resp.addHeader("Access-Control-Allow-Headers", "Content-Type");
resp.setContentType("application/octet-stream");
resp.setCharacterEncoding("utf-8");
resp.setContentLength((int) file.length());
resp.setHeader("Content-Disposition", "attachment;filename=" + name);
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
os = resp.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int i = 0;
while ((i = bis.read(buff)) != -1) {
os.write(buff, 0, i);
os.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
关键代码
resp.reset();
/*
* 跨域配置
* */
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
resp.addHeader("Access-Control-Allow-Headers", "Content-Type");
启发
response.reset()调用以后,全局设置的跨域header就丢了,因此重新设置的。