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

 

 

你可能感兴趣的:(java 发布流文件和读取流文件)