当前位置:操作系统 > 安卓/Android >>

Android中图片的上传和下载

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

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

客户端代码:       

[java] <span style="font-size:16px;">    File file = new File(imageUrl);  
        String httpUrl = httpDomain+"AddImageServlet"+"?gid="+gid;  
        HttpPost request = new HttpPost(httpUrl);   
        HttpClient httpClient = new DefaultHttpClient(); 
        FileEntity entity = new FileEntity(file,"binary/octet-stream"); 
        HttpResponse response; 
try { 
            request.setEntity(entity); 
            entity.setContentEncoding("binary/octet-stream"); 
            response = httpClient.execute(request); 
             
//如果返回状态为200,获得返回的结果  
 if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){   
……//图片上传成功             


catch(Exception e){  

</span> 
<span style="font-size:16px;"> File file = new File(imageUrl); 
  String httpUrl = httpDomain+"AddImageServlet"+"?gid="+gid;
        HttpPost request = new HttpPost(httpUrl); 
        HttpClient httpClient = new DefaultHttpClient();
        FileEntity entity = new FileEntity(file,"binary/octet-stream");
      HttpResponse response;
try {
   request.setEntity(entity);
   entity.setContentEncoding("binary/octet-stream");
      response = httpClient.execute(request);
   
//如果返回状态为200,获得返回的结果
 if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ 
……//图片上传成功          
}
}
catch(Exception e){
}
</span>

 


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

服务器端代码:

 

[java] <span style="font-size:16px;">//获得新闻id  
        String gid = request.getParameter("gid");        
        String filePath = getRealPath(request) + "\\userpic\\";  
        //      定义上载文件的最大字节   
        int MAX_SIZE = 102400 * 102400;          
        //      声明文件读入类   
        DataInputStream in = null;  
        FileOutputStream fileOut = null;             
        //      取得客户端上传的数据类型   
        String contentType = request.getContentType();               
        if(contentType.indexOf("binary/octet-stream") >= 0){  
            //      读入上传的数据   
            in = new DataInputStream(request.getInputStream());  
            int formDataLength = request.getContentLength();  
            //  如果图片过大  
            if(formDataLength > MAX_SIZE){  
                String errormsg=("上传的文件字节数不可以超过" + MAX_SIZE); 
                out.println(errormsg); 
                return ; 
            }  
        //      保存上传文件的数据   
        byte dataBytes[] = new byte[formDataLength];  
        int byteRead = 0;  
        int totalBytesRead = 0;  
        //      上传的数据保存在byte数组   
        while(totalBytesRead < formDataLength){  
        byteRead = in.read(dataBytes,totalBytesRead,formDataLength);  
        totalBytesRead += byteRead;  
          }  
        String fileName = filePath + gid+".png"; 
         //     检查上载文件的目录是否存在   
        File fileDir = new File(filePath);  
        if(!fileDir.exists()){  
        fileDir.mkdirs();  
        } 
        //      创建文件的写出类   
        fileOut = new FileOutputStream(fileName);  
        //      保存文件的数据   
        fileOut.write(dataBytes);  
        fileOut.close(); 
         
        //保存文件的路径名  
……   
</span> 
<span style="font-size:16px;">//获得新闻id
  String gid = request.getParameter("gid"

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,