tags:

views:

142

answers:

2

Hello, i have a String with a path of my file and i want to instancie a new FormFile with this String. It's possible ..?

My code:

    public ArrayList<FormFile> getFilesFromFolder(String path) {
  File file = new File(path);
  ArrayList<FormFile> vFiles = new ArrayList<FormFile>();

  if (file.exists()) {

   File[] files = file.listFiles();
   int i;

   for (i = 0; i < files.length; i++) {
    if (files[i].isFile()) {
     vFiles.add((FormFile) files[i]);
    }
   }
  } else {
   vFiles = null;
  }
  return vFiles;
 }

but i have an error in this line vFiles.add((FormFile) files[i]);

java.lang.ClassCastException: java.io.File cannot be cast to org.apache.struts.upload.FormFile

A: 

Your code is failing because you create a new File object, but you try to cast it to a FormFile. A FormFile is an interface, so it can't be instantiated directly. It looks like you need a DiskFile, and it would be new DiskFile(path).

Jeff Storey
A: 

FormFile is an interface (can't be instantiated). Look at an implementation of this interface, like CommonsMultipartRequestHandler.CommonsFormFile. This one has a constructor and can be created for a FileItem (DiskFileItem) which represents a file.

Andreas_D
i don't know how to do this
Mercer
read the javaDoc for those classes - it's all explained. If you don't have it: google for 'docjar FormFile' to access an online javaDoc.
Andreas_D
when i do this DiskFile diskFile = new DiskFile(files[i].getPath());i have the path of my file but i not have the content of my file
Mercer