上传文件到服务器方式之一:使用InputFile的ValueChangeListener
来源:互联网 发布:网络众筹 编辑:程序博客网 时间:2024/04/29 11:18
开发环境:JDeveloper 11.1.2.0.0。
说明:本文改自本人旧作,使用了目前最新的JDeveloper 11.1.2.0.0重新开发验证。(2011-8-1)
1. 新增页面:upload_file.jspx
<af:form id="f1" usesUpload="true">
<af:inputFile label="File Name:" id="if1"
valueChangeListener="#{myBackingBean.inputFile_valueChangeListener}"
binding="#{myBackingBean.inputFileComponent}"/>
<af:commandButton text="Upload" id="cb1" actionListener="#{myBackingBean.uploadButton_actionListener}"/>
</af:form>
说明:
(1)核心处理代码是myBackingBean.inputFile_valueChangeListener方法,
myBackingBean.uploadButton_actionListener方法只是检查文件名是否为空。
(2)点击Upload按钮的主要作用是触发ValueChangeEvent发生。
2. 对应的Managed Bean代码
// 点击Upload按钮时,先调用此方法,因为ValueChange事件先于actionListener事件 。
public void inputFile_valueChangeListener(ValueChangeEvent event) {
UploadedFile file = (UploadedFile)event.getNewValue();
if (!(new File(fileUploadLocation).exists())) {
(new File(fileUploadLocation)).mkdirs();
}
if (file != null && file.getLength() > 0) {
try {
InputStream in = file.getInputStream();
FileOutputStream out = new FileOutputStream(fileUploadLocation + "/" + file.getFilename());
writeInputStreamToOutputStream(in, out);
in.close();
out.close();
String message =
"Successfully uploaded file '" + file.getFilename() + "' (" + file.getLength() + " bytes)";
popupMessage(event, message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 点击Upload按钮时,执行完valueChangeListener后,调用此方法
public void uploadButton_actionListener(ActionEvent actionEvent) {
if (this.getInputFileComponent().getValue() != null) {
// 清空InputFile,这样符合中国人的习惯。
inputFileComponent.setValue(null);
} else {
popupMessage(actionEvent, Not_Valid_FileName_Message);
}
}
Project下载:UploadFile.7z。
参考文献:
1. http://gergerconsulting.blogspot.com/2007/04/adf-faces-progressindicator-example-for.html
2. http://groundside.com/blog/DuncanMills.php?title=the_progress_indicator&more=1&c=1&tb=1&pb=1
3. http://technology.amis.nl/blog/2297/adf-faces-file-uploading-it-is-really-that-simple
4. http://thepeninsulasedge.com/blog/?p=6
5. http://log-cd.javaeye.com/blog/415844
6. http://cn.forums.oracle.com/forums/thread.jspa?messageID=4013464
7. http://forums.oracle.com/forums/thread.jspa?threadID=983109
http://maping930883.blogspot.com/2010/04/adf090.html
0 0
- 上传文件到服务器方式之一:使用InputFile的ValueChangeListener
- adf中用inputFile上传文件到weblogic 方法1
- ADF 上传文件到服务器 方法之一
- af inputFile上传文件后立即消失的原因
- 上传文件到服务器方式之二:使用Button的ActionListener
- libcurl上传文件到服务器的几种方式
- 文件上传到服务器的2种方式
- Android使用post方式上传图片到服务器的方法
- Android使用post方式上传图片到服务器的方法
- thinkphp3.2+bootstrap-inputfile多文件上传
- 使用PUTTY的PSFTP上传文件到服务器
- 使用HTTP Client 上传文件到服务器
- 使用httpclient上传文件到web服务器
- 使用psftp上传文件到服务器
- 使用structs上传文件到服务器
- 使用commons-fileupload上传文件到服务器
- 使用ADF上传文件到服务器
- 使用commons-fileupload上传文件到服务器
- Task Flow使用指南之五:捕获异常 (3)
- jquery设置radio
- catalan卡特兰数
- 我的安卓之旅----启航
- Libgdx的使用(12)——physics-body-editor
- 上传文件到服务器方式之一:使用InputFile的ValueChangeListener
- MediaPlayer本地播放流程解析(二)
- 1408201941-hd-Elevator.cpp
- 上传文件到服务器方式之二:使用Button的ActionListener
- 如何实现下载文件之一:静态文件
- JsonUtil工具类
- vim中ctrl+z,jobs,fg命令用法
- 如何实现下载文件之二:动态文件
- C 与 C++ 的混合编程