从Content-Disposition获取文件名 [英] Get filename from Content-Disposition

查看:6192
本文介绍了从Content-Disposition获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSP将blob文件从HTML表单上传到MySQL数据库。我需要将文件名插入DB。我知道文件名存储在Content-Disposition标题中,我怎么能得到它?提前致谢!

I'm uploading a blob file from HTML form to MySQL database using JSP. I need to insert the filename into DB. I know that the filename is stored in the Content-Disposition header, how could I get that? Thanks in advance!

推荐答案

如果您使用带有 HttpServletRequest.getPart

Part part = request.getPart("xxx"); // input type=file name=xxx
String disposition = part.getHeader("Content-Disposition");
String fileName = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");

参见部分

As @ Marc提到我没有处理URL编码。(他还使文件名周围的引号可选。)

As @Marc mentioned I did not treat URL encoding. (He also made the quotes around the filename optional.)

fileName = URLDecoder.decode(fileName, StandardCharsets.ISO_8859_1);

未选中,但标头的HTTP编码应为默认ISO -8859-1。

Not checked, but HTTP encoding for headers should be the default ISO-8859-1.

这篇关于从Content-Disposition获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆