richfaces fileupload组件是用于文件批量上传的组件,用他做JSF的文件上传是非常方便和高效的.但有些问题需要注意,配置方法如下
1、先修改web.xml配置文件
<filter>
<display-name>Ajax4jsf Filter</display-name>
<filter-name>ajax4jsf</filter-name>
<filter-class>org.ajax4jsf.Filter</filter-class>
<init-param>
<param-name>createTempFiles</param-name>
<param-value>true</param-value>(此项必须为true否则后台管理bean无法获得文件)
</init-param>
<init-param>
<param-name>maxRequestSize</param-name>
<param-value>4000000</param-value>(此项设置意思是限制上传文件的大小,超过4MB的就不允许上传了,配合后面组件里的某些属性进行控制上传文件大小)
</init-param>
</filter>
<filter-mapping>
<filter-name>ajax4jsf</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
2、jsp页面的写法
<h:panelGroup>
<h:outputText value="能上传扩展名为doc,txt,xls,rar的文件,最大能够上传4MB的文件"/>
<rich:fileUpload fileUploadListener="#{phealthstatute.uploadListener}"
maxFilesQuantity="5" autoclear="false" addControlLabel="添加" immediateUpload="true"
uploadControlLabel="上传" clearAllControlLabel="清除所有" clearControlLabel="清除"
cancelEntryControlLabel="取消" stopControlLabel="停止" stopEntryControlLabel="停止"
noDuplicate="true" listHeight="50" listWidth="500" allowFlash="true" onsizerejected="op();"
style="height:100;" id="upload" doneLabel="上传成功" sizeErrorLabel="文件不能超过4MB,上传失败!" fileEntryClass="fhys" transferErrorLabel="上传失败!"
acceptedTypes="txt,doc,xls,rar">
<a4j:support reRender="info" event="onuploadcomplete" />
</rich:fileUpload>
</h:panelGroup>
其中maxFilesQuantity="5"是表示只能上传五个文件
onsizerejected="op();"意为当选择的文件大小,超过web.xml设置的大小,则执行JS方法op().
sizeErrorLabel="文件不能超过4MB,上传失败!" 意为当选择的文件大小时,上传列表中则显示该提示信息。
transferErrorLabel="上传失败!" 意为传输发生异常未成功时,显示的提示信息。
<a4j:support reRender="info" event="onuploadcomplete" />
该句代码意为当上传文件完毕时即刷新ID为info的组件。通常用来显示上传文件的一些具体显示。
immediateUpload="true" 意为允许自动上传文件,即用户不需要再手动点击上传,只需直接选择文件即可
allowFlash="true" 意为允许按照flash方式上传,主要是显示效果要好看些,有很漂亮的上传进度条。如果不能正常浏览,设为false试试...
fileUploadListener="#{phealthstatute.uploadListener}" 意为当文件上传时调用后台管理bean的监听方法。
3、后台管理bean监听方法的写法
public void uploadListener(UploadEvent event) {
this.setDispMessage("display:none");
try {
UploadItem item =event.getUploadItem();//获取上传的文件对象
String fullFileName = item.getFileName();
fileName = fullFileName.substring(fullFileName.lastIndexOf("//")+1
, fullFileName.length());
String[] names=fileName.split("//.");
PAccessories pa=new PAccessories();//一个pojo类
pa.setCfilename(names[0]);
pa.setCextendname(names[1]);
if(item.getFileSize()>1000){
pa.setCfilesize(((item.getFileSize()/1000)+" kb").toString());
}else{
pa.setCfilesize((item.getFileSize()+" b").toString());
}
filePath = com.fl.flhis.util.ParamList.getInstance().getParamValue("TEMPFILE");
java.io.File saveFile = new java.io.File(filePath, fileName);
if (item.getFileSize() <= 4000000) {//判断小于4MB的文件才处理(这里主要是程序是否处理,其实也可以不要,如果web.xml配置了的话)
FileInputStream fis = new FileInputStream(item.getFile());
FileOutputStream out = new FileOutputStream(saveFile);
int bytes = 0;
byte[] bteFile = new byte[1024];
while ((bytes = fis.read(bteFile)) != -1) {
out.write(bteFile, 0,bytes);
}
out.flush();
fis.close();
out.close();
list.add(pa);
this.setDispMessage("display:none");
}else{
this.setFileName("");
this.setDispMessage("display:");
}
} catch (Exception e) {
e.printStackTrace();
}
}
上面的方法有些还要以减化,其主要代码就是一句UploadItem item =event.getUploadItem();//获取上传的文件对象
其余的就是围着这个对象进行一些读写信息的操作!
个人感觉该组件还是比较好用,而且也比较好看,推荐使用!
用法二:
前沿:JSF本身并没有提供上传组件,如果使用原生的JSF做上传工具,你可能需要做更多的额外操作。幸运的是richfaces知道了我们的苦
衷,它提供了高效可控的上传组件<richfaces:fileUpload/>,下面简单给你大家介绍一下如何使用。
1.web.xml中的配置
如果你需要对文件上传的大小提供阀值控制,可以设定,具体参考richfaces的文档。否则我们不做更多处理。
如果你需要配置的自己的文件系统路径,你可以在context-param中配置路径。
-
<
context-param
>
-
<
param-name
>
rentImagesPath
</
param-name
>
-
<
param-value
>
/images/site/rent/
</
param-value
>
-
</
context-param
>
<context-param>
<param-name>rentImagesPath</param-name>
<param-value>/images/site/rent/</param-value>
</context-param>
2.page部分:上传组件
-
<rich:fileUploadfileUploadListener=
"#{rentPhoto.upload}"
maxFilesQuantity=
"#{rentPhoto.uploadsAvailable}"
-
id="upload"
immediateUpload=
"#{rentPhoto.autoUpload}"
acceptedTypes=
"jpg,gif,png,bmp"
allowFlash=
"true"
-
listWidth="500px"
listHeight=
"100px"
autoclear=
"true"
status=
"operStatus"
-
transferErrorLabel="传输出错--文件过大或名称错误!"
>
-
<!--上传成功后,刷新图片列表,当然这个组件只是点缀,本文不作介绍-->
-
<a4j:supportevent="onuploadcomplete"
reRender=
"photoList"
/>
-
</rich:fileUpload>
<rich:fileUpload fileUploadListener="#{rentPhoto.upload}" maxFilesQuantity="#{rentPhoto.uploadsAvailable}"
id="upload" immediateUpload="#{rentPhoto.autoUpload}" acceptedTypes="jpg, gif, png, bmp" allowFlash="true"
listWidth="500px" listHeight="100px" autoclear="true" status="operStatus"
transferErrorLabel="传输出错--文件过大或名称错误!">
<!-- 上传成功后,刷新图片列表,当然这个组件只是点缀,本文不作介绍 -->
<a4j:support event="onuploadcomplete" reRender="photoList" />
</rich:fileUpload>
上面就是我们的fileUpload上传组件,它的一些属性,很容易理解,当然你可以参考文档。主要看rentPhoto.upload这个侦听器是如何工作的。
3.java部分
-
import
org.richfaces.event.UploadEvent;
-
import
org.richfaces.model.UploadItem;
-
-
-
public
class
RentPhoto{
-
.....
-
-
public
void
upload(UploadEventevent)
throws
Exception{
-
FacesContextcontext=FacesContext.getCurrentInstance();
-
ExternalContextec=context.getExternalContext();
-
-
ServletContextsc=(ServletContext)ec.getContext();
-
-
UploadItemitem=event.getUploadItem();
-
Datedate=new
Date(System.currentTimeMillis());
-
-
Stringtemp_path=java.net.URLDecoder.decode(item.getFileName(),"utf-8"
);
-
-
if
(temp_path.lastIndexOf(
'.'
)<
0
){
-
return
;
-
}
-
if
(item.getFileSize()>
512000
){
-
return
;
-
}
-
-
StringfileName=MD5.Md5(date.toString())+temp_path.substring(temp_path.lastIndexOf('.'
));
-
-
FileOutputStreamout=new
FileOutputStream(sc.getRealPath(sc.getInitParameter(
"rentImagesPath"
))+
"/"
+fileName);
-
out.write(item.getData());
-
out.close();
-
-
Sessionsession=HibernateInit.currentSession();
-
RPhotophoto=new
RPhoto(fileName,rent.getId());
-
new
RPhotoOper().update(session,photo);
-
HibernateInit.closeSession();
-
-
photoList.add(photo);
-
}
-
-
}
分享到:
相关推荐
Richfaces组件使用指南、richfaces手册(很详细、很全面的教程)
RichFaces组件简介,复合组件,日期控件,Ajax标签, 轻松实现。RichFaces组件简介,复合组件,日期控件,Ajax标签, 轻松实现。RichFaces组件简介,复合组件,日期控件,Ajax标签, 轻松实现。
中文版的richfaces组件使用介绍手册,很好用 尤其是对英语不太好的同学,对于初学者来说直接去官网看英文的demo难度有点大,先从中文的入手,了解了再去看英文的获得一些自己的见解,这样效果是不是更好呢
java Richfaces组件使用指南
Richfaces3.3.3常用组件使用手册 便于学习Richfaces和a4j和jsf框架 很好很实用
本文档主要根据richfaces3.3.3 用户手册和demo 编写。 Richfaces 下载地址http://www.jboss.org/richfaces/download/stable。 demo 下载地址http://www.jboss.org/richfaces/demos。
这是关于Richaface的Ajax4j以及richfaces组件的相关介绍,欢迎使用下载,
详细的介绍了RichFaces中的Ajax组件的使用配置以及组件的使用方法。是为初学者了解ajax组件提供一点方便
详细的阐述了RichFaces组件中如何实现验证码的刷新
richfaces组件说明文档及使用实例
这是我用来测试的一个工程,都不能在rich:dataTable中使用action actionListener 博文链接:https://insomniask.iteye.com/blog/111030
JSF2和RICHFACES4使用指南
使用richfaces需要用到的jar包! commons-beanutils-1.7.0.jar,commons-codec-1.3.jar,commons-collections-3.2.jar,commons-digester-1.8.jar,commons-discovery-0.4.jar,commons-el-1.0.jar,commons-...
教会如何使用richfaces,及richfaces应用
详细描述了Richfaces的组件使用情况!
3.1.3日期组件不支持多语言,因此修改richfaces-ui-3.1.3.GA.jar包中org\richfaces\ui.pack.js为中文
richfaces标签学习笔记richfaces标签学习笔记richfaces标签学习笔记richfaces标签学习笔记
Richfaces标签 简介Richfaces标签相关内容 初学者认知
richfaces-ui-3.1.2的组件。AJAX基于jsf。全程是JBoss RichFaces ,不知道为什么在Exadel上找不到免费下载,还不清楚和Exadel RichFaces在代码中的关系到了什么程度。