java j2ee primefaces 文件上传文件保存目的地

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4358493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:02:20  来源:igfitidea点击:

j2ee primefaces fileupload file saving destination

javafile-uploadprimefaces

提问by El_w

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.

今天我遇到了 PrimeFaces FileUpload 的问题。它运行良好,但文件存储在 JBoss 的临时目录中,因此每当我重新部署应用程序或将源提交到 svn 时,我上传的所有图像都消失了。所以想问一下,有没有办法把图片保存到war项目的“source”目录下。

My handleFileUpload method :

我的 handleFileUpload 方法:

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

当我将 update="messages" 添加到 fileUpload 我得到

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?

每当我尝试使用 fileUpload 打开页面时。你不知道如何摆脱它吗?

回答by Vladimir Ivanov

You can configure upload directory in the web.xmlif you want.

web.xml如果需要,您可以在 中配置上传目录。

<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>