有谁知道一些返回操作系统友好文件名的Java类? [英] Does anyone know some Java class that return a operating system friendly filename?

查看:70
本文介绍了有谁知道一些返回操作系统友好文件名的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".

推荐答案

换句话说,您希望摆脱

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", "");

这篇关于有谁知道一些返回操作系统友好文件名的Java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆