//此方法只能用HTTP协议
//保存文件到本地
//Url是文件下载地址,fileName 为一个全名(路径+文件名)文件
URL url
DataOutputStream out = null
DataInputStream in = null
try {
url = new URL(Url)
HttpURLConnection connection = (HttpURLConnection) url.openConnection()
in = new DataInputStream(connection.getInputStream())
out = new DataOutputStream(new FileOutputStream(fileName))
byte[] buffer = new byte[4096]
int count = 0
while ((count = in.read(buffer)) >0) {
out.write(buffer, 0, count)
}
}catch (Exception e) {
e.printStackTrace()
}finally{
try {
if(out != null){
out.close()
}
if(in != null){
in.close()
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
java用输入输出流读取文件的时候应该没有关闭输入输出流,这样的话文件就会一直被占用, 重启后肯定就可以再次访问了。 一般java中io *** 作:InputStream is = null
try{
一些io *** 作
}catch(){}finally{
is.close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)