当前位置: 代码迷 >> Web前端 >> JAVA上载网下文件到本地
  详细解决方案

JAVA上载网下文件到本地

热度:86   发布时间:2012-10-06 17:34:01.0
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();
}
}

  相关解决方案