0

我有以下代码来上传文件,当我使用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

4

3 回答 3

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 ;
    }
于 2014-12-04T04:49:23.857 回答
0

只需添加另一个 getter/setter,如下所示,并删除 getFile() 方法,它将起作用

私有字符串文件文件名;

public String getFileFileName() {
    return fileFileName;
}

public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
}
于 2021-01-20T12:18:38.013 回答
-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

相反,创建另一个 settersetFileName(String fileName)应该这样做。

于 2013-02-25T03:02:11.533 回答