电脑下载正常,手机端乱码?ResponseEntity文件下载问题的深度解析及解决方案
许多开发者在开发文件下载功能时,常常遇到一个难题:电脑端下载的文件能够正常打开,但手机端却无法打开或显示乱码。本文将通过一个Spring框架ResponseEntity文件下载案例,深入分析此问题的原因,并提供有效的解决方案。
案例分析:
后端使用Spring框架的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;
登录后复制
本文来自互联网或AI生成,不代表软件指南立场。本站不负任何法律责任。