Jsf p:fileUpload上传的文件保存在哪里?如何更改?
我在Netbeans开发中使用Primefaces的简单文件上传。我的测试示例与Primefaces手册类似。Jsf p:fileUpload上传的文件保存在哪里?如何更改?,jsf,netbeans,file-upload,glassfish,primefaces,Jsf,Netbeans,File Upload,Glassfish,Primefaces,我在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-
我的问题:文件在我的本地计算机上上传到哪里?我如何更改它的路径?谢谢强> 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>
默认情况下,它保存在servlet容器的内存或临时文件夹中,具体取决于文件大小和Apache Commons文件上载配置(另请参见中的
一章的“过滤器配置”部分)
你根本不应该担心这个。servlet容器和PrimeFaces确切地知道它们做什么。您应该在命令按钮的操作方法中将上载的文件内容保存到您需要的位置。您可以通过UploadedFile#getInputStream()
以InputStream
的形式获取上传的文件内容,或者通过UploadedFile#getContents()
以字节[]的形式获取上传的文件内容
在大文件的情况下内存可能会很昂贵,您知道,每个字节都会占用JVM的一个字节内存,所以在大文件的情况下不要这样做)
例如
与
(FilenameUtils
和IOUtils
来自Commons IO,为了让
正常工作,您应该已经安装了它们)
要生成唯一的文件名,您可能会发现该工具很有用
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);
// ...
我使用了简单模式,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页面
谢谢,非常感谢您的帮助!我得到了它的逻辑,它只是工作。不能设置像:“/path/to/uploads”这样的静态路径。。如何找到primefaces上传文件的动态路径。然后您可能需要将其保存在DB中。这确实是一种不好的做法,但当您无法设置静态路径时,这是唯一的解决方案。
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);
// ...
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());
}
}
<p:commandButton value="Submit" ajax="false" actionListener="#{staffController.upload}"/>