简体   繁体   English

Java:如何在上传和下载文件时容纳文件名中的特殊字符?

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

Background: 背景:

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. 我有一个我上传的文件,在此过程中文件的链接存储在数据库而不是实际文件中,acutal文件存储在文件系统中,当前存储在本地机器中。

Goal: 目标:

My goal is to upload a file and download a file properly which has special characters in it - #,$,%,@ etc . 我的目标是上传文件并正确下载文件,其中包含特殊字符 - #,$,%,@ 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. 另外我不能对Download Servlet进行任何更改,因为它是Framework一部分,所以我可以使用的是Upload Servlet ,所以我的重点是上传带有特殊字符的文件,以便我可以下载它们。

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. 谢谢。

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. 我有一个问题, java.io.File类已经开始自动编码包含在构造函数中传递的文件名中的波浪号。

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. 因此,例如,如果使用"~filename"实例化文件,它将在内部将其解释为"%7Efilename"这样如果您需要读取或写入名为"~filename" ,则无法执行此操作。

The issue was introduced when I endorsed a newer set of Xalan / Xerces jars (the full set of 5) on the tomcat server. 当我在tomcat服务器上支持一组更新的Xalan / Xerces jar(全套5)时,引入了这个问题。 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. 如果这与您的问题类似,您可能需要查看您的服务器是否使用任何支持的XML解析jar并考虑删除它们。 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. 我还没有找到一种方法可以让新的xerces jars与java.io.File配合得很好,甚至可以理解为什么这里会产生影响。

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. 为此,您可以使用URLEncoderURLDecoder类。

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. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM