android客户端从服务器端下载文件,服务端返回文件流(文件不在项目里)
来源:互联网 发布:少儿编程教育 编辑:程序博客网 时间:2024/06/06 00:36
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;
}
- android客户端从服务器端下载文件,服务端返回文件流(文件不在项目里)
- 多客户端实现文件从服务器端下载文件--并行下载
- 服务端客户端文件下载
- (下载、打开)从服务器端将文件传送到客户端
- 客户端从服务端下载文件的流程分析
- Java客户端下载服务器端的文件
- 多线程文件下载的服务器端及客户端
- TCP编程例三:从客户端发送文件给服务器端,服务器端保存到本地,并返回“发送成功”给客户端。
- jsp中从服务端下载文件
- android文件上传客户端与服务器端代码
- springmvc服务端+android客户端的文件上传
- Android 文件上传(客户端+服务端)
- Android文件下载含服务器端(客户端 UI界面异步请求部分)三
- Android文件下载含服务器端(服务器端)一
- 批量下载图片文件(服务器端 和 客户端)
- 基于cs实现客户端与服务器端的文件下载
- [java]客户端从服务器下载文件
- 使用 java Socket 从客户端向服务器端传文件
- 架构之美第十三章-美丽的架构
- c++预处理器定义的四个有用的常量
- 查询数据时候等待效果
- yii jquery折叠、弹对话框、拖拽、滑动条、ol和ul列表、局部内容切换
- 百度竞价如何理清思路?一张图让你明白!
- android客户端从服务器端下载文件,服务端返回文件流(文件不在项目里)
- java内部类
- JSTL标签的用法
- sencha touch :Ext.list 使用方法
- Qt的皮肤设计(Style Sheet)
- OPENGL标点问题的解决
- Qt QPainter绘图原理
- java反射机制的两个应用
- Sencha Touch 用scroller实现滚到底部加载更多