好吧,Sebek,我很高兴你问!这是一个非常有趣的方法,没有神奇的方法可以做到这一点。答案确实是重命名您上传的文件。但我建议在文件名前添加随机字符串;比如:fdjrke432test.txt。尝试这个:
filekey= RenameRandom();
File renamedUploadFile = new File(uploadFolder + File.separator+ filekey+ fileName);
item.write(renamedUploadFile);
//remember to give the user the filekey
和
public String RenameRandom()
{
final int LENGTH = 8;
StringBuffer sb = new StringBuffer();
for (int x = 0; x < LENGTH; x++)
{
sb.append((char)((int)(Math.random()*26)+97));
}
System.out.println(sb.toString());
return sb.toString();
}
要从服务器删除或下载文件,您需要找到它,用户将输入密钥,您只需在上传文件夹中搜索以该密钥开头的文件:
filekey= request.getParameter("filekey");
File f = new File(getServletContext().getRealPath("") + File.separator+"data");
File[] matchingFiles = f.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filekey);
}
});
String newfilename = matchingFiles[0].getName();
// now delete or download newfilename