I have the following code to upload the files, when I use the file.getNeme
it shows the temporary file name that has the tmp
extension, how can I find the extension of the actual upload file? the file that I am uploading is called 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();
}
......
the current output is similar to >>> file:upload__1a6d32_13d0eda6d49__7fdf_00000012.tmp
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 ;
}
Just add another getter/setter as below and also remove the getFile() method and it will work
private String fileFileName;
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
You should not use 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
Instead, create another setter setFileName(String fileName)
should do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.