public void downloadExcelFile() throws Exception {
URL url = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
// 打印HTTP header
Map headers = conn.getHeaderFields();
Setkeys = headers.keySet();
// 转换编码
String contentDisposition = URLDecoder.decode(
conn.getHeaderField("content-Disposition"), "UTF-8");
System.out.println(contentDisposition);
// 匹配文件名
Pattern pattern = Pattern.compile(".*fileName=(.*)");
Matcher matcher = pattern.matcher(contentDisposition);
System.out.println(matcher.groupCount());
System.out.println(matcher.matches());
String filename = contentDisposition.split("=")[1];
// 写盘
File vfile = new File(path);
if (!vfile.exists()) {
vfile.mkdirs();
}
RandomAccessFile file = new RandomAccessFile(path + PreFileName, "rw");
InputStream stream = conn.getInputStream();
byte buffer[] = new byte[1024];
while (true) {
int len = stream.read(buffer);
if (len == -1) {
break;
}
file.write(buffer, 0, len);
}
if (file != null) {
file.close();
}
if (stream != null) {
stream.close();
}
}