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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值