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;
}
相关文章
|
1月前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
1月前
|
Ubuntu 网络协议 Java
在Android平板上使用code-server公网远程Ubuntu服务器编程
在Android平板上使用code-server公网远程Ubuntu服务器编程
|
2月前
|
Ubuntu 网络协议 Linux
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
【Linux】Android平板上远程连接Ubuntu服务器code-server进行代码开发
57 0
|
3月前
|
数据采集 编解码 图形学
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
Android平台Unity下如何通过WebCamTexture采集摄像头数据并推送至RTMP服务器或轻量级RTSP服务
104 0
|
3月前
|
安全 网络协议 Linux
【公网远程手机Android服务器】安卓Termux搭建Web服务器
【公网远程手机Android服务器】安卓Termux搭建Web服务器
66 0
|
4月前
|
XML JSON Apache
【Android】如何获得Apache服务器的JSON文件数据
【Android】如何获得Apache服务器的JSON文件数据
61 0
|
4月前
|
安全 网络协议 Linux
【公网远程手机Android服务器】安卓Termux搭建Web服务器
【公网远程手机Android服务器】安卓Termux搭建Web服务器
|
1月前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
1月前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
420 4
|
1月前
|
SQL 弹性计算 安全
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
当我们通过阿里云的活动购买完云服务器之后,并不是立马就能使用了,还需要我们设置云服务器密码,配置安全组等基本操作之后才能使用,有的用户还需要购买并挂载数据盘到云服务器上,很多新手用户由于是初次使用阿里云服务器,因此并不知道这些设置的操作流程,下面给大家介绍下这些设置的具体操作流程。
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程