How do I transfer Primefaces-Upload-Temp-Files files instead of doing a time-intensive process?

I want to upload files larger than 2 GB using Java EE using simple elements. In general, this is not so important, but I have a little problem. Usually with PrimeFaces you can upload a file, load this UploadedFile-Object, get an InputStream from it and write it to disk.

But writing large files to disk is a time-consuming task. Instead, I would like to find the downloaded file in the file system, and then move it to the folder where I store all the files, because moving files does not waste time.

So, in accordance with this question here, I decided to set the PrimeFaces-Tmp folder and the size-limit when the files will be placed there. Now each downloaded file goes directly to this folder. The fact is that I now have a file on disk, and the user downloads - and does not create it afterwards.

So far so good. I could identify the file and just move it (although it has a strange name). But, pointing to the Primefaces Userguide (Page 187) , this Tmp folder is used only internally. And I would even steal the contents of the UploadedFile-Object from Primefaces. This does not seem to be a clean solution for me.

Any clues on how to do this?

I also saw this question . It could be my name, but thatโ€™s not what Iโ€™m looking for.

EDIT:

Due to the comment, some code. First, I collect the UploadFile objects in fileUploadListener:

public void handleFileUpload(FileUploadEvent event) throws IOException { FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); FacesContext.getCurrentInstance().addMessage(null, msg); getUploadedFilesDTO().add(new UploadedFileDTO(event.getFile(), uploadedFilesCounter)); uploadedFilesCounter++; } 

Second in EJB, I call copyFile for each UploadedFile object:

 private void copyFile(UploadedFile uploadedFile, String randomFilename) throws IOException { InputStream originalFile = uploadedFile.getInputstream(); OutputStream fileOutput = null; try { fileOutput = new FileOutputStream("/pvtfiles/" + randomFilename); IOUtils.copy(originalFile, fileOutput); } finally { IOUtils.closeQuietly(fileOutput); IOUtils.closeQuietly(originalFile); } } 
+4
source share
2 answers

By creating your own filter, it will not create temporary files. Please read my answer below in another post

fooobar.com/questions/1124135 / ...

0
source

I was able to do this by writing a special filter, as AZ_ suggested, and adding the following:

 FileItem fileItem = multipartRequest.getFileItem("form:fileUpload"); File file = new File(uploadDir + "/" + fileItem.getName()); fileItem.write(file); 

The trick is to use the MultipartRequest object to get a FileItem . FileItem write method can be used to save the file to another location or using a different name. According to the documentation, some implementations can still do this with a copy, but what I use (Apache Commons 1.3) seems to do this by renaming / moving as desired. Note that the string used in getFileItem to search for FileItem is the ID attribute of the <p:fileUpload> .

Details Code:

The modified part of web.xml :

 <filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>some.package.CustomFileUploadFilter</filter-class> </filter> 

Jsf example:

 <h:form id="form"> <h:panelGrid columns="1" class="blockleft"> <p:fileUpload id="fileUpload" auto="true" /> </h:panelGrid> </h:form> 

An example of a custom filter class:

 package some.package; import java.io.File; import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.primefaces.webapp.MultipartRequest; import org.primefaces.webapp.filter.FileUploadFilter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CustomFileUploadFilter extends FileUploadFilter { private static final Logger LOG = LoggerFactory.getLogger(CustomFileUploadFilter.class); private String thresholdSize; private String uploadDir; private final static String THRESHOLD_SIZE_PARAM = "thresholdSize"; private final static String UPLOAD_DIRECTORY_PARAM = "uploadDirectory"; @Override public void init(FilterConfig filterConfig) throws ServletException { thresholdSize = filterConfig.getInitParameter(THRESHOLD_SIZE_PARAM); uploadDir = filterConfig.getInitParameter(UPLOAD_DIRECTORY_PARAM); LOG.info("CatalogFileUploadFilter initiated successfully"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; boolean isMultipart = ServletFileUpload.isMultipartContent(httpServletRequest); if (isMultipart) { LOG.info("Parsing file upload request"); DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); if (thresholdSize != null){ diskFileItemFactory.setSizeThreshold(Integer.valueOf(thresholdSize).intValue()); } if (uploadDir != null){ diskFileItemFactory.setRepository(new File(uploadDir)); } ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); MultipartRequest multipartRequest = new MultipartRequest(httpServletRequest, servletFileUpload); try { FileItem fileItem = multipartRequest.getFileItem("form:fileUpload"); File file = new File("path/to/location/" + fileItem.getName()); fileItem.write(file); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } LOG.info("File upload request parsed successfully, continuing with filter chain with a wrapped multipart request"); filterChain.doFilter(multipartRequest, response); } else { filterChain.doFilter(request, response); } } } 
+1
source

Source: https://habr.com/ru/post/1499933/


All Articles