Mercer
Mercer

Reputation: 9986

How to instantiate a FormFile Object?

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

Upvotes: 2

Views: 5934

Answers (2)

Andreas Dolk
Andreas Dolk

Reputation: 114797

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.

Upvotes: 1

Jeff Storey
Jeff Storey

Reputation: 57202

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).

Upvotes: 0

Related Questions