java如何从Content-Disposition获取文件名的正则表达式
一,主要是关于 : post请求下载文件,如何从Content-Disposition获取文件名的正则表达式
记录:
HttpResponse httpResponse = httpRequest.execute();
byte[] bytes = httpResponse.bodyBytes(); String header = httpResponse.header("Content-Disposition"); //Content-Disposition内容: Cookie;filename=7aqq单KgjzeLL3T1a.zip Pattern pattern = Pattern.compile(".*filename=\"?([^\"]+)\"?.*$"); Matcher matcher = pattern.matcher(header); String fileName = ""; if (matcher.matches()) { fileName = matcher.group(1); } //有可能有乱码,处理一下: fileName = URLDecoder.decode(fileName, StandardCharsets.UTF_8);
IoUtil.write(new FileOutputStream("D:\\测试\\"+fileName),true,bytes);
取别的参数也可以:
String headerLine = " Cookie;file=7aqq单KgjzeLL3T1a.zip";
1. String regex ="(?i)^.*;" + paramName + "=\"?([^\"]+)\"?.*$";
(paramName应该是filename的name或任何您想获取的东西)。然后2
2. headerLine.replaceFirst(regex,"$1");
本文来自博客园,作者:青城杂文录,转载请注明原文链接:https://www.cnblogs.com/chenyq/p/17789423.html