posts - 431,  comments - 344,  trackbacks - 0

               <form-bean name="fileUploadForm" type="org.apache.struts.action.DynaActionForm">
                 <form-property name="fileName" type="Java.lang.String"></form-property>
                 <form-property name="docfile" type="org.apache.struts.upload.FormFile"/>
                 <form-property name="fileDescription" type="Java.lang.String"></form-property>
              </form-bean>


          <form name="form1" method="post" action="<%=request.getContextPath()%>/fileUpload.do?method=upload" enctype="multipart/form-data">
            <table width="43%" border="1" align="center">
              <tr>
                <td colspan="2"><div align="center">上傳周報</div></td>
              </tr>
              <tr>
                <td width="22%">文件名稱</td>
                <td width="78%"><input type="text" name="fileName"></td>
              </tr>
              <tr>
                <td width="22%">選擇文件</td>
                <td width="78%"><input type="file" name="docfile"/></td>
              </tr>
              <tr>
                <td width="22%">文件描述</td>
                <td><input type="textarea" name="fileDescription"/></td>
              </tr>
              <tr>
                <td colspan="2" align="center"><input type="submit" name="Submit" value="上傳"><input type="reset" name="Submit2" value="重置"></td>
              </tr>
            </table>


          public ActionForward upload(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
            request.setCharacterEncoding("UTF-8");
            DynaActionForm dynaform = (DynaActionForm) form;
            FormFile docfile = (FormFile) dynaform.get("docfile");
            String fileName=dynaform.get("fileName").toString();
            String fileDescription=(String) dynaform.get("fileDescription");
            if (docfile == null) {
             return mapping.getInputForward();
            }
            if (docfile.getFileSize() == 0) {
             request.getSession().setAttribute("message", "未選中文件或文件大小為零!");
             return mapping.getInputForward();
            }
            String ctype = docfile.getContentType();
            if (ctype.indexOf("doc") != -1) {
             request.getSession().setAttribute("message", "文件類型錯誤!");
             return mapping.getInputForward();
            }
            // 寫文件
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            InputStream is = null;
            OutputStream fos = null;

            String filename = "";
            String filePath = "";
            try {
             is = (InputStream) docfile.getInputStream();// 把文件讀入
             bis = new BufferedInputStream(is);

             filePath ="E:/upload/";// 取當前系統(tǒng)路徑
             File rootfile = new File(filePath);
             if (!rootfile.exists()) {
              rootfile.mkdirs();
             }
             String name= new String(docfile.getFileName().getBytes("UTF-8"),"gb2312");
             filename = new Date().getTime()+request.getSession().getId()+fileName+".doc";
            fos = new FileOutputStream(filePath + filename);// 建立一個上傳文件的輸出流
             bos = new BufferedOutputStream(fos);
             int bytesRead = 0;
             byte[] buffer = new byte[2 * 1024];
             while ((bytesRead = bis.read(buffer)) != -1) {
              bos.write(buffer, 0, bytesRead);// 將文件寫入服務(wù)器
             }
             FileUpload fileUpload=new FileUpload();
             fileUpload.setFileName(fileName);
             fileUpload.setFilePath(filePath+filename);
             fileUpload.setFileDescription(fileDescription);
             this.getSiteBusiness().getFileLoadService().save(fileUpload);
             return mapping.findForward("uploadsucess");
            }catch (Exception e) {
             e.printStackTrace();
             return mapping.getInputForward();
            } finally {
             if (bos != null) {
              try {
               bos.close();
              } catch (IOException e) {
               System.err.print(e);
               return mapping.getInputForward();
              }
             }
             if (bis != null) {
              try {
               bis.close();
              } catch (IOException e) {
               System.err.print(e);
               return mapping.getInputForward();
              }
             }
            }

           }

          posted on 2007-01-30 11:40 周銳 閱讀(554) 評論(2)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 秦皇岛市| 修武县| 武冈市| 岫岩| 琼海市| 天水市| 玉山县| 墨竹工卡县| 临朐县| 团风县| 丰都县| 通海县| 巨鹿县| 准格尔旗| 高邮市| 翁牛特旗| 平谷区| 思南县| 高州市| 颍上县| 手机| 改则县| 泰州市| 平利县| 金寨县| 西藏| 济阳县| 富民县| 宁晋县| 武宣县| 阿克陶县| 云和县| 水城县| 伽师县| 乌兰察布市| 濮阳县| 海南省| 阳东县| 定安县| 宜黄县| 福州市|