Java 使用angular2和spring下载文件
我正在尝试使用spring代码下载文件,我的代码正在运行,但下载文件夹中没有下载的文件。我的代码如下:-Java 使用angular2和spring下载文件,java,spring,angular2-routing,Java,Spring,Angular2 Routing,我正在尝试使用spring代码下载文件,我的代码正在运行,但下载文件夹中没有下载的文件。我的代码如下:- @RequestMapping(value="/downloadRecordFile/{id}", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE) @Timed public void downloadRecordFile(@PathVariable("id") Long id,Htt
@RequestMapping(value="/downloadRecordFile/{id}",
method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public void downloadRecordFile(@PathVariable("id") Long id,HttpServletRequest request,HttpServletResponse response) throws IOException {
File fileName = new File("C:\workarea\UplodedRecords\test_annotation.pdf");
if (fileName != null) {
if (!fileName.exists()) {
String errorMessage = "Sorry. The file you are looking for does not exist";
log.info(errorMessage);
OutputStream outputStream = response.getOutputStream();
outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));
outputStream.close();
return;
}
String mimeType = URLConnection.guessContentTypeFromName(fileName.getName());
if (mimeType == null) {
mimeType = "application/octet-stream";
}
log.info("mimetype : " + mimeType);
response.setContentType(mimeType);
response.setHeader("Content-Disposition", String.format("inline; filename=\"" + fileName.getName() + "\""));
response.setContentLength((int) fileName.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
//Copy bytes from source to destination(outputstream in this example), closes both streams.
FileCopyUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
response.getOutputStream().flush();
}
}
}
我的代码没有出现错误或异常,但它没有下载文件
角度2代码:-
function downloadRecord() {
alert("Downloading");
var record_id = 1234;
downloadRecordFile.query({id: record_id}, onSuccess);
}
这是我的服务
(function() {
'use strict';
angular
.module('testApp')
.factory('downloadRecordFile', downloadRecordFile);
downloadRecordFile.$inject = ['$resource'];
function downloadRecordFile($resource) {
var resourceUrl = 'api/downloadRecordFile/:id';
return $resource(resourceUrl, {}, {
'query': { method: 'GET', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
if (data) {
data = angular.fromJson(data);
}
return data;
}
}
});
}
})();
代码中是否有错误请帮助我。如果正在下载,为什么要使用
products=MediaType.APPLICATION\u JSON\u VALUE
?尝试删除。仍不工作。然后提供详细信息。你看到日志了吗?控制器被调用了吗?你怎么打电话下载?从浏览器发送的是什么,在控制器中得到的是什么?文件是否存在?“不工作”这句话毫无意义。这是你的代码,我们无法在没有看到信息的情况下猜出错误。我正在使用angular2调用get方法:-不要将其放在注释中。更新问题:如果正在下载,为什么要使用products=MediaType.APPLICATION\u JSON\u VALUE
?尝试删除。仍不工作。然后提供详细信息。你看到日志了吗?控制器被调用了吗?你怎么打电话下载?从浏览器发送的是什么,在控制器中得到的是什么?文件是否存在?“不工作”这句话毫无意义。这是你的代码,我们无法在没有看到信息的情况下猜出错误。我正在使用angular2调用get方法:-不要将其放在注释中。更新问题