一、發短信(附加從URL讀數據流)
php
new Thread(new Runnable() { @Override public void run() { URL url; InputStreamReader in; BufferedReader br; try { url = new URL("https://quanapi.sinaapp.com/fetion.php?u=15249040332&p=08121319&to=15249040332&m="+ sendms); while (true) { in = new InputStreamReader(url.openStream()); br = new BufferedReader(in); String s = br.readLine(); JSONObject jso = JSONObject.fromObject(s); if (jso.get("result").toString() == 0 + "") { break; } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start();
二、向servlet直接發數據,收數據
java
收數據:api
ServletInputStream inputStream = ((ServletRequest) request).getInputStream();app
byte[] buffer = new byte[1024];ide
try {url
FileOutputStream outputStream = new FileOutputStream(ftest,true);spa
while ((len = inputStream.readLine(buffer, 0, 1024)) != -1) {orm
offset += len;get
outputStream.write(buffer, 0, len);input
System.out.println(5);
}
outputStream.flush();
outputStream.close();
}
發數據:
url = new URL(surl);
URLConnection urlc=url.openConnection();
urlc.setDoOutput(true);
urlc.connect();
BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(urlc.getOutputStream()));
String str="20.0 50";
bfw.write(str);
bfw.newLine();
bfw.write(str);
bfw.newLine();
bfw.flush();
bfw.close();
BufferedReader bfr=new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String s;
while((s=bfr.readLine())!=null){
System.out.println(s);
}
bfr.close();