1、发短信(附加从URL读数据流)

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();

2、向servlet直接发数据,收数据

收数据:

ServletInputStream inputStream = ((ServletRequest) request).getInputStream();

byte[] buffer = new byte[1024];

try {

    FileOutputStream outputStream = new FileOutputStream(ftest,true);

    while ((len = inputStream.readLine(buffer, 0, 1024)) != -1) {

            offset += len;

            outputStream.write(buffer, 0, len);

            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();