【问题标题】:Where is the p:fileUpload uploaded file saved and how do I change it?p:fileUpload 上传的文件保存在哪里,如何更改?
【发布时间】:2012-08-03 12:33:14
【问题描述】:

我在 Netbeans 的开发中使用 Primefaces 的简单文件上传。我的测试示例类似于 Primefaces 手册。
我的问题:文件在我的本地计算机上上传到哪里?我怎样才能改变它的路径?谢谢!

jsf文件:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>Page test</title>
    </h:head>
    <h:body>
        Hello! My first JSF generated page!
        <h:form enctype="multipart/form-data">
            <p:fileUpload value="#{fileBean.file}" mode="simple" />
            <p:commandButton value="Submit" ajax="false"/>
        </h:form>

    </h:body>
</html>

和托管 bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;


    @ManagedBean

@RequestScoped
public class FileBean {

    private UploadedFile file;

    public FileBean() {
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;

    }
}

【问题讨论】:

    标签: jsf netbeans file-upload glassfish primefaces


    【解决方案1】:

    它默认保存在 servlet 容器的内存或临时文件夹中,具体取决于文件大小和 Apache Commons FileUpload 配置(另请参阅 PrimeFaces User's Guide 中的 &lt;p:fileUpload&gt; 章节的“过滤器配置”部分)。

    您完全不必担心这一点。 servlet 容器和 PrimeFaces 确切地知道它们在做什么。您应该在命令按钮的操作方法中实际将上传的文件内容保存到 需要的位置。您可以通过UploadedFile#getInputStream()InputStreamUploadedFile#getContents()byte[] 的形式获取上传的文件内容(在大文件的情况下,获取byte[] 可能会占用大量内存,您知道,每个byte 都会吃掉一个字节的 JVM 内存,所以不要在大文件的情况下这样做)。

    例如

    <p:commandButton value="Submit" action="#{fileBean.save}" ajax="false"/>
    

    private UploadedFile uploadedFile;
    
    public void save() throws IOException {
        String filename = FilenameUtils.getName(uploadedFile.getFileName());
        InputStream input = uploadedFile.getInputStream();
        OutputStream output = new FileOutputStream(new File("/path/to/uploads", filename));
    
        try {
            IOUtils.copy(input, output);
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    }
    

    FilenameUtilsIOUtils 来自 Commons IO,无论如何您都应该已经安装它们以使 &lt;p:fileUpload&gt; 工作)

    要生成唯一的文件名,您可能会发现File#createTempFile() 工具很有帮助。

    String filename = FilenameUtils.getName(uploadedFile.getFileName());
    String basename = FilenameUtils.getBaseName(filename) + "_";
    String extension = "." + FilenameUtils.getExtension(filename);
    File file = File.createTempFile(basename, extension, "/path/to/uploads");
    FileOutputStream output = new FileOutputStream(file);
    // ...
    

    【讨论】:

    • 谢谢,感谢您的帮助!我明白了它的逻辑,它就奏效了。
    • 无法设置静态路径,例如:“/path/to/uploads”.. 如何查找 primefaces 上传文件的动态路径。
    • 那么您可能需要将其保存在数据库中。这确实是一种不好的做法,但是当您无法设置静态路径时,这是唯一的解决方案。
    【解决方案2】:

    我使用了简单模式,GlassFish 4.0 和 PrimeFaces 4.0

    支持豆

    private UploadedFile file;
    private String destination="C:\\temp\\";
    
    public void upload() {
    
        System.out.println("uploading");
        if(file != null) {  
            System.out.println("the file is" +file);
            FacesMessage msg = new FacesMessage("Succesful" + file.getFileName() + " is uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);  
    
            try {
               copyFile(file.getFileName(), file.getInputstream());
           }
           catch (IOException e) {
              e.printStackTrace();
           }
        }
        System.out.println("uploaf finished");
    }  
    
    public void copyFile(String fileName, InputStream in) {
        try {
            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(destination + fileName));
    
            int read = 0;
            byte[] bytes = new byte[1024];
    
            while ((read = in.read(bytes)) != -1) {
                 out.write(bytes, 0, read);
            }
    
            in.close();
            out.flush();
            out.close();
    
            System.out.println("New file created!");
        } catch (IOException e) {
           System.out.println(e.getMessage());
        }
    }
    

    JSF 页面

    <p:commandButton value="Submit" ajax="false" actionListener="#{staffController.upload}"/> 
    

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 2011-04-13
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      相关资源
      最近更新 更多