How to upload files without turning them into a temporary file? (Core JSF NetBeans Interfaces)

Good day to all!

I am building a simple web application using Netbeans, JSF and Primefaces which can download .csv, .jpeg / .jpg and .pdf files. I made 2 folders that were saved on C: drive (downloaded folder and tmp folder).

I assigned the โ€œdownloadedโ€ folder to where the downloaded files are stored, and โ€œtmpโ€ for the .tmp downloaded files. I went through many topics and video tutorials that I correctly completed.

I also uploaded the file sharing and commons io file and added it to the library. It works fine, it shows that it downloads and even sees the .tmp file in the folder to which I attached it.

But I do not see the downloaded files in my "downloaded" folder. So my question is How to upload these files to the uploaded folder.

Here are my codes:

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <title>Facelet Title</title> </h:head> <h:body> <h:form enctype="multipart/form-data" > <p:fileUpload fileUploadListener="#{FileUploadControl.fileUploadControl}" mode="advanced" update="messages" auto="true" sizeLimit="10000000" allowTypes="/(\.|\/)(gif|jpe?g|csv|pdf)$/" /> <!-- --> <p:growl id="messages" showDetail="true"/> </h:form> </h:body> </html> 

FileUploadControl.java

 package controller; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.logging.Level; import java.util.logging.Logger; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.primefaces.model.UploadedFile; @ManagedBean @SessionScoped public class FileUploadControl implements Serializable { private String destination = "C:\\uploaded\\"; private UploadedFile file; public UploadedFile getFile() { return file; } public void setFile(UploadedFile file) { this.file = file; } public FileUploadControl() { } public void TransferFile(String fileName, InputStream in) { try { OutputStream out = new FileOutputStream(new File(destination + fileName)); int reader = 0; byte[] bytes = new byte[(int) getFile().getSize()]; while ((reader = in.read(bytes)) != -1) { out.write(bytes, 0, reader); } in.close(); out.flush(); out.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } public void upload() { String extValidate; if (getFile() != null) { String ext = getFile().getFileName(); if (ext != null) { extValidate = ext.substring(ext.indexOf(".")+1); } else { extValidate = "null"; if (extValidate.equals("pdf")) { try { TransferFile(getFile().getFileName(), getFile().getInputstream()); } catch (IOException ex) { Logger.getLogger(FileUploadControl.class.getName()).log(Level.SEVERE, null, ex); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Wrong", "Error Uploading file...")); } FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Succesful", getFile().getFileName() + "is uploaded.")); } else { FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Wrong_ext", "only extension .pdf")); } } } else { FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Wrong", "Select File!")); } } } 

web.xml

 <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <!--File upload commons --> <filter> <filter-name>PrimeFaces FileUpload Filter</filter-name> <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> <init-param> <param-name>thresholdSize</param-name> <param-value>51200</param-value> </init-param> <init-param> <param-name>uploadDirectory</param-name> <param-value>C:\tmp</param-value> </init-param> </filter> <filter-mapping> <filter-name>PrimeFaces FileUpload Filter</filter-name> <servlet-name>Faces Servlet</servlet-name> </filter-mapping> <!--File upload commons --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>faces/index.xhtml</welcome-file> </welcome-file-list> 

Thanks for your reply and help. looking forward to it!

+1
source share
1 answer

The main reason it isnโ€™t working right now is because you did not bind the value attribute to your backup bean variable, so getFile() always returns null, and upload does nothing.

You probably won't get any results because it seems like you're trying to combine two different modes of the <p:fileUpload/> component.

  • Easy mode

    • You do not define fileUploadListener
    • You define the value attribute on the component and bind to an attribute of type UploadedFile in your bean support (which you have)
  • Advanced mode

    • You do not define the value attribute
    • You define a fileUploadListener that is associated with a method in your bean support (which you also have)
+1
source

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


All Articles