[英]APK file cannot be downloaded completely in android browser ,but can be download successfully in PC from my same web server?
題:
我的apk文件無法從任何Android瀏覽器中完全下載,但可以在PC瀏覽器中成功下載。 實際上,我的apk文件有5.9 MB,但總共只能下載1.2KB。 因此,我收到了“分析失敗”錯誤。
Web服務器: linux + tomcat 7.x + jdk1.7,它是在tomcat服務器web.xml中設置的apk mime類型。
Web應用程序: spring 4.0.2 + spring mvc + mybatis,
測試鏈接: http://127.0.0.1:8080/testapk/appstore/download
: http://127.0.0.1:8080/testapk/appstore/download
: http://127.0.0.1:8080/testapk/appstore/download
下載功能 :
@RequestMapping(value = "/appstore/download", method = RequestMethod.GET)
public ResponseEntity<byte[]> download() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//Linux env.
File file = new File("/usr/appstore/test.apk");
if (!file.exists()) {
//test env. windows
file = new File("D:/test.apk");
if(!file.exists()){
throw new FileNotFoundException("Oops! can not find app file.");
}
}
String fileName = FilenameUtils.getName(file.getAbsolutePath());
//
fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
headers.setContentDispositionFormData("attachment", fileName);
//
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
headers, HttpStatus.CREATED);
}
我按照Bradford200的建議解決了。 我認為原因是我沒有添加produces="application/apk
的注釋,或者原因是我沒有添加其他標頭,以及下面的新代碼:
@RequestMapping(value = "/appstore/download", method = RequestMethod.GET, produces="application/apk")
public ResponseEntity<InputStreamResource> download() throws IOException {
File file = new File("/usr/appstore/test.apk");
if (!file.exists()) {
file = new File("D:/test.apk");
if(!file.exists()) {
throw new FileNotFoundException("Oops! File not found");
}
}
InputStreamResource isResource = new InputStreamResource(new FileInputStream(file));
FileSystemResource fileSystemResource = new FileSystemResource(file);
String fileName = FilenameUtils.getName(file.getAbsolutePath());
fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.setContentLength(fileSystemResource.contentLength());
headers.setContentDispositionFormData("attachment", fileName);
return new ResponseEntity<InputStreamResource>(isResource, headers, HttpStatus.OK);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.