作者:余蒙


在实现一个Android的WEB服务客户端,比如微博,论坛客户端时,经常会使用到图片的上传和下载。在这里介绍如何利用HttpClient实现图片的上传和下载功能。

1 图片上传:上传图片时,首先获得图片的路径,创建文件,并将图片转化为字节流写入到request,并发送该请求。

客户端代码:

[java] view plain copy
  1. <spanstyle="font-size:16px;">Filefile=newFile(imageUrl);
  2. StringhttpUrl=httpDomain+"AddImageServlet"+"?gid="+gid;
  3. HttpPostrequest=newHttpPost(httpUrl);
  4. HttpClienthttpClient=newDefaultHttpClient();
  5. FileEntityentity=newFileEntity(file,"binary/octet-stream");
  6. HttpResponseresponse;
  7. try{
  8. request.setEntity(entity);
  9. entity.setContentEncoding("binary/octet-stream");
  10. response=httpClient.execute(request);
  11. //如果返回状态为200,获得返回的结果
  12. if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
  13. ……//图片上传成功
  14. }
  15. }
  16. catch(Exceptione){
  17. }
  18. </span>


服务器端所做的工作则是接收该字节流,写入文件中,并在服务器中相应文件夹中保存该文件,并记录该文件的路径,将图片文件路径写入到数据库中保存。

服务器端代码:


[java] view plain copy
  1. <spanstyle="font-size:16px;">//获得新闻id
  2. Stringgid=request.getParameter("gid");
  3. StringfilePath=getRealPath(request)+"\\userpic\\";
  4. //定义上载文件的最大字节
  5. intMAX_SIZE=102400*102400;
  6. //声明文件读入类
  7. DataInputStreamin=null;
  8. FileOutputStreamfileOut=null;
  9. //取得客户端上传的数据类型
  10. StringcontentType=request.getContentType();
  11. if(contentType.indexOf("binary/octet-stream")>=0){
  12. //读入上传的数据
  13. in=newDataInputStream(request.getInputStream());
  14. intformDataLength=request.getContentLength();
  15. //如果图片过大
  16. if(formDataLength>MAX_SIZE){
  17. Stringerrormsg=("上传的文件字节数不可以超过"+MAX_SIZE);
  18. out.println(errormsg);
  19. return;
  20. }
  21. //保存上传文件的数据
  22. bytedataBytes[]=newbyte[formDataLength];
  23. intbyteRead=0;
  24. inttotalBytesRead=0;
  25. //上传的数据保存在byte数组
  26. while(totalBytesRead<formDataLength){
  27. byteRead=in.read(dataBytes,totalBytesRead,formDataLength);
  28. totalBytesRead+=byteRead;
  29. }
  30. StringfileName=filePath+gid+".png";
  31. //检查上载文件的目录是否存在
  32. FilefileDir=newFile(filePath);
  33. if(!fileDir.exists()){
  34. fileDir.mkdirs();
  35. }
  36. //创建文件的写出类
  37. fileOut=newFileOutputStream(fileName);
  38. //保存文件的数据
  39. fileOut.write(dataBytes);
  40. fileOut.close();
  41. //保存文件的路径名
  42. ……
  43. </span>


2 图片下载:首先获得网络图片的图片地址,发送请求后,服务器将会返回该图片的字节流,利用BitmapFactory.decodeStream()方法将字节流转化为图片并返回。具体代码如下:


[java] view plain copy
  1. <spanstyle="font-size:16px;">//获得网络中的图片
  2. publicBitmapgetGossipImage(Stringgid){
  3. StringhttpUrl=httpDomain+"userpic/"+gid+".png";
  4. Bitmapbitmap=null;
  5. HttpGethttpRequest=newHttpGet(httpUrl);
  6. //取得HttpClient对象
  7. HttpClienthttpclient=newDefaultHttpClient();
  8. try{
  9. //请求httpClient,取得HttpRestponse
  10. HttpResponsehttpResponse=httpclient.execute(httpRequest);
  11. if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
  12. //取得相关信息取得HttpEntiy
  13. HttpEntityhttpEntity=httpResponse.getEntity();
  14. InputStreamis=httpEntity.getContent();
  15. bitmap=BitmapFactory.decodeStream(is);
  16. is.close();
  17. }else{
  18. Toast.makeText(context,"连接失败!",Toast.LENGTH_SHORT).show();
  19. }
  20. }catch(ClientProtocolExceptione){
  21. e.printStackTrace();
  22. }catch(IOExceptione){
  23. e.printStackTrace();
  24. }
  25. returnbitmap;
  26. }
  27. </span>


更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android8.0.0-r4系统的启动过程
  6. Android中资源文件夹res/raw和assets的使用(续)——分割文件以及合
  7. android中如何利用attrs和styles定义控件
  8. 基于Android(安卓)Webview的Hybrid App开发的前端优化
  9. RN 与 Android(安卓)代码交互

随机推荐

  1. android webview 中处理网页中的400、404
  2. Android电池电量更新 - BatteryService
  3. android经常调用的组件
  4. android启动其他应用的一段代码
  5. Android(安卓)Activity生命周期
  6. 今天玩玩Android(安卓)-==-- 了解一下
  7. 解决Conversion to Dalvik format failed
  8. android 开源自组织网络开源包
  9. 编译android 64位openssl库
  10. Android-Intent的使用方法详解