我想从我的web应用程序使用浏览器在Android手机上下载文件(在我的情况下,我使用弹簧mvc尽管在这,因为我认为这是一个常见的问题,而不是框架特定)。事情是,尝试在Android设备的铬或本地浏览器的文件下载永远不会结束,总是出现在下载应用程序In progress
或queue
(取决于Android版本,我尝试与4.1.1和5.1.1)。从Android的HTTPS在浏览器中下载文件
我试图改变参与http-headers
Content-Type
,Content-Disposition
与内联或附件,指定Content-Length
没有运气。
事情是,在Windows或Linux的桌面浏览器的文件下载正确的情况下。
我的一些尝试:在@RequestMapping
@RequestMapping(value= {"/evidenciaDownload"},method = RequestMethod.GET)
public ResponseEntity<byte[]> getEvidencia(
@RequestParam(value = "paramsCoded", required = true) String paramsCoded) throws IOException {
String url = configPropsService.getEvidenciaUrlDownload() + URLEncoder.encode(paramsCoded,"UTF-8");
logger.info("[getEvidencia] Recuperem l'evidencia de : " + url);
// bytes are correct
byte[] evidencia = downloadFile(url);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Content-Type", "application/pdf");
//responseHeaders.set("Content-Disposition","attachment; filename=\"evidencia.pdf\";");
responseHeaders.set("Content-Disposition","inline; filename=evidencia.pdf");
responseHeaders.setContentLength(evidencia.length);
return new ResponseEntity<byte[]>(evidencia, responseHeaders,HttpStatus.CREATED);
}
直接使用org.springframework.web.bind.annotation.ResponseBody
返回文档bytes[]
和produces = "application/pdf"
:
使用
org.springframework.http.ResponseEntity
@RequestMapping(value= {"/evidenciaDownload"},method = RequestMethod.GET, produces = "application/pdf") public @ResponseBody byte[] getEvidencia( @RequestParam(value = "paramsCoded", required = true) String paramsCoded) throws IOException { String url = configPropsService.getEvidenciaUrlDownload() + URLEncoder.encode(paramsCoded,"UTF-8"); logger.info("[getEvidencia] Recuperem l'evidencia de : " + url); return downloadFile(url); }
或写在
javax.servlet.http.HttpServletResponse
OutputStream的文件,并指定标题:@RequestMapping(value= {"/documentDownload"},method = RequestMethod.GET) public void getDocument( @RequestParam(value = "paramsCoded", required = true) String paramsCoded, HttpServletResponse response) throws IOException { String url = configPropsService.getDocumentsNotficacioUrlDownload() + URLEncoder.encode(paramsCoded,"UTF-8"); logger.info("[getDocument] Recuperem el document de : " + url); // bytes are correct byte[] downloadFile = downloadFile(url); ServletOutputStream outputStream = response.getOutputStream(); int fileLength = IOUtils.copy(new ByteArrayInputStream(downloadFile), outputStream); response.setHeader("Content-Disposition","attachment;filename=\"evidencia.pdf\""); response.setContentLength(fileLength); response.setContentType("application/pdf"); outputStream.flush(); outputStream.close(); }
的事情是,我说3例在Windows/Linux上的桌面浏览器中运行良好,惟未对铬和原生浏览器android设备。