Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

java - j2ee primefaces fileupload file saving destination

Today I've got a problem with the PrimeFaces FileUpload. It works nice, but the files are stored in JBoss' temporary directory so whenever I redeploy the application or just commit the sources to svn, all of the images I uploaded are gone. So I wanted to ask, whether there is a way to save the images to the "source" directory of the war project.

My handleFileUpload method :

public String getUrlBase() {
    return FacesContext.getCurrentInstance().getExternalContext().getRealPath("//upload");
}

public void handleFileUpload(FileUploadEvent event) {
        new File(getUrlBase() + "/" + album.getId()).mkdirs();
        File result = new File(getUrlBase() + "/" + album.getId() + "/" + event.getFile().getFileName());

        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);

            byte[] buffer = new byte[BUFFER_SIZE];

            int bulk;
            InputStream inputStream = event.getFile().getInputstream();
            while (true) {
                bulk = inputStream.read(buffer);
                if (bulk < 0) {
                    break;
                }
                fileOutputStream.write(buffer, 0, bulk);
                fileOutputStream.flush();
            }

            fileOutputStream.close();
            inputStream.close();

            FacesMessage msg = new FacesMessage("Succesful",
                    event.getFile().getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);

        } catch (IOException e) {
            e.printStackTrace();
            FacesMessage error = new FacesMessage("The files were not uploaded!");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }

EDIT: another issue

my view file:

<h:form enctype="multipart/form-data" prependId="false">
        <p:growl id="messages" showSummary="true" showDetail="true" />
        <p:fileUpload fileUploadListener="#{photo.handleFileUpload}"
                      update="messages"  sizeLimit="1073741824"
                      multiple="true" label="choose" allowTypes="*.jpg;*.png;*.gif;"
                      description="Images" />
    </h:form>

When I add update="messages" to the fileUpload I get

javax.servlet.ServletException: null source

whenever I try to just open the page with the fileUpload. Don't you know how to get rid of it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can configure upload directory in the web.xml if you want.

<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>/Users/primefaces/temp</param-value>
  </init-param>
</filter>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...