URL对象代表统一资源定位器,它是指向互联网”资源”的指针,资源可以是简单的文件或目录,也可以是对更复杂的对象的引用,URL可以由协议名、主机、端口和资源组成。
URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和URL之间的通信连接,程序可以通过URLConnection实例向该URL发送请求,读取URL引用的资源。
通常创建一个和URL的连接,并发送请求。读取此URL引用的资源需要如下几个步骤:
1) 通过调用Url对象openConnection()方法来创建URLConnection对象
2) 设置URLConnection的参数和普通请求属性
3) 如果只是发送get方式请求,使用Connect方法建立和远程资源之间的实际连接即可;如果需要发送post方式的请求需要获取URlConnection实例对应的输出流来发送请求参数
4) 远程资源变为可用,程序可以访问远程资源的头字段或通过输入流读取远程资源的数据。
J2SE实现网络图片的获取
importjava.io.ByteArrayOutputStream; importjava.io.File; importjava.io.FileOutputStream; importjava.io.InputStream; importjava.net.HttpURLConnection; importjava.net.URL; publicclass NetTest { public static void main(String[] args)throws Exception { Stringpath="http://pic2.nipic.com/20090505/2186940_203750018_2.jpg"; URL url = new URL(path); HttpURLConnection conn =(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); InputStream inStream =conn.getInputStream(); ByteArrayOutputStream bos = newByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len=0; while((len=inStream.read(buffer))!= -1){ bos.write(buffer,0,len); } byte[] data = bos.toByteArray(); File file = newFile("picture.jpg"); FileOutputStream fos = newFileOutputStream(file); fos.write(data); fos.close(); System.out.println("finish......."); } }
Androd中获取网络图片
资源文件:
<string name="btn_text">显示网络图片</string> <string name="error">下载图片失败!!</string>
布局文件:
<Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/btn_text" android:id="@+id/showImageBtn" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" />
添加ImageService类
packagecn.class3g.service; importjava.io.ByteArrayOutputStream; importjava.io.InputStream; importjava.net.HttpURLConnection; importjava.net.MalformedURLException; importjava.net.URL; publicclass ImageService { public static byte[] getImageData(Stringpath) throws Exception { URL url = new URL(path); HttpURLConnection conn =(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); InputStream inStream =conn.getInputStream(); ByteArrayOutputStream bos = newByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len=0; while((len=inStream.read(buffer)) != -1){ bos.write(buffer,0,len); } byte[] data = bos.toByteArray(); return data; } } ShowImageActivity类 packagecn.class3g.net; importcn.class3g.service.ImageService; importandroid.app.Activity; importandroid.graphics.Bitmap; importandroid.graphics.BitmapFactory; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.ImageView; importandroid.widget.Toast; publicclass ShowImageActivity extends Activity implements OnClickListener { Button btn; ImageView imgView; public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.img_layout); findViews(); } private void findViews() { btn = (Button)this.findViewById(R.id.showImageBtn); imgView = (ImageView)this.findViewById(R.id.imageView); btn.setOnClickListener(this); } @Override public void onClick(View v) { Stringpath="http://pic2.nipic.com/20090505/2186940_203750018_2.jpg"; byte data[]; try { data =ImageService.getImageData(path); Bitmap titmap =BitmapFactory.decodeByteArray(data, 0, data.length); imgView.setImageBitmap(titmap); } catch (Exception e) { // e.printStackTrace(); Log.e("TAG",e.toString()); Toast.makeText(this,R.string.error, Toast.LENGTH_SHORT).show(); } } }