java下载图片
public void saveToFile(String destUrl, String fileName) {
??? ???
??? ??? FileOutputStream fos = null;
??? ??? BufferedInputStream bis = null;
??? ??? HttpURLConnection httpUrl = null;
??? ??? URL url = null;
??? ??? byte[] buf = new byte[BUFFER_SIZE];
??? ??? int size = 0;
??? ??? // 建立链接
??? ??? try {
??? ??? ??? url = new URL(destUrl);
??? ??? ??? httpUrl = (HttpURLConnection) url.openConnection();
??? ??? ??? // 连接指定的资源
??? ??? ??? httpUrl.setRequestMethod("GET");
??? ??? ??? httpUrl.connect();
??? ??? ??? // 获取网络输入流
??? ??? ??? bis = new BufferedInputStream(httpUrl.getInputStream());
??? ??? ??? // 建立文件
??? ??? ??? fos = new FileOutputStream(fileName);
??? ??? ??? // if (this.DEBUG)
//??? ??? ??? System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件["
//??? ??? ??? ??? ??? + fileName + "]");
??? ??? ??? System.out.println("IMG IS url [" + destUrl + "] file save in ["
??? ??? ??? ??? ??? + fileName + "]");
??? ??? ??? // 保存文件
??? ??? ??? while ((size = bis.read(buf)) != -1)
??? ??? ??? ??? fos.write(buf, 0, size);
??? ??? } catch (IOException e) {
??? ??? ??? e.printStackTrace();
??? ??? } finally {
??? ??? ??? try {
??? ??? ??? ??? if (fos != null) {
??? ??? ??? ??? ??? fos.close();
??? ??? ??? ??? }
??? ??? ??? ??? if (bis != null) {
??? ??? ??? ??? ??? bis.close();
??? ??? ??? ??? }
??? ??? ??? ??? httpUrl.disconnect();
??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? }
??? }
?
?
?
?
------destUrl表示图片URL??? fileName表示图片保存地址和名称