需要通过地址下载一个文件:
http://localhost:9025/download/b.csv
后台代码:
@RequestMapping(
value = {"/download/{filename:}"},
method = {RequestMethod.GET}, produces = {"application/json"}
)
public ResponseEntity<BigDecimal> download(
@PathVariable(value = "filename") String filename, HttpServletRequest request, HttpServletResponse response) throws NotFoundException{
try {
logger.info(id);
File f = new File("E:/" + filename);
downLoad(f, response);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
return null;
}
访问地址,报错:406 Not Acceptable;一顿查询,看到文章https://blog.csdn.net/allenChenZhiMing/article/details/82015932
需要增加@CrossOrigin(origins = "*", maxAge = 3600),调整produces = {},调整代码后:
@RequestMapping(
value = {"/download/{filename:.+}"},
method = {RequestMethod.GET}, produces = {}
)
@CrossOrigin(origins = "*", maxAge = 3600)
public ResponseEntity<BigDecimal> download(
@PathVariable(value = "filename") String filename, HttpServletRequest request, HttpServletResponse response) throws NotFoundException{
try {
logger.info(id);
File f = new File("E:/" + filename);
downLoad(f, response);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
return null;
}
再次访问地址:发现后台接收到的文件名不带后缀,点(.)后边的被截取。再次查询,看到文章:https://blog.csdn.net/liming19890713/article/details/78417995
调整@RequestMapping(
value = {"/test/{id:.+}"},
method = {RequestMethod.GET}, produces = {}
)
再次访问,成功下载文件