p:fileUpload 上传的文件保存在哪里,如何更改? [英] Where is the p:fileUpload uploaded file saved and how do I change it?

查看:23
本文介绍了p:fileUpload 上传的文件保存在哪里,如何更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the simple File upload of Primefaces in development with Netbeans. My test example is similar to to the Primefaces manual.
My question: where does the file get uploaded on my local computer? How can I change the path for it? Thx!

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:

and the managed 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;

    }
}

推荐答案

它默认保存在 servlet 容器的内存或临时文件夹中,具体取决于文件大小和 Apache Commons FileUpload 配置(另请参阅过滤器配置"PrimeFaces 用户指南中的章节).

It's by default saved in either the servlet container's memory or the temp folder, depending on the file size and the Apache Commons FileUpload configuration (see also "Filter Configuration" section of the <p:fileUpload> chapter in PrimeFaces User's Guide).

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

You shouldn't worry about this at all. The servlet container and PrimeFaces know exactly what they do. You should in the command button's action method actually be saving the uploaded file contents to the location where you need it to be. You can get the uploaded file contents as an InputStream by UploadedFile#getInputStream() or as a byte[] by UploadedFile#getContents() (getting a byte[] is potentially memory expensive in case of large files, you know, each byte eats one byte of JVM's memory, so don't do that in case of large files).

例如

<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,你应该已经安装了它以获得 <p:fileUpload>工作)

(FilenameUtils and IOUtils are from Commons IO which you should anyway already have installed in order to get <p:fileUpload> to work)

要生成唯一的文件名,您可以找到 File#createTempFile() 工具很有帮助.

To generate unique file names, you may find File#createTempFile() facility helpful.

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);
// ...

这篇关于p:fileUpload 上传的文件保存在哪里,如何更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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