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

来源:互联网 发布:javaweb报销系统源码 编辑:程序博客网 时间:2024/04/29 22:05

一、前台页面实现

   <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']}" onclick="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
原创粉丝点击