ADF 上传文件到服务器 方法之一

本文介绍了使用ADF在前台页面设置usesUpload属性为true来实现文件上传,以及后台java类中如何实现上传文件的方法uploadFileButton。
摘要由CSDN通过智能技术生成

一、前台页面实现

   <afh:body>
        <af:form id="form" usesUpload="true">
          <af:panelForm inlineStyle="border-style:solid; border-color:rgb(0,0,0); border-width:1.0pt; ">
            <afh:tableLayout cellPadding="1" borderWidth="0" halign="center"
                             cellSpacing="0" width="750px">
              <afh:rowLayout inlineStyle="height:20px;"/>
              <afh:rowLayout>
                <afh:cellFormat halign="center">
                  <af:panelHorizontal>
                    <af:outputLabel value="#{res['BIDMANAGE.UPLOAD_FILE_SELECT']}"/>
                    <af:objectSpacer width="8"/>
                    <af:inputFile inlineStyle="width:450px;"  id="file"
                                  binding="#{bidBatchFileUploadBean.upFile}"/>
                  </af:panelHorizontal>
                </afh:cellFormat>
              </afh:rowLayout>
              <afh:rowLayout>
                <afh:cellFormat halign="center" height="50px" valign="middle">
                  <af:panelHorizontal>
                    <af:commandButton text="#{res['UPLOAD']}" οnclick="saveFile();"
                                      action="#{bidBatchFileUploadBean.uploadFileButton}"/>
                    <af:objectSpacer width="20"/>
                    <af:commandButton text="#{res['BIDMANAGE.RETURN']}"
                                      action="#{bidBatchFileUploadBean.returnBidSectionEdit}"/>
                  </af:panelHorizontal>
                </afh:cellFormat>
              </afh:rowLayout>
            </afh:tableLayout>
          </af:panelForm>
        </af:form>
      </afh:body>

 

注意页面中form设置属性 usesUpload="true"


效果如下:

 

二、java类种方法uploadFileButton实现

    public String uploadFileButton() {
        //上传的附件名称
        String mainDocName = "";
        //获取上传的文件
         System.out.println("00000000000000000proName="+proName );
        UploadedFile file = (UploadedFile)this.getUpFile().getValue();
        System.out.println("file="+file);
        if (file != null) {
            //检查文件类型
             mainDocName = file.getFilename();
             System.out.println("mainDocName=========>" + mainDocName);
            long fileSize = file.getLength() / 1024;
            System.out.println("文件大小:" + fileSize + "KB");
            if (fileSize > 81920) {
                this.showInfoMessage("文件过大!");
                return null;
            }
            //文件上传路径
            Properties pros = new Properties();
            try {
                pros.load(this.getClass().getClassLoader().getResourceAsStream("test/bean/config.properties"));
            } catch (Exception e) {
            }
            String path = pros.getProperty("path");
            System.out.println("path " + path);
            String dirFlag = System.getProperty("file.separator"); //自动匹配操作系统文件路径
            String saveDirectory = "";
             File dir = new File(saveDirectory);
             if(!dir.isDirectory())
                 dir.mkdirs();
                    
            InputStream birdIn;
            FileOutputStream birdOut;
            //获取系统当前时间
            java.text.SimpleDateFormat date = new java.text.SimpleDateFormat("yyyyMMddHHmmssSS");
            String currentTimeMillis = date.format(new Date(System.currentTimeMillis()));
            //随机数
            java.util.Random rand = new java.util.Random(); 
            int iValue = rand.nextInt();
            String t1_fname = mainDocName.substring(0,mainDocName.lastIndexOf("."));
            String t2_fname = mainDocName.substring(mainDocName.lastIndexOf("."));
            String mainName="";
            mainName = t1_fname + "_"+currentTimeMillis + t2_fname;
            byte[] bytes1 = UrlBase64.encode((path+dirFlag).getBytes());
             byte[] bytes2 = UrlBase64.encode(mainName.getBytes());
             String encodeNewFilePath = new String(bytes1);
            String encodeNewFileName = new String(bytes2);
            
            String address = path + mainName;
            System.out.println("address===="+address);
           String url =path +  mainDocName;
            try{
                birdOut = new FileOutputStream(address);
                birdIn = file.getInputStream();
                for (int bytes = 0; bytes < file.getLength(); bytes++) {
                    birdOut.write(birdIn.read());
                }
                birdIn.close();
                 birdOut.close();
            }catch(Exception e){
                StringWriter sw = new StringWriter();
                e.printStackTrace(new PrintWriter(sw));
                String result = sw.toString();
                if (result.indexOf("ORA-20017:") > 0) {
                    this.showInfoMessage(MessageCommon.getFlexMessage(e));
                } else {
                    this.showWarnMessage(result);
                    System.out.println(result);
                }
            } 
        }
        return null;
    }


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值