java网络编程URLConnection和HttpClient(下载图片范例)

一、用HttpClient下图片:

 protected Bitmap doInBackground(String... params) {
               //  完成对图片下载的功能
               Bitmap bitmap=null;
               ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
               InputStream inputStream=null;
               try {
                    HttpClient httpClient=new DefaultHttpClient();
                    HttpGet httpGet=new HttpGet(params[0]);
                    HttpResponse httpResponse=httpClient.execute(httpGet);
                    if(httpResponse.getStatusLine().getStatusCode()==200){
                          inputStream=httpResponse.getEntity().getContent();
                          //先要获得文件的总长度
                          long file_length=httpResponse.getEntity().getContentLength();
                          int len=0;
                          byte[] data=new byte[1024];//读取
                          int total_length=0;
                          while((len=inputStream.read(data))!=-1){
                        	  System.out.println(total_length);
                                total_length += len;
                                
                                int value=(int) ((total_length / (float) file_length) * 100);
                                publishProgress(value);//把刻度发布出去
                                outputStream.write(data, 0, len);//写入
                          }
                          byte[] result=outputStream.toByteArray();//声明字节数组
                          bitmap=BitmapFactory.decodeByteArray(result, 0, result. length);
                    }
               } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
               }finally{
                    if(inputStream!=null){
                          try {
                                inputStream.close();
                          } catch (IOException e) {
                                e.printStackTrace();                       
                          }
                    }
               }
               return bitmap;
         }

二、用URLConnection下载图片


public void run (){
				 URL urll;
					try {
						urll = new URL("http://www.91ri.org/9408.html");
						HttpURLConnection conn =  (HttpURLConnection) urll.openConnection();     
				        conn.setConnectTimeout(5000);     
				         //获取到文件的大小       
				            
				        int total_length = conn.getContentLength();
				        
				        //pd.setMax(conn.getContentLength());
				        
				         InputStream is = conn.getInputStream();     
				         File file = new File(Environment.getExternalStorageDirectory(), "aupdata.html");     
				        FileOutputStream fos = new FileOutputStream(file);     
				         BufferedInputStream bis = new BufferedInputStream(is);     
				         byte[] buffer = new byte[1024];     
				         int len ;     
				        int total=0;  
				        while((len = bis.read(buffer)) != -1){
				        	fos.write(buffer, 0, len);
				        	total = total + len ;
				        	System.out.println(total);
				        	
				        	Thread.sleep(1000);
				        	
				        	progress_num = (100*total)/total_length ;
				        	System.out.println("pro" + progress_num + "gg" + total_length);
				        	//progress_num = total;
				        	progress_handler.sendEmptyMessage(0x123);
				        	//pd.setProgress(total);
				        }
				        
				         
				           fos.close();     
				           bis.close();     
				           is.close(); 


二者的比较,请看以下链接

http://blog.csdn.net/zealot_2002/article/details/8250268

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值