有谁知道一些返回操作系统友好文件名的Java类?
我的网页上有一个上传器,但是有些人上传的文件名为compañia15%* 09.jpg",文件名像这样的文件时我遇到了问题.
I have an uploader in my web page, but some people upload files named like "compañia 15% *09.jpg" and i have problems when the filenames are like that one.
我想找到一个返回该示例的类,例如:"compania1509.jpg".
I would like to found a class that returns for that example something like this: "compania1509.jpg".
换句话说,您希望摆脱 String#replaceAll()
,为此使用[^\x20-\x7e]
模式.
In other words, you'd like to get rid of all characters outside the printable ASCII range? You can use String#replaceAll()
with a pattern of [^\x20-\x7e]
for this.
name = name.replaceAll("[^\\x20-\\x7e]", "");
如果您也想摆脱空格,请改以\x21
开头.您甚至可以仅将其限制为单词字符.使用\W
表示任何非单词"字符.然后,该名称将仅与字母数字和下划线匹配.
If you want to get rid of spaces as well, then start with \x21
instead. You can even restrict it to Word-characters only. Use \W
to indicate "any non-word" character. The name will then match only alphanumericals and the underscore.
name = name.replaceAll("\\W", "");