Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get filename from Content-Disposition [closed]

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?

like image 947
h2c Avatar asked Dec 01 '14 10:12

h2c


1 Answers

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.

like image 161
Joop Eggen Avatar answered Oct 23 '22 00:10

Joop Eggen