读书人

java 公布流文件和读取流文件

发布时间: 2012-10-11 10:16:10 作者: rapoo

java 发布流文件和读取流文件

?

?

File file = new File("E:\\assets\\picture\\1-1.png");

InputStream a = new FileInputStream(file);

byte[] bbb = ?new byte[a.available()];

a.read(bbb);

?

URL url = new URL(url);

HttpURLConnection httpurl = (HttpURLConnection) url.openConnection();

?

?

httpurl.setDoOutput(true);

OutputStream os = httpurl.getOutputStream();

os.write(bbb);

os.flush();

os.close();

int code = httpurl.getResponseCode();?

System.out.println(code);

?

?

//读取json流文件

?

URL url = new URL(url);//url资源地址

HttpURLConnection httpURL = (HttpURLConnection) url.openConnection();

InputStream is = httpURL.getInputStream();

if(httpURL.getResponseCode()!=200){

//exception

}

byte[] b = new byte[1];

String str = "";

while(is.read(b)!=-1){

str += new String(b,"utf-8");

}

//第二种

?

URL url = new URL(url);

InputStream is = url.openStream();

byte[] b = new byte[1];

String str = "";

while(is.read(b)!=-1){

str += new String(b,"utf-8");

}

?

//转json集合

?

JSONArray jsonArray=new JSONArray();

jsonArray=jsonArray.fromObject(str);

list=(List<User>)JSONArray.toList(jsonArray,User.class);

?

//获取单个对象

?

System.out.println(str);

JSONObject json = JSONObject.fromObject(str);

System.out.println("a"+json.get("userName"));

System.out.println("b"+JSONObject.fromObject(json.get("list")));//获取list字段的集合,变成对象

List list=JSONArray.fromObject(json.get("list"));//变成集合

?

?

读书人网 >编程

热点推荐