重新命名上传的文件 // 重命名文件 @SuppressWarnings("static-access") private String renameFile() { String t = new Timestamp(new Date().getTime()).toString(); t = t.replaceAll("-", ""); t = t.replaceAll(":", ""); t = t.replace(".", ""); t = t.replaceAll(" ", ""); Random rd = new Random(); int rdNum = rd.nextInt(Integer.MAX_VALUE); return (t + rdNum); }
楼主试下看看
应该没有问题。
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date dt = new Date(System.currentTimeMillis()); String filename =fmt.format(dt);
saveurl = request.getRealPath("\\")+"file\\";
saveurl +=filename + "." + ext;// 最终文件的保存路径
String name= filename + "." + ext; 这样既解决了文件名的乱码问题又解决了文件名相同的问题
有很多方法值得看下
http://topic.csdn.net/u/20081225/15/7f53258e-4543-4802-8fe5-6ff7033bf9b1.html
// 重命名文件
@SuppressWarnings("static-access")
private String renameFile() {
String t = new Timestamp(new Date().getTime()).toString();
t = t.replaceAll("-", "");
t = t.replaceAll(":", "");
t = t.replace(".", "");
t = t.replaceAll(" ", "");
Random rd = new Random();
int rdNum = rd.nextInt(Integer.MAX_VALUE);
return (t + rdNum);
}
fileName = URLEncoder.encode(fileName,"GBK");再在处理上传的页面将文件名解码解码:
(注:fileName是文件名,它在传递过程中编码可能会乱,
这时要注意将文件名转为ISO-->fileName = new String(fileName .getBytes("xxxx"), "ISO-8859-1");"xxx"是它在传递过程中改变的编码)
fileName = URLDecoder.decode(fileName,"GBK");如果文件名是在处理上传的页面给定的就不用再编码(encode),直接解码(decode),但在解码时同样注意文件名是不是ISO。祝你好运!