开发环境: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
没有评论:
发表评论