我有以下代码上传文件,当我使用file.getNeme
它显示具有tmp
扩展名的临时文件名时,如何找到实际上传文件的扩展名?我上传的文件名为test.jpg
import java.io.File;
import org.apache.commons.io.FileUtils;
public class FileUploder {
private File file;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
......
System.out.println("file:" +file.getName());
try {
File fileToCreate = new File(filePath,name);
FileUtils.copyFile(file, fileToCreate);
} catch (IOException ex) {
ex.printStackTrace();
}
......
当前输出类似于>>> file:upload__1a6d32_13d0eda6d49__7fdf_00000012.tmp
答案 0 :(得分:0)
private String getImageFileExtendtion (FileItem item) {
String formatName = "png";
String fileName = item.getName();
if (fileName != null && fileName.length() >0) {
int index = -1;
for (int i = fileName.length()-1 ; i >=0; i --) {
if (fileName.charAt(i)=='.') {
index = i;
break;
}
}
if (index >=0 && index < fileName.length() -1) {
formatName = fileName.substring(index+1);
}
}
return formatName ;
}
答案 1 :(得分:0)
只需添加另一个 getter/setter 如下所示,并删除 getFile() 方法,它就会工作
私有字符串文件文件名;
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
答案 2 :(得分:-1)
您不应该使用file.getName()
http://struts.apache.org/release/2.0.x/docs/file-upload.html#FileUpload-BasicUsage
setX(File file)
The file that contains the content of the uploaded file. This is a temporary file and file.getName() will not return the original name of the file
相反,创建另一个setter setFileName(String fileName)
应该这样做。