Я пытаюсь загрузить файл с помощью браузера на Android-мобиле из моего веб-приложения (в моем случае я использую весенний mvc, несмотря на это, потому что я думаю, что это общая проблема,). Дело в том, что при попытке использовать хром или собственный браузер в Android-устройстве загрузка файла никогда не заканчивается, всегда появляется In progress
или queue
(в зависимости от версии для Android, я пробую с 4.1.1 и 5.1.1) в приложении для загрузки.Скачать файл с HTTPS в браузере на android
Я пытаюсь изменения типичный http-headers
участие: Content-Type
, Content-Disposition
с инлайн или приложением, указав Content-Length
не повезло.
Дело в том, что при использовании настольных браузеров в Windows или Linux файлы загружаются правильно в каждом случае.
Некоторые из моих попыток:
Использование
org.springframework.http.ResponseEntity
:@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"
на@RequestMapping
:@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-устройства.