private static void downfilewithname(String cookies,String urlString,String path) throws IOException {
URL url = new URL(urlString);
URLConnection con = url.openConnection();
con.setRequestProperty("Cookie", cookies);
String fieldValue = con.getHeaderField("Content-Disposition");
String filename = fieldValue.substring(fieldValue.indexOf("fileName=") + 9, fieldValue.length() );
File filetemp = new File(path);
if(!filetemp.exists()){
filetemp.mkdirs();
}
File download = new File(path, new String(filename.getBytes("ISO8859-1"),"gb2312"));
if(download.exists()){ //如果文件存在,则不下载
return;
}
ReadableByteChannel rbc = Channels.newChannel(con.getInputStream());
FileOutputStream fos = new FileOutputStream(download);
try {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} finally {
fos.close();
}
}
URLConnection下载文件,header中获取文件名称,模拟登录
最新推荐文章于 2023-10-31 16:05:38 发布