ADF实现文件上传

1. 上传到服务器的某个目录下
(1)页面代码
方式一:
<af:form usesUpload="true">
<af:inputFile label="Upload:"valueChangeListener="#{managedBean.fileUploaded}"/>
<af:commandButton text="Begin"/>
</af:form>

方式二:
<af:form usesUpload="true">
<af:inputFile label="Upload:" value="#{managedBean.file}"/>
<af:commandButton text="Begin" action="#{managedBean.doUpload}"/>
</af:form>

(2)ManageBean 代码
对应方式一:

private RichInputFile if1;

public void setIf1(RichInputFile if1) {
this.if1 = if1;
}

public RichInputFile getIf1() {
return if1;
}
public void fileUploaded(ValueChangeEvent event) {
InputStream in;
FileOutputStream out;

UploadedFile file = (UploadedFile)event.getNewValue();
String fileUploadLoc = "C:/Temp";

boolean exists = (new File(fileUploadLoc)).exists();
if (!exists) {
(new File(fileUploadLoc)).mkdirs();
}
if (file != null && file.getLength() > 0) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage message =
new FacesMessage("File Uploaded" + file.getFilename() + "(" +
file.getLength() + "bytes)");
context.addMessage(event.getComponent().getClientId(context),
message);

try {
out = new FileOutputStream(fileUploadLoc + "/" + file.getFilename());
in = file.getInputStream();
for (int bytes = 0; bytes < file.getLength(); bytes++) {
out.write(in.read());
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
String filename = file != null ? file.getFilename() : null;
String byteLength = file != null ? "" + file.getLength() : "0";
}

if1.setValue(null);
}


对应方式二:
import org.apache.myfaces.trinidad.model.UploadedFile;
public class AManagedBean{
public UploadedFile getFile(){
return _file;
}
public void setFile(UploadedFile file){
_file = file;
}
public String doUpload(){
UploadedFile file = getFile();
// ... and process it in some way}
private UploadedFile _file;
}

doUpload方法与fileUploaded方法类似,故略。

2. 上传到数据库BLOB字段中
请参考《使用ADF BC存取BLOB数据》。

3. 上传文件最大默认限制为2M,可以修改web.xml增大大小,增加如下设置:


<context-param>
<!-- Maximum memory per request (in bytes) -->
<param-name>org.apache.myfaces.trinidad.UPLOAD_MAX_MEMORY</param-name>
<!-- Use 500K -->
<param-value>512000</param-value>
</context-param>
<context-param>
<!-- Maximum disk space per request (in bytes) -->
<param-name>org.apache.myfaces.trinidad.UPLOAD_MAX_DISK_SPACE</param-name>
<!-- Use 5,000K -->
<param-value>5120000</param-value>
</context-param>
<context-param>
<!-- directory to store temporary files -->
<param-name>org.apache.myfaces.trinidad.UPLOAD_TEMP_DIR</param-name>
<!-- Use a TrinidadUploads subdirectory of /tmp -->
<param-value>/tmp/TrinidadUploads/</param-value>
</context-param>

转载于:http://maping930883.blogspot.com/2010/04/adf090.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值