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

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.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest