Spring: 无法从服务器下载文件 (FileInputStream)
Spring: can't download a file from a server (FileInputStream)
我无法从控制器下载文件。没有抛出异常。
用户单击视图中的按钮,方法被正确调用并到达结束,但文件从未下载。
编辑:文件找到了。另外,用户点击的按钮是模态的。
public void downloadFile(Long fileId, HttpServletRequest request, HttpServletResponse response)
throws HibernateValidationException, IOException, FileNotFoundException {
Attachment attachment = attachmentDao.getById(fileId);
String fileName = attachment.getName();
File file = new File(FILE_DIRECTORY, attachment.getSysname());
String contentType = request.getSession().getServletContext().getMimeType(file.getAbsolutePath());
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=\""+URLEncoder.encode(fileName,"UTF-8")+"\"");
response.setHeader("Cache-Control","no-cache, no-store");
InputStream is = new FileInputStream(file);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
is.close();
}
你的方法无效。所以,正确的文件下载:
try (FileInputStream is = new FileInputStream(generatedFile)) {
ByteArrayResource resource = new ByteArrayResource(is.readAllBytes());
return ResponseEntity.ok()
.header("Content-disposition", "attachment; filename=" + UriEncoder.encode(realReportName))
.contentLength(resource.contentLength())
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.body(resource);
} finally {
Files.deleteIfExists(generatedFile.toPath());
}
和控制器声明:
public ResponseEntity getFile()
事实证明这不是我的后端方法。这是前端。
方法是这样的,我发现无法下载这种形式的文件:
this.downloadFile = function(file) {
return $http({
method: 'GET',
url: '/app/downloadFile',
params: {
id: file.id,
}
})
}
我不得不将其更改为:
this.downloadFile = function(file) {
$window.location.href = '/app/downloadFile?id=' + file.id;
}
我无法从控制器下载文件。没有抛出异常。
用户单击视图中的按钮,方法被正确调用并到达结束,但文件从未下载。
编辑:文件找到了。另外,用户点击的按钮是模态的。
public void downloadFile(Long fileId, HttpServletRequest request, HttpServletResponse response)
throws HibernateValidationException, IOException, FileNotFoundException {
Attachment attachment = attachmentDao.getById(fileId);
String fileName = attachment.getName();
File file = new File(FILE_DIRECTORY, attachment.getSysname());
String contentType = request.getSession().getServletContext().getMimeType(file.getAbsolutePath());
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=\""+URLEncoder.encode(fileName,"UTF-8")+"\"");
response.setHeader("Cache-Control","no-cache, no-store");
InputStream is = new FileInputStream(file);
IOUtils.copy(is, response.getOutputStream());
response.flushBuffer();
is.close();
}
你的方法无效。所以,正确的文件下载:
try (FileInputStream is = new FileInputStream(generatedFile)) {
ByteArrayResource resource = new ByteArrayResource(is.readAllBytes());
return ResponseEntity.ok()
.header("Content-disposition", "attachment; filename=" + UriEncoder.encode(realReportName))
.contentLength(resource.contentLength())
.contentType(MediaType.parseMediaType("application/vnd.ms-excel"))
.body(resource);
} finally {
Files.deleteIfExists(generatedFile.toPath());
}
和控制器声明:
public ResponseEntity getFile()
事实证明这不是我的后端方法。这是前端。
方法是这样的,我发现无法下载这种形式的文件:
this.downloadFile = function(file) {
return $http({
method: 'GET',
url: '/app/downloadFile',
params: {
id: file.id,
}
})
}
我不得不将其更改为:
this.downloadFile = function(file) {
$window.location.href = '/app/downloadFile?id=' + file.id;
}