Ajax 使用Spring引导和Java脚本从应用程序下载压缩的父文件夹时出错
我正在开发SpringBoot应用程序,我需要提供下载压缩报告文件的功能。为了做到这一点,我将压缩的报告文件转换为字节数组,然后尝试在浏览器端发送它。我可以下载压缩的报告文件,但当我尝试解压缩时,会显示一条错误消息“压缩的压缩文件夹无效”。请查找下面的代码片段: 服务器端代码:Ajax 使用Spring引导和Java脚本从应用程序下载压缩的父文件夹时出错,ajax,spring-boot,Ajax,Spring Boot,我正在开发SpringBoot应用程序,我需要提供下载压缩报告文件的功能。为了做到这一点,我将压缩的报告文件转换为字节数组,然后尝试在浏览器端发送它。我可以下载压缩的报告文件,但当我尝试解压缩时,会显示一条错误消息“压缩的压缩文件夹无效”。请查找下面的代码片段: 服务器端代码: @ResponseBody @RequestMapping(value = "/downloadResult", method = RequestMethod.POST, produces=&
@ResponseBody
@RequestMapping(value = "/downloadResult", method = RequestMethod.POST, produces="application/zip" )
public byte[] downloadResult(@RequestBody ResultParamsVO resultParamsVO, HttpServletRequest request,
HttpServletResponse response) {
byte[] result = null;
try {
HttpSession session = request.getSession(false);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String user = auth.getPrincipal().toString();
String testType = null;
if (session.getAttribute("testType") != null) {
testType = session.getAttribute("testType").toString();
}
result = jobService.downloadResult(resultParamsVO, testType, user);
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=resultDownload1.zip");
} catch (Exception e) {
logger.error("Error in getResult() for job id - " + resultParamsVO.getJobId(), e.getMessage());
e.printStackTrace();
}
return result;
}
浏览器端Ajax调用:
$.ajax({
type: "POST",
url: "/downloadResult",
contentType: "application/json",
data: JSON.stringify(resultParamsVO),
success: function(result) {
var downloadUrl = window.URL.createObjectURL(new Blob([result], {type: "application/zip"}));
var a = document.createElement("a");
a.href = downloadUrl;
a.download = "resultDownload.zip";
document.body.appendChild(a);
a.click();
}
});
请帮忙。提前谢谢。它可以从浏览器中工作吗?不可以。它不能从浏览器中工作。然后java代码中出现了一些问题。我可以从相同的代码中下载文本文件。所以,我认为问题在于只有.zip文件。哦!然后可能是拉链问题。