在哪里号码:文件上传上传的文件保存,如何改变呢?(Where is the p:fileUpload

2019-06-26 18:53发布

我用Primefaces的简单的文件上传在开发使用Netbeans。 我的测试示例类似于到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;

    }
}

Answer 1:

它的默认保存在任servlet容器的存储器或临时文件夹,根据文件大小和Apache通用FileUpload配置(也参见“过滤器配置”的节<p:fileUpload>章PrimeFaces用户指南 )。

你不应该担心这一点的。 servlet容器和PrimeFaces知道他们做了什么。 你应该在命令按钮的操作方法其实是可以保存上传文件的内容, 需要它的位置。 你可以上传文件内容的InputStreamUploadedFile#getInputStream()或作为byte[]UploadedFile#getContents()获得一个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是从你无论如何都应该已经安装,以获得下议院IO <p:fileUpload>工作)

要生成唯一的文件名,你可能会发现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);
// ...


Answer 2:

我用简单的模式,GlassFish的4.0和4.0 PrimeFaces

辅助Bean

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}"/> 


文章来源: Where is the p:fileUpload uploaded file saved and how do I change it?