我有一个返回byte[]
的Web服务方法,所以我这样做是为了强制下载返回的文件
final byte[] content = axisService.getWebFileContent(pieceJointe.getVlidnlfy());
if (content != null) {
final Map<String, Object> lrFile = new HashMap<>(2);
lrFile.put("CONTENT", content);
lrFile.put("MIMETYPE", "application/octet-stream");
response.reset();
response.resetBuffer();
response.setContentType("application/force-download");
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Content-Disposition",
"attachment; filename=\"" + pieceJointe.getLbnompcj() + "\"");// fileName);
response.getOutputStream().write((byte[]) lrFile.get("CONTENT"));
response.setContentLength(((byte[]) lrFile.get("CONTENT")).length);
response.getOutputStream().close();
response.flushBuffer();
}
但是它不起作用,我在控制台上看不到任何错误。我是否需要向响应对象添加其他配置?
您正在写输出后设置内容的长度。
response.getOutputStream().write((byte[]) lrFile.get("CONTENT"));
response.setContentLength(((byte[]) lrFile.get("CONTENT")).length);
到
response.setContentLength(((long) lrFile.get("CONTENT")).length);
response.getOutputStream().write((byte[]) lrFile.get("CONTENT"));