-1

i'm having a problem to get the fullPath of a file which i upload using primefaces component . This is my code:

 <h:form prependId="false" enctype="multipart/form-data">
    <p:fileUpload update="@form" mode="advanced" auto="true" 
        fileUploadListener="#{myBean.myFileUpload}"/>
    <h:outputText value="#{myBean.fileName}"/>    
</h:form>     

@ManagedBean
@SessionScoped
public class MyBean {
        private String fileName; 
        public void myFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        fileName = event.getFile().getFileName();
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

} 

i only get the file's name but what i really want to get is the full path. i already try this but it doesn't show anything.

fileName = FilenameUtils.getFullPath(event.getFile().getFileName());
Marina
  • 71
  • 2
  • 15
  • Possible duplicate of [How to get full path of selected file on change of using javascript, jquery-ajax?](https://stackoverflow.com/questions/15201071/how-to-get-full-path-of-selected-file-on-change-of-input-type-file-using-jav) – Kukeltje Jun 12 '18 at 05:49

2 Answers2

2

What would you expect as fullPath of an uploaded file? The Browser sent you some bytes, they are in the memory of the servletcontainer and nowhere stored. There is no fullPath like /var/tmp/myfile.txt.

Ishkafel
  • 333
  • 1
  • 10
-3

Try this:

String fileName  = event.getFile().getFileName();
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String newFileName = servletContext.getRealPath("") + File.separator + "upload" +    File.separator+ fileName;

where "upload" must be replaced by name given in web.xml configuration for prime faces:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
         <param-name>uploadDirectory</param-name>
         <param-value>/upload</param-value>
    </init-param>
</filter>
Kuba
  • 2,069
  • 23
  • 30