网络流写入文件中

来源:互联网 发布:linux开机自检 编辑:程序博客网 时间:2024/05/10 00:53
/**
* Save XML to local

* @throws Exception
*/
protected void saveDataSourceFile(String fileName, String urlAddress)
throws Exception {
// DNS setting
// java.security.Security.setProperty("networkaddress.cache.negative.ttl", "0");
// java.security.Security.setProperty("networkaddress.cache.ttl", "0");
// System.setProperty("sun.net.inetaddr.ttl", "0");
// System.setProperty("sun.net.inetaddr.negative.ttl", "0");
int byteLength = 0;
byte[] input_buffer = new byte[BUF_SIZE];
BufferedOutputStream outStream = null;
InputStream inStream = null;
try {
outStream = new BufferedOutputStream(new FileOutputStream("Mark.txt"), BUF_SIZE);
URLConnection urlCon = null;
URL url = new URL(locationUrl);
urlCon = url.openConnection();
urlCon.setConnectTimeout((int) 100000);
urlCon.setReadTimeout((int) 100000);
inStream = urlCon.getInputStream();
while ((byteLength = inStream.read(input_buffer, 0, BUF_SIZE)) > 0) {
outStream.write(input_buffer, 0, byteLength);
}
// Close streams.
outStream.flush();
} finally {
if (outStream != null) {
outStream.close();
}
if (inStream != null) {
inStream.close();
}
}
}
原创粉丝点击