JSF文件上传功能
2007-10-21 22:24
176 查看
最近项目需要用jsf1.2实现文件上传功能,我没有采用myfaces,而是用Apache MyFaces Trinidad 这个实现的文件上传,实现方法和myfaces是一样的。页面采用的是.xhtml。
Apache MyFaces Trinidad是一个基于部分Oracle's ADF Faces构建的JSF1.2组件库。我们要先下在它的jar包,然后导入到我们工程里面。http://myfaces.apache.org/trinidad/index.html这个链接里有文档和包的下载。
下面来看看代码,首先要配置web.xml文件,配置文件里的UPLOAD_MAX_MEMORY设置成500k说明只要是小于500k的文件都会存放在memory里面,UPLOAD_MAX_DISK_SPACE设置成5000k说明上传文件最大为5000k,并且大小在500k和5000k之间的文件才会存放在磁盘默认路径里面,UPLOAD_TEMP_DIR用来设置存放文件的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Maximum memory per request (in bytes) -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>org.apache.myfaces.trinidad.UPLOAD_MAX_MEMORY</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Use 500K -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>512000</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Maximum disk space per request (in bytes) -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>org.apache.myfaces.trinidad.UPLOAD_MAX_DISK_SPACE</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Use 5,000K -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>5120000</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- directory to store temporary files -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>org.apache.myfaces.trinidad.UPLOAD_TEMP_DIR</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Use a TrinidadUploads subdirectory of /tmp -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>/tmp/TrinidadUploads/</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- This filter is always required; one of its functions is
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
file upload. -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>trinidad</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</web-app>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后是faces-config.xml的配置信息
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version='1.0' encoding='UTF-8'?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
version="1.2">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- upload files by lw -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<navigation-rule>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<from-view-id>/upload/upload.xhtml</from-view-id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<navigation-case>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<from-outcome>uploadedfile</from-outcome>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<to-view-id>/upload/uploadlist.xhtml</to-view-id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</navigation-case>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</navigation-rule>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean-name>upLoadBean</managed-bean-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
com.lw.bean.UpLoadBean
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</managed-bean-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean-scope>request</managed-bean-scope>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</managed-bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</faces-config>
UpLoadBean类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.lw.bean;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.BufferedInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.faces.application.FacesMessage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.faces.context.FacesContext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.myfaces.trinidad.model.UploadedFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class UpLoadBean ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private UploadedFile file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public UploadedFile getFile() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setFile(UploadedFile file) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.file = file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String uploadedfile() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FacesContext context = FacesContext.getCurrentInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//在控制台输出上传文件的文件名,和文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FacesMessage message = new FacesMessage(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Successfully uploaded file" + file.getFilename() + "("
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ file.getLength() + "bytes)");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
context.addMessage(null, message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream stream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stream = new BufferedInputStream(file.getInputStream());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = new byte[1024 * 1024];//设定文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unused")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count = stream.read(bytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stream.close();//关闭流
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "uploadedfile";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (IOException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上传页面upload.xhtml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:ui="http://java.sun.com/jsf/facelets"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:u="http://java.sun.com/blueprints/ui"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:c="http://java.sun.com/jsp/jstl/core"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:h="http://java.sun.com/jsf/html"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:f="http://java.sun.com/jsf/core"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:tr="http://myfaces.apache.org/trinidad" xml:lang="en" lang="en">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>My Facelets Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="keywords" content="enter,your,keywords,here" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="description"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
content="A short description of this page." />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr:form usesUpload="true">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr:inputFile label="上传:" value="#{upLoadBean.file}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr:commandButton text="开始" action="#{upLoadBean.uploadedfile}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr:form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
显示上传文件的文件名和文件大小的页面uploadlist.xhtml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:ui="http://java.sun.com/jsf/facelets"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:h="http://java.sun.com/jsf/html"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>My Facelets Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="keywords" content="enter,your,keywords,here" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="description"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
content="A short description of this page." />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:panelGrid border="0" columns="2" style="width: 411px">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="文件名:" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="#{upLoadBean.file.filename}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="文件大小:" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="#{upLoadBean.file.length}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</h:panelGrid>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
现在功能已经实现了,不过这个程序上传的文件成功后,会把文件变成tmp文件,不过我需要的功能已经实现了,因为我上传的是excel和work文件,然后用POI解析tmp文件,把它存入数据库就可以了。
Apache MyFaces Trinidad是一个基于部分Oracle's ADF Faces构建的JSF1.2组件库。我们要先下在它的jar包,然后导入到我们工程里面。http://myfaces.apache.org/trinidad/index.html这个链接里有文档和包的下载。
下面来看看代码,首先要配置web.xml文件,配置文件里的UPLOAD_MAX_MEMORY设置成500k说明只要是小于500k的文件都会存放在memory里面,UPLOAD_MAX_DISK_SPACE设置成5000k说明上传文件最大为5000k,并且大小在500k和5000k之间的文件才会存放在磁盘默认路径里面,UPLOAD_TEMP_DIR用来设置存放文件的路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Maximum memory per request (in bytes) -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>org.apache.myfaces.trinidad.UPLOAD_MAX_MEMORY</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Use 500K -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>512000</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Maximum disk space per request (in bytes) -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>org.apache.myfaces.trinidad.UPLOAD_MAX_DISK_SPACE</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Use 5,000K -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>5120000</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- directory to store temporary files -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>org.apache.myfaces.trinidad.UPLOAD_TEMP_DIR</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Use a TrinidadUploads subdirectory of /tmp -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>/tmp/TrinidadUploads/</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</context-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- This filter is always required; one of its functions is
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
file upload. -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>trinidad</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</web-app>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后是faces-config.xml的配置信息
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version='1.0' encoding='UTF-8'?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
version="1.2">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- upload files by lw -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<navigation-rule>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<from-view-id>/upload/upload.xhtml</from-view-id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<navigation-case>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<from-outcome>uploadedfile</from-outcome>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<to-view-id>/upload/uploadlist.xhtml</to-view-id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</navigation-case>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</navigation-rule>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean-name>upLoadBean</managed-bean-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
com.lw.bean.UpLoadBean
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</managed-bean-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<managed-bean-scope>request</managed-bean-scope>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</managed-bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</faces-config>
UpLoadBean类
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.lw.bean;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.BufferedInputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.InputStream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.faces.application.FacesMessage;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.faces.context.FacesContext;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import org.apache.myfaces.trinidad.model.UploadedFile;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class UpLoadBean ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private UploadedFile file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public UploadedFile getFile() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setFile(UploadedFile file) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.file = file;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String uploadedfile() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FacesContext context = FacesContext.getCurrentInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//在控制台输出上传文件的文件名,和文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
FacesMessage message = new FacesMessage(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
"Successfully uploaded file" + file.getFilename() + "("
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
+ file.getLength() + "bytes)");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
context.addMessage(null, message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InputStream stream;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stream = new BufferedInputStream(file.getInputStream());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] bytes = new byte[1024 * 1024];//设定文件大小
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unused")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int count;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
count = stream.read(bytes);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
stream.close();//关闭流
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "uploadedfile";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
} catch (IOException e) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
上传页面upload.xhtml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:ui="http://java.sun.com/jsf/facelets"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:u="http://java.sun.com/blueprints/ui"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:c="http://java.sun.com/jsp/jstl/core"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:h="http://java.sun.com/jsf/html"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:f="http://java.sun.com/jsf/core"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:tr="http://myfaces.apache.org/trinidad" xml:lang="en" lang="en">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>My Facelets Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="keywords" content="enter,your,keywords,here" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="description"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
content="A short description of this page." />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr:form usesUpload="true">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr:inputFile label="上传:" value="#{upLoadBean.file}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<tr:commandButton text="开始" action="#{upLoadBean.uploadedfile}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</tr:form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
显示上传文件的文件名和文件大小的页面uploadlist.xhtml
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:ui="http://java.sun.com/jsf/facelets"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:h="http://java.sun.com/jsf/html"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
xmlns:f="http://java.sun.com/jsf/core" xml:lang="en" lang="en">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>My Facelets Page</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="keywords" content="enter,your,keywords,here" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="description"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
content="A short description of this page." />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:panelGrid border="0" columns="2" style="width: 411px">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="文件名:" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="#{upLoadBean.file.filename}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="文件大小:" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h:outputText value="#{upLoadBean.file.length}" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</h:panelGrid>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</f:view>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
现在功能已经实现了,不过这个程序上传的文件成功后,会把文件变成tmp文件,不过我需要的功能已经实现了,因为我上传的是excel和work文件,然后用POI解析tmp文件,把它存入数据库就可以了。
相关文章推荐
- Primefaces(JSF2)实现简单显示上传文件列表功能
- 用c++开发基于tcp协议的文件上传功能
- php+ajax实现图片文件上传功能实例
- ajaxSubmit多文件上传功能
- 使用Nginx Upload Module实现上传文件功能
- 使用jQuery.form.js/springmvc框架实现文件上传功能
- 用ActiveXObject可实现从客户端本地上传文件到服务器端的功能
- AJAX与JSF实现带进度条的批量上传文件实例
- AJAX与JSF实现带进度条的批量上传文件实例
- VS2008中,MFC对话框类实现类似QQ拖拽上传文件的功能,CWnd::OnDropFiles
- CKEditor 3开启文件上传功能(Servlet实现)
- Java实现文件上传下载功能
- CI应用:文件上传功能
- Java最基础的文件上传功能
- servlet实现文件上传、预览、下载、删除功能
- C#实现文件上传以及多文件上传功能
- 转:使用 Nginx Upload Module 实现上传文件功能
- 【转载】刚刚做了个文件上传功能,拿来分享一下!(MVC架构及传统架构通用)
- JSF,Myfaces上传文件,以及不需要将excel保存到服务解析excel取出excel中的数据
- HTML供文件上传功能浏览功能的元素是<input/>的file类型