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");


 

posted @ 2023-10-26 14:56  青城杂文录  阅读(660)  评论(0编辑  收藏  举报