spring - APK file cannot be downloaded completely in android browser ,but can be download successfully in PC from my same web server? -
question:
apk file can not downloaded android browser, can downloaded @ pc's browser. actually, apk file has 5.9 mb, can downloaded 1.2kb in total. therefore, got 'analyzed failed' error.
web server: linux + tomcat 7.x + jdk1.7 , , set apk mime type in tomcat server web.xml.
web app: spring 4.0.2 + spring mvc + mybatis,
test link: http://127.0.0.1:8080/testapk/appstore/download
download function:
@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); }
i solved following bradford200's advice. think reason had not add annotation of produces="application/apk
, or reason had not add other headers, , new code @ below:
@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); }
Comments
Post a Comment