上传文件到服务器方式之二:使用Button的ActionListener
来源:互联网 发布:网络众筹 编辑:程序博客网 时间:2024/04/29 13:23
开发环境: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="#{myBackingBean2.inputFile_valueChangeListener}"
binding="#{myBackingBean2.inputFileComponent}"/>
<af:commandButton text="Upload" id="cb1" actionListener="#{myBackingBean2.doUpload}"/>
</af:form>
说明:
(1)核心处理代码是myBackingBean.uploadButton_actionListener方法,
myBackingBean.inputFile_valueChangeListener方法只是负责给file赋值。
(2)点击Upload按钮会触发ValueChangeEvent发生,然后处理文件上传逻辑。
2. 对应的Managed Bean代码
// 点击Upload按钮时,先调用此方法,因为ValueChange事件先于actionListener事件 。
// 该方法主要作用是给file赋值。
public void inputFile_valueChangeListener(ValueChangeEvent event) {
file = (UploadedFile)event.getNewValue();
}
// 点击Upload按钮时,执行完valueChangeListener后,调用此方法
public void uploadButton_actionListener(ActionEvent event) {
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();
}
// 清空InputFile,这样符合中国人的习惯。
inputFileComponent.setValue(null);
} else {
popupMessage(event, Not_Valid_FileName_Message);
}
}
3. 思考:哪种方式更好一些?
感觉第2种方式更科学和自然一些。
因为点击Upload按钮时,才是真正确认要上传文件,所以主要逻辑应该写在这里。
而ValueChangeEvent发生时,只是选择了一个文件而已,还不能确定用户是否真的要上传。
从代码角度看,第2种方式略微比第一种方式复杂一点点:多了一个私有变量:private UploadedFile file;。
说明:本文改自本人旧作,使用了目前最新的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="#{myBackingBean2.inputFile_valueChangeListener}"
binding="#{myBackingBean2.inputFileComponent}"/>
<af:commandButton text="Upload" id="cb1" actionListener="#{myBackingBean2.doUpload}"/>
</af:form>
说明:
(1)核心处理代码是myBackingBean.uploadButton_actionListener方法,
myBackingBean.inputFile_valueChangeListener方法只是负责给file赋值。
(2)点击Upload按钮会触发ValueChangeEvent发生,然后处理文件上传逻辑。
2. 对应的Managed Bean代码
// 点击Upload按钮时,先调用此方法,因为ValueChange事件先于actionListener事件 。
// 该方法主要作用是给file赋值。
public void inputFile_valueChangeListener(ValueChangeEvent event) {
file = (UploadedFile)event.getNewValue();
}
// 点击Upload按钮时,执行完valueChangeListener后,调用此方法
public void uploadButton_actionListener(ActionEvent event) {
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();
}
// 清空InputFile,这样符合中国人的习惯。
inputFileComponent.setValue(null);
} else {
popupMessage(event, Not_Valid_FileName_Message);
}
}
3. 思考:哪种方式更好一些?
感觉第2种方式更科学和自然一些。
因为点击Upload按钮时,才是真正确认要上传文件,所以主要逻辑应该写在这里。
而ValueChangeEvent发生时,只是选择了一个文件而已,还不能确定用户是否真的要上传。
从代码角度看,第2种方式略微比第一种方式复杂一点点:多了一个私有变量:private UploadedFile file;。
Project下载:UploadFile2.7z。
http://maping930883.blogspot.com/2010/04/adf092buttonactionlistener.html
0 0
- 上传文件到服务器方式之二:使用Button的ActionListener
- 上传文件到服务器方式之一:使用InputFile的ValueChangeListener
- PHP使用之上传文件到服务器
- java上传文件到远程服务器(二)---HttpClient方式
- libcurl上传文件到服务器的几种方式
- 文件上传到服务器的2种方式
- Android使用post方式上传图片到服务器的方法
- Android使用post方式上传图片到服务器的方法
- 如何使用Git上传文件到Github之tortoiseGit的使用(二)
- 使用PUTTY的PSFTP上传文件到服务器
- java之TCP上传文件到服务器
- C#网络编程之利用Form表达上传文件到服务器之二
- 使用HTTP Client 上传文件到服务器
- 使用httpclient上传文件到web服务器
- 使用psftp上传文件到服务器
- 使用structs上传文件到服务器
- 使用commons-fileupload上传文件到服务器
- 使用ADF上传文件到服务器
- 我的安卓之旅----启航
- Libgdx的使用(12)——physics-body-editor
- 上传文件到服务器方式之一:使用InputFile的ValueChangeListener
- MediaPlayer本地播放流程解析(二)
- 1408201941-hd-Elevator.cpp
- 上传文件到服务器方式之二:使用Button的ActionListener
- 如何实现下载文件之一:静态文件
- JsonUtil工具类
- vim中ctrl+z,jobs,fg命令用法
- 如何实现下载文件之二:动态文件
- C 与 C++ 的混合编程
- 在JAVA中使用DES算法
- 高版本Chrome扩展程序强制停止问题
- Spark