Can we create FileItem object from file name and location?

11.3k Views Asked by At

Can we create FileItem object from given file name and file location using Java? If we use commons-fileupload jar we can upload file using jsp/html and in servlet on parsing request we will get List<FileItem>. But I want to do file upload using plain java where I want to create FileItem object manually (I don't want to use byte[] array to store the file). So is there any way to create FileItem object manually?

1

There are 1 best solutions below

3
On

Yes, you can.

Use the DiskFileItemFactory like this:

DiskFileItemFactory factory = new DiskFileItemFactory();
FileItem fi = factory.createItem("formFieldName", "application/zip", false,
    "/var/temp/somefile.zip");

Obviously use content type and other parameters appropriate to your case.