引用:http://blog.csdn.net/join_wangzi/article/details/7701556

http://www.cnblogs.com/quanjia/archive/2010/11/01/1866753.html

android客户端从服务器端下载文件,服务端返回文件流

服务端 strtus: /** * 文件下载 : 返回文件流 * @param mapping * @param actionForm * @param request * @param response * @return * @throws Exception */ public ActionForward fileUpload(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception{ logger.debug("fileUpload()"); RequestMap params = WebUtil.getQueryMap(request); String file_key = (String)params.get("filekey", ""); if(file_key != null && !"".equals(file_key)){ NewsPaperManager service = NewsPaperManager.getInstance(); String file_path = service.getFilePath(file_key); if(file_path != null && !"".equals(file_path)){ File file = new File(file_path); if(file.exists()){ InputStream fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header response.setHeader("Content-Type", "application/octet-stream"); response.addHeader("Content-Disposition", "attachment;filename=" + file.getName()); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = response.getOutputStream(); toClient.write(buffer); toClient.flush(); toClient.close(); } } } return null; } android客户端: public byte[] getImage(String path) throws Exception { URL url = new URL("http://120.197.230.53:9902/kuchuan_api/newspaper.do?method=fileUpload&filekey=2LM7vAcHt8iFX2ABTXZT"); HttpURLConnection conn = (HttpURLConnection)url.openConnection();//基于HTTP协议的连接对象 conn.setConnectTimeout(5000);//请求超时时间 5s conn.setRequestMethod("GET");//请求方式 Log.i("ResponseCode", "prepare"); Log.i("ResponseCode", conn.getResponseCode()+""); if(conn.getResponseCode() == 200){//响应码==200 请求成功 InputStream inputStream = conn.getInputStream();//得到输入流 Log.i("ResponseCode", inputStream.toString().length()+""); ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; Log.i("ResponseCode", "inputStream.read(buffer) :"+inputStream.read(buffer)+""); while((len = inputStream.read(buffer)) != -1){ arrayOutputStream.write( buffer, 0, len); Log.i("ResponseCode", "buffer.length :"+buffer.length+" " + len); } inputStream.close(); arrayOutputStream.close(); Log.i("ResponseCode", "arrayOutputStream.toByteArray().length :"+arrayOutputStream.toByteArray().length+""); } return null; }

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Android实现服务器登录
  4. Android(安卓)studio 使用Google 的AdMob
  5. Android定制出厂默认输入法
  6. Android学习笔记之mainfest文件中android属性
  7. android资料下载地址汇总
  8. Android(安卓)IOS平台AE动画库
  9. Android下使用Properties文件保存程序设置

随机推荐

  1. Android中点击按钮的事件处理实现步骤
  2. Android中全屏无标题设置
  3. Android(安卓)开发10个常用工具类
  4. android —— 自定义控件 竖向的ViewPage
  5. android-疑问2
  6. Android(安卓)解决AIDL bindService异常
  7. Android(安卓)节点进度条
  8. android 用tcpdump抓取网络包
  9. android知识点积累
  10. ConstraintLayout 学习