首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-10-11 
java 发布流文件和读取流文件??File file new File(E:\\assets\\picture\\1-1.png)InputStream a n

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"));//变成集合

?

?

热点排行