use*_*472 1 java arrays file spring-mvc spring-boot
我有file.zipDB喜欢的BLOB.我想在Spring控制器中创建方法,以便在客户端下载此文件.
@RequestMapping(value = "/downloadResolution/{resolutionId}", method = RequestMethod.GET)
public void downloadResolution(@PathVariable("resolutionId") Long resolutionId, HttpServletResponse response) {
Resolution resolution = resolutionService.findOne(resolutionId);
ResolutionArchive resolutionArchive = resolution.getResolutionArchive();
if (resolutionArchive == null) return;
byte[] archive = resolutionArchive.getArchive();
//this byte[] archive - my zip file from db
}
Run Code Online (Sandbox Code Playgroud)
如何更改此methot以便在客户端下载?
用户按下载按钮.Methos从byte []中获取数据,用户可以下载它.
编辑
我尝试了@pleft的解决方案,它的工作原理.我知道 - 我使用ajax进行呼叫方法
function downloadResolution(resulutionId) {
$.ajax({
type: 'GET',
dataType: "json",
url: '/downloadResolution/' + resulutionId,
success: function (data) {
},
error: function (xhr, str) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果我使用ajax,怎么意识到这一点?
你可以使用OutputStream你HttpServletResponse的编写存档字节.
例如
response.setHeader("Content-Disposition", "attachment; filename=file.zip");
response.setHeader("Content-Type", "application/zip");
response.getOutputStream().write(archive);
Run Code Online (Sandbox Code Playgroud)
编辑
样本下载
@RequestMapping(value = "/downloadResolution/{resolutionId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadResolution(@PathVariable("resolutionId") Long resolutionId, HttpServletResponse response) throws IOException {
String test = "new string test bytes";
response.setHeader("Content-Disposition", "attachment; filename=file.txt");
response.getOutputStream().write(test.getBytes());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5449 次 |
| 最近记录: |