Get filename from Content-Disposition [closed]

僤鯓⒐⒋嵵緔 提交于 2019-12-01 17:31:00

问题


I'm uploading a blob file from an HTML form to a 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?


回答1:


If you uploaded the file using JavaEE 6 with 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");

See Part.


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

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



来源:https://stackoverflow.com/questions/27226258/get-filename-from-content-disposition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!