最近在项目里写了个下载功能,在电脑上测试可用。我下载的是apk文件,服务端用的是tomcat.
response.setContentType("application/vnd.android.package-archive") 这个也设置过了。但是手机上访问时,我点下载链接竟然没一点反应。用了安卓手机自带的浏览器 和 qq浏览器 都不能用,只有uc能成功!在网上查查也没找到答案,第一种方法后台是这样写的
File _file = new File( request.getSession().getServletContext().getRealPath(Constants.PATH_ANDROIDPN_FILE) + "\\短息接收.apk"); response.setContentType("application/vnd.android.package-archive");// oper save as 对话框 response.setCharacterEncoding("GBK"); // response.setHeader("Content-Disposition", "attachment;" + "filename=" + new String(gg_fjghwd.getShowname().getBytes(), "ISO-8859-1")); response.setHeader("Content-Disposition", "attachment;" + "filename=" + new String("大嘴巴.apk".getBytes(),"ISO-8859-1")); System.out.println("------1-----"); copyFileToStream(_file, response.getOutputStream());
private void copyFileToStream(File f, OutputStream ostream) throws IOException {
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[BUFFER_SZ];
try {
while (true) {
int bytesReceived = fis.read(buffer);
if (bytesReceived < 1) {
break;
}
ostream.write(buffer, 0, bytesReceived);
}
System.out.println(ostream);
} finally {
fis.close();
}
}
但是用第一种方法后台会报错
ClientAbortException: java.io.IOException
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:358)
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:434)
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:349)
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:381)
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:370)
最后没法了我这样写<a href="<%=strHttp %>${ctx}/uploads/DZB.apk" >1安卓系统</a> 可是还是不能下载!大家帮忙看看
前台现在连接写的有问题,忘记写tagert
怎么解决的呀?