title: Java : How to accommodate special characters in the filename while uploading and downloading the file?
body: <p><strong>Background:</strong></p>
  
  <p>I have a file which I upload, during this process the link of the file is stored in the database and not the actual file, acutal file is stored in the File System, currently am storing it in my local machine. </p>
  
  <p><strong>Goal:</strong> </p>
  
  <p>My goal is to upload a file and download a file properly which has special characters in it - <code>#,$,%,@ etc</code>.</p>
  
  <p><strong>Issue:</strong></p>
  
  <p>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 <code>Download Servlet</code> as it is part of the <code>Framework</code>, so all I can work with is the <code>Upload Servlet</code>, so my focus is to upload file with special characters in such a way so that I can download them. </p>
  
  <p>I have tried creating an alias for the filename where in am replacing the special characters with <code>'_'</code> 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 <code>'_'</code> symbol and this is not acceptable as user should actual name of the file.</p>
  
  <p>Any suggestions or approach:</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><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&lt;splChars.length; i++)
      newString = StringUtils.replace(newString, splChars[i], "_");
    return newString;
  }
  </code></pre>
  
  <p>Hope am making some sense here. </p>
  
  <p>Thanks. </p>