j2ee primefaces 文件上传文件保存目的地 [英] j2ee primefaces fileupload file saving destination

查看:12
本文介绍了j2ee primefaces 文件上传文件保存目的地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的 handleFileUpload 方法:

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);
        }
    }

另一个问题

我的视图文件:

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

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

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

javax.servlet.ServletException: null source

每当我尝试使用 fileUpload 打开页面时.不知道怎么摆脱吗?

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

推荐答案

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

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>

这篇关于j2ee primefaces 文件上传文件保存目的地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆