如何上传文件而不将其转换为临时文件? (NetBeans JSF Primefaces) [英] How to upload files without turning it to temporary file? (NetBeans JSF Primefaces)
问题描述
我使用Netbeans,JSF和Primefaces可以上传.csv ,.jpeg / .jpg和.pdf文件。我做了2个文件夹存储在驱动器C :(上传的文件夹和tmp文件夹)。
我将上传文件夹分配到上传文件的存储位置和tmp用于上传文件的.tmp文件。我已经通过了许多问题的线程和视频教程,我正确地遵循。
我也下载了commons fileupload和commons io并将其添加到库中。它工作正常,它显示,它正在上传,甚至看到我分配的文件夹.tmp文件。
但我看不到上传的文件在我的上传文件夹。 以下是我的代码: index.xhtml mode =advanced FileUploadControl.java web.xml 感谢您的回应,帮帮我。期待它! 现在失败的主要原因是您没有绑定 你仍然可能不会得到任何结果,因为看起来你试图结合两种不同的操作模式 简单模式 高级模式 Good day to all! I've been making a simple web Application using Netbeans, JSF and Primefaces that can upload .csv, .jpeg/.jpg and .pdf files. I made 2 folders which was stored in drive C: (uploaded folder and tmp folder). I assigned the "uploaded" folder to where the uploaded files are stored and the "tmp" for the .tmp of the uploaded files. I've been through many question threads and video tutorial which I followed correctly. I also downloaded the commons fileupload and commons io and added it to the library. It is working fine, it displays that it is uploading and even saw the .tmp file on the folder i assigned it to. But I cannot see the uploaded files on my "uploaded" folder.
So, my question is
所以,我的问题是,
如何将这些文件上传到我上传的文件夹中。 p>
<?xml version ='1.0'encoding ='UTF-8'?>
xmlns:h =http://java.sun.com/jsf/html
xmlns:p =http://primefaces.org/ui>
< h:head>
< title> Facelet标题< / title>
< / h:头>
< h:body>
< h:form enctype =multipart / form-data>
update =messages
auto =true
sizeLimit =10000000
allowTypes =/(\。| \ /)(gif | jpe?g | csv | pdf)$ /
/>
<! - - >
< p:growl id =messagesshowDetail =true/>
< / h:表格>
< / h:body>
< / html>
,
How can I upload these files into my "uploaded" folder.程序包控制器;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
@ManagedBean
@SessionScoped
public class FileUploadControl实现Serializable {
private String destination =C:\\uploaded \\;
私人UploadedFile文件;
public UploadedFile getFile(){
return file;
}
public void setFile(UploadedFile file){
this.file = file;
$ b public FileUploadControl(){
}
public void TransferFile(String fileName,InputStream in){
try {
OutputStream out = new FileOutputStream(new File(destination + fileName));
int reader = 0;
byte [] bytes = new byte [(int)getFile()。getSize()]; $(读取器= in.read(字节))!= -1){
out.write(bytes,0,reader);
}
in.close();
out.flush();
out.close();
catch(IOException e){
System.out.println(e.getMessage());
public void upload(){
String extValidate;
if(getFile()!= null){
String ext = getFile()。getFileName();
if(ext!= null){
extValidate = ext.substring(ext.indexOf(。)+ 1);
} else {
extValidate =null; ());
if(extValidate.equals
catch(IOException ex){
Logger.getLogger(FileUploadControl.class.getName()).log(Level.SEVERE,null,ex);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,new FacesMessage(Wrong,Error Uploading file ...));
}
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,new FacesMessage(Succesful,getFile()。getFileName()+is uploaded。));
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,new FacesMessage(Wrong_ext,only extension .pdf));
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null,new FacesMessage(Wrong,Select File!));
<?xml version =1.0encoding =UTF-8?>
< web-app version =3.0xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\">
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
<参数值>开发< /参数值>
< / context-param>
<! - 文件上传共享 - >
< filter>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< filter-class> org.primefaces.webapp.filter.FileUploadFilter< / filter-class>
< init-param>
< param-name> thresholdSize< / param-name>
<参数值> 51200< /参数值>
< / init-param>
< init-param>
< param-name> uploadDirectory< / param-name>
< param-value> C:\ tmp< / param-value>
< / init-param>
< / filter>
< filter-mapping>
< filter-name> PrimeFaces FileUpload Filter< / filter-name>
< servlet-name> Faces Servlet< / servlet-name>
< / filter-mapping>
<! - 文件上传共享 - >
< servlet>
< servlet-name> Faces Servlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
<加载启动> 1< /加载启动>
< / servlet>
< servlet-mapping>
< servlet-name> Faces Servlet< / servlet-name>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>
< session-config>
< session-timeout>
30
< / session-timeout>
< / session-config>
< welcome-file-list>
< welcome-file> faces / index.xhtml< / welcome-file>
< / welcome-file-list>
value
属性添加到您的支持bean变量中,所以 getFile()
将总是返回null并且 upload $ c
< p:fileUpload />
组件。
$ b
fileUploadListener
值
属性并绑定到你的后台bean中的 UploadedFile
类型属性您有)
值
属性
fileUploadListener $ c $这是boun d到你的后台bean中的一个方法(你也有)
Here are my codes:
index.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<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>Facelet Title</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data" >
<p:fileUpload fileUploadListener="#{FileUploadControl.fileUploadControl}"
mode="advanced"
update="messages"
auto="true"
sizeLimit="10000000"
allowTypes="/(\.|\/)(gif|jpe?g|csv|pdf)$/"
/>
<!-- -->
<p:growl id="messages" showDetail="true"/>
</h:form>
</h:body>
</html>
FileUploadControl.java
package controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
@ManagedBean
@SessionScoped
public class FileUploadControl implements Serializable {
private String destination = "C:\\uploaded\\";
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public FileUploadControl() {
}
public void TransferFile(String fileName, InputStream in) {
try {
OutputStream out = new FileOutputStream(new File(destination + fileName));
int reader = 0;
byte[] bytes = new byte[(int) getFile().getSize()];
while ((reader = in.read(bytes)) != -1) {
out.write(bytes, 0, reader);
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void upload() {
String extValidate;
if (getFile() != null) {
String ext = getFile().getFileName();
if (ext != null) {
extValidate = ext.substring(ext.indexOf(".")+1);
} else {
extValidate = "null";
if (extValidate.equals("pdf")) {
try {
TransferFile(getFile().getFileName(), getFile().getInputstream());
} catch (IOException ex) {
Logger.getLogger(FileUploadControl.class.getName()).log(Level.SEVERE, null, ex);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Wrong", "Error Uploading file..."));
}
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Succesful", getFile().getFileName() + "is uploaded."));
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Wrong_ext", "only extension .pdf"));
}
}
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage("Wrong", "Select File!"));
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!--File upload commons -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:\tmp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<!--File upload commons -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
Thank you for your response and help. looking forward to it!
The main reason it's failing as at now is that you haven't bound the value
attribute to your backing bean variable, so getFile()
will always return null and upload
will do nothing.
You're still probably not going to get any results because it appears that you're trying to combine two different modes of operation of the <p:fileUpload/>
component.
Simple mode
- You don't define a
fileUploadListener
- You define a
value
attribute on the component and bind to theUploadedFile
type attribute in your backing bean (which you have)
- You don't define a
Advanced mode
- You don't define a
value
attribute - You define a
fileUploadListener
which is bound to a method in your backing bean (which you also have)
- You don't define a
这篇关于如何上传文件而不将其转换为临时文件? (NetBeans JSF Primefaces)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!