从服务器下载文件,读内容到buffer中

来源:互联网 发布:qq飞车软件下载 编辑:程序博客网 时间:2024/06/15 13:15

public class DownloadFileTask {
public File getFile(String path,String filepath) throws Exception{
URL url=new URL(path);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod(“GET”);
if ((conn.getResponseCode()==200)) {
InputStream is=conn.getInputStream();
File file=new File(filepath);
FileOutputStream fos=new FileOutputStream(filepath);
byte[] buf=new byte[1024];
int len=0;
while ((len=is.read(buf))!=-1) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
is.close();
return file;
}
return null;
}
}

0 0