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/";// 取當前系統路徑
             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);// 將文件寫入服務器
             }
             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
          主站蜘蛛池模板: 旬阳县| 崇明县| 棋牌| 东平县| 潞城市| 余干县| 青河县| 公安县| 海丰县| 灵川县| 准格尔旗| 互助| 恩施市| 抚远县| 襄樊市| 深圳市| 游戏| 永新县| 阳新县| 恩平市| 湖口县| 长泰县| 元氏县| 海淀区| 基隆市| 甘谷县| 那曲县| 信丰县| 黄龙县| 临朐县| 砚山县| 东乌珠穆沁旗| 游戏| 武陟县| 五原县| 怀化市| 绵竹市| 库伦旗| 梨树县| 明光市| 阜南县|