JAVA下载网上文件到本地

public static void writeFile(String strUrl, String filePath, String fileName){
try {
URL url = new URL(strUrl);
InputStream is = url.openStream();
File f = new File(filePath);
f.mkdirs();

OutputStream os = new FileOutputStream(filePath + fileName);

int bytesRead = 0;
byte[] buffer = new byte[8192];

while((bytesRead = is.read(buffer,0,8192)) != -1){
os.write(buffer, 0, bytesRead);
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

你可能感兴趣的:(java)