开发者

Java : How to accommodate special characters in the filename while uploading and downloading the file?

开发者 https://www.devze.com 2023-01-08 03:35 出处:网络
Background: I have a file which I upload, during this process the link of the file is stored in the database and not the actua开发者_开发技巧l file, acutal file is stored in the File System, currentl

Background:

I have a file which I upload, during this process the link of the file is stored in the database and not the actua开发者_开发技巧l file, acutal file is stored in the File System, currently am storing it in my local machine.

Goal:

My goal is to upload a file and download a file properly which has special characters in it - #,$,%,@ etc.

Issue:

I am able to upload the file with special character but am not able to download file with special characters. Also I cannot do any changes in the Download Servlet as it is part of the Framework, so all I can work with is the Upload Servlet, so my focus is to upload file with special characters in such a way so that I can download them.

I have tried creating an alias for the filename where in am replacing the special characters with '_' symbol, this approach works fine and am able to download the file but actual name of file is not maintained in here, all special characters in the filename are replaced by '_' symbol and this is not acceptable as user should actual name of the file.

Any suggestions or approach:

Code:

public ModelAndView save(HttpServletRequest request, HttpServletResponse response, Object command, 
                        ModelAndView modelView, BindException errors) throws Exception {

String newFileName = checkForSpecialCharsAndGetNewFileName(file.getOriginalFilename());
System.out.println("alias filename="+ newFileName);
String    url = "f" + (String.valueOf(System.currentTimeMillis())) + "_" + newFileName;
String    fileName = file.getOriginalFilename(); 
System.out.println("FileName "+ fileName);
}

//Code to replace all special characters in the incoming file with '_' symbol. 
private String checkForSpecialCharsAndGetNewFileName (String originalFileName) {
  final String[] splChars = {"#", "+", "$"};
  String newString = originalFileName;
  for (int i=0; i<splChars.length; i++)
    newString = StringUtils.replace(newString, splChars[i], "_");
  return newString;
}

Hope am making some sense here.

Thanks.


If I am understanding you correctly, you want to encode the filename such that when you upload it, and later download it, you want to be able to find the same file from the file name.

To do this, you can use URLEncoder and URLDecoder classes.

You can do this doing something like the following:

String fileName;
fileName = URLEncoder.encode("My ! String #", "UTF-8");

That will encode it. To get the original file name:

String originalFileName = URLDecoder.decode(fileName, "UTF-8");

You can use the encoded file name to download the file from the service. You can then decode the file name to store it appropriately.

Hope that helps.


I have a problem where the java.io.File class has started auto encoding tildes that are contained in the filename thats passed in the constructor.

So for example if you instantiate a file using "~filename" it will internally interpret it as "%7Efilename" so that if you need to read or write to a file named "~filename" there is no way to do it.

The issue was introduced when I endorsed a newer set of Xalan / Xerces jars (the full set of 5) on the tomcat server. If you remove the endorsed jars the issue immediately goes away (go figure).

If that is similar to your issue, you may need to look to see if your server is using any endorsed XML parsing jars and consider removing them. I haven't figured out a way to make the newer xerces jars play well with java.io.File or even understand why there is an impact here to begin with.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号