Java实现HTTP文件下载
- 摘要:实现本地下载http文件,/***包为java.net.**下载图片*@paramsourceFile文件源路径*@paramtargetFile下载存放路径*@throwsIOException*/publicstaticvoidcopyFile(StringsourceFile,StringtargetFile)throwsIOException{try{URLurl=newURL(sourceFile);HttpURLConnectionhttpUrl=
- 标签:实现 文件 Java 下载 HTTP
实现本地下载http文件,
/**
* 包为java.net.*
* 下载图片
* @param sourceFile 文件源路径
* @param targetFile 下载存放路径
* @throws IOException
*/
public static void copyFile(String sourceFile, String targetFile) throws IOException {
try {
URL url = new URL(sourceFile);
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());
FileOutputStream fos = new FileOutputStream(targetFile);
int size = 0;
byte[] buf = new byte[8096];//此值可以调
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
fos.close();
bis.close();
httpUrl.disconnect();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}