ResponseEntity 文件下载,电脑下载可以正常打开,手机下载无法打开或打开乱码,这种情况是因为什么?

下载文件功能,电脑下载可以正常打开,手机下载无法打开,或者打开乱码

后台代码

HttpStatus statusCode = HttpStatus.OK;

HttpHeaders headers = new HttpHeaders();

if (download) {

String fileName = new String(r.getName().getBytes(), "ISO8859-1");

headers.add("Content-Disposition", "attachment;filename=" + fileName);

}

Resource resource = resourceLoader.getResource("file:" + path + "/" + id);

InputStream in = resource.getInputStream();

byte[] body = new byte[in.available()];

in.read(body);

ResponseEntity<byte[]> streamResponse = new ResponseEntity(body, headers, statusCode);

return streamResponse;

前端下载

function handleDownload(file: any) {

console.log(file)

let a = document.createElement('a')

let event = new MouseEvent('click')

a.download = file.name

a.href = file.url

a.dispatchEvent(event)

}


回答:

handleDownload(file: any) file.url 是什么东西?移动端对于 bloburl 和 dataurl 支持都不太行


回答:

在请求头中设置content-type
headers.setContentType(MediaType);

以上是 ResponseEntity 文件下载,电脑下载可以正常打开,手机下载无法打开或打开乱码,这种情况是因为什么? 的全部内容, 来源链接: utcz.com/p/945355.html

回到顶部