android 获取服务器文件流 hander

简介: 引用:http://blog.csdn.net/join_wangzi/article/details/7701556 http://www.cnblogs.com/quanjia/archive/2010/11/01/1866753.

引用: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;
}
相关文章
|
PHP Android开发
android通过http上传文件,服务器端用php写(原创)
android通过http上传文件,服务器端用php写(原创)
153 4
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
210 2
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
Android开发 数据安全/隐私保护 虚拟化
安卓手机远程连接登录Windows服务器教程
安卓手机远程连接登录Windows服务器教程
2635 5
|
Ubuntu Linux Android开发
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
本文介绍了如何在Android设备上安装Termux和AnLinux,并通过这些工具运行Ubuntu系统和桌面环境。
2691 3
termux+anlinux+Rvnc viewer来使安卓手机(平板)变成linux服务器
|
前端开发 Java API
Android端通过HttpURLConnection上传文件到服务器
Android端通过HttpURLConnection上传文件到服务器
271 0
|
Ubuntu Android开发 数据安全/隐私保护
【Android平板编程】远程Ubuntu服务器Code-Server编程写代码
【Android平板编程】远程Ubuntu服务器Code-Server编程写代码
351 0
|
2月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
276 10
|
2月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
2月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
419 8

热门文章

最新文章