David Ruan
David Ruan

Reputation: 794

how to avoid file name being filtered in HttpServletRequest

My file name is abc#123.pdf which is passed to the server side by concatenating it to the url.

On the server side, I use this line to get the file name. But only get "abc", the string after # is filtered. How can I avoid this?

fileName= request.getParameter(DOCUMENT_NAME);

Upvotes: 2

Views: 198

Answers (1)

quantumThinker
quantumThinker

Reputation: 156

This occurs because anything after # in URL is considerd as reference part of the URL. You need to encode you query parameters while sending it to server side. If you are using javascript you can do following.

var myUrl = "http://example.com?documentName=" + encodeURIComponent("abc#123.pdf");

On java side after getting the parameter with following code

encodeFileName = request.getParameter(DOCUMENT_NAME);

You can decode the value using URLDecoder .

String fileName = java.net.URLDecoder.decode(encodeFileName, "UTF-8");

Make sure you use encodeURIComponent in javascript, if you use encodeURI it would not encode #.

Upvotes: 4

Related Questions