标签:校验 operation 获取 工具类 track 组成 打开 回调接口 官方
利用JSON实现网络下载package org.download.photo;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadUtil {
// JSON字符串下载
public static String downloadJSON(String path) {
URL url = null;
InputStream input = null;
HttpURLConnection conn = null;
byte date[] = null;
try {
url = new URL(path);// 1.将网址封装到URL中
conn = (HttpURLConnection) url.openConnection();// 2.打开http连接
conn.connect();// 3.开始连接
input = conn.getInputStream();// 4.获取网络输入流,用于读取要下载内容
ByteArrayOutputStream bos = new ByteArrayOutputStream();// 5.获取字节转换流
byte[] b = new byte[1024];
int len = 0;
while ((len = input.read(b)) != -1) {
bos.write(b, 0, len);
}
date = bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(date);
}
//下载图片
public static void downloadPhoto(String photo,String dest) {
URL url = null;
InputStream input = null;
HttpURLConnection conn = null;
OutputStream out=null;
byte date[] = null;
try {
out=new FileOutputStream(dest);
url = new URL(photo);// 1.将网址封装到URL中
conn = (HttpURLConnection) url.openConnection();// 2.打开http连接
conn.connect();// 3.开始连接
input = conn.getInputStream();// 4.获取网络输入流,用于读取要下载内容
ByteArrayOutputStream bos = new ByteArrayOutputStream();// 5.获取字节转换流
byte[] b = new byte[1024];
int len = 0;
while ((len = input.read(b)) != -1) {
bos.write(b, 0, len);
}
date = bos.toByteArray();
out.write(date);
System.out.println(dest+"图片下载成功,以写入本地磁盘..");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
input.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
下载测试类(主方法):
package org.download.photo;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Downloadtest {
public static void main(String[] args) {
// 1.下载JSON字符串
String path = "http://open.qyer.com/qyer/recommands/index?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=860806022766611&track_app_version=5.1.1&track_app_channel=360m&track_device_info=mx2&track_os=Android4.2.1&track_user_id=&app_installtime=1404882938883%20HTTP/1.1";
String jsonStr = DownloadUtil.downloadJSON(path);
// System.out.println(jsonStr);//测试JSON字符串是否下载成功
// 2.解析JSON字符串,获取到photo数据(此处使用官方解析)
try {
// 3.将JSON字符串转化为java对象
JSONObject jsonObj = new JSONObject(jsonStr);
// 4.根据key值获取value值
JSONObject jsonData = jsonObj.optJSONObject("data");// 先获取data对象(一级目录)
JSONArray jsonPlace = jsonData.optJSONArray("place");// 再获取数组形式的place(二级目录)
// 数组形式的jsonPlace,获取photo数据
for (int i = 0; i < jsonPlace.length(); i++) {
JSONObject jsonOb = jsonPlace.optJSONObject(i);// 获取数组中的各个对象
String photoStr = jsonOb.optString("photo");// 获取其中的photo字符串
String[] picArray = photoStr.split("/");
// 5.开始下载图片,并为其命名
DownloadUtil.downloadPhoto(photoStr, "e:" + File.separator
+ "myphoto" +File.separator+ picArray[picArray.length - 1]);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
(4)运行结果:
例2(下载网络图片,不用JSON解析,建立下载接口,回调接口):
(1)编写代码:
下载接口:
package org.download.nettu;
//回调接口
public interface Load {
void load(byte[] b);
}
下载方法类:
package org.download.nettu;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadUtilPic {
//图片
public static void downloadPhoto(String path,Load load){
//1.将网址封装到URL中
URL url=null;
InputStream input=null;
HttpURLConnection conn=null;
try {
url=new URL(path);
conn=(HttpURLConnection) url.openConnection(); //获取网络连接对象
conn.connect();//开启网络连接
input=conn.getInputStream();//获取网络输入流
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int len=0;
byte[] b=new byte[1024];
while((len=input.read(b))!=-1){
bos.write(b, 0, len);
}
//将下载的数据转换成字节数组
byte[] date= bos.toByteArray();
load.load(date); //回调load方法
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试下载类(主方法):
package org.download.nettu;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class DownloadTest {
public static void main(String[] args) {
String path="http://static.qyer.com/upload/mobile/operation/6e/e1/6ee105f770fe63f625422912b1465e2e.jpg";
//调用下载方法
DownloadUtilPic.downloadPhoto(path, new Load() {
@Override
public void load(byte[] b) {
OutputStream out=null;
try {
out=new FileOutputStream("f:"+File.separator+"abcdgdh.jpg");
BufferedOutputStream bos=new BufferedOutputStream(out);
bos.write(b);
System.out.println("图片下载成功。。");
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
运行结果:
标签:校验 operation 获取 工具类 track 组成 打开 回调接口 官方
原文地址:http://blog.51cto.com/13501268/2129718