-------------------------------------------------------------------------------------------------------------------------------
获取某个网页的源代码
URL url = new URL("http://xxx.xxx.com/a.txt");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
InputStream inputStream = connection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String str;
while((str=reader.readLine())!=null){
buffer.append(str);
}
System.out.println(buffer.toString());
-------------------------------------------------------------------------------------------------------------------------------
从网络上的图片声音等文件下载到本地
URL url = new URL(urlstr);
URLConnection connection = url.openConnection();
connection.setReadTimeout(60000);
connection.setDoInput(true);
connection.connect();
byte[] data = new byte[connection.getContentLength()];
inputStream = connection.getInputStream();
int c;
int i=0;
while((c=inputStream.read())!=-1){
data[i]=(byte)c;
i++;
}
File file = new File(path);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(data);
inputStream.close();
outputStream.flush();
outputStream.close();
-----------------------------------------------------------------------------------------------------------------------------------
把中文字符串写成本地文本文件
String string="我是中文";
FileWriter writer = new FileWriter("E:\\aaaa.txt");
writer.write(string);
writer.close();
-----------------------------------------------------------------------------------------------------------------------------------
在程序中,向网络地址发出请求,post方式传送参数
StringBuffer parameters = new StringBuffer();
parameters.append("sender=");
parameters.append("18603829929");
parameters.append("&");
parameters.append("subject=");
parameters.append("我是标题");
parameters.append("&");
parameters.append("images=");
parameters.append("http://www.qq360.cn/uploads/allimg/090623/1K6093141-42.gif");
parameters.append("&");
parameters.append("texts=");
parameters.append("http://www.qq360.cn/uploads/allimg/090623/1K6093141-42.gif");
//System.out.println(parameters.length());
try {
URL url = new URL("http://www.XXX.com/XXX.jsp");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection
.getOutputStream(), "gbk");
out.write(parameters.toString());
out.flush();
out.close();
connection.getInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}