Can we create FileItem object from file name and location?

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

1 Answer

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.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like