vjame

          優化代碼是無止境的
          隨筆 - 65, 文章 - 9, 評論 - 26, 引用 - 0
          數據加載中……

          使用FormFile處理文件上傳下載

          1.首先界面設計,自己搞定 .。。。



          2. 然后在jsp頁面中定義文件上傳標簽,name 要和From中定義的FormFile變量名稱一致



          3. 在Form中定義FormFile私有變量, 它和文件上傳標簽名稱要一樣。



          4.  ,這里要先在po類里面定義個字節變量content 接收上傳來的文件內容


          5. 在Action的增加方法中,首先判斷頁面上是否有文件被上傳,然后把內容設置到po類里面去 ,這里getFileData()得到的是字節.

          6. 文件需要提交下載時,在Action中加入一下代碼

              //下載公文附件
              public ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
                  
                  DocumentActionForm daf 
          = (DocumentActionForm)form;
                  
                  Document document 
          = documentManager.findDocument(daf.getId());
                  
                  response.reset();
                  response.setContentType(
          "application/x-download;charset=GBK");
                  response.setHeader(
          "Content-Disposition""attachment;filename=temp.doc");
                  
                  response.getOutputStream().write(document.getContent());
                  
                  response.getOutputStream().flush();
                  response.getOutputStream().close();
                  
                  
          //指示struts,不要對返回值進行處理
                  return null;
              }

          7. 程序運行效果
          另外一種方法在Action中添加download方法也是可以達到下載功能的目的
              public ActionForward downloadFile(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response) 
          throws Exception{

                  String docId 
          = request.getParameter("docId");
                  Doc doc 
          = baseService.find(docId);
                  SerializableBlob serBlob 
          = null;
                  
          if(doc!= null){
                      serBlob 
          = (SerializableBlob) doc.getContentBinary();
                  }

                  Blob blob 
          = serBlob.getWrappedBlob();
                  
          try {
                      
          if (blob != null) {
                          response.reset();
                          BufferedInputStream in 
          = new BufferedInputStream(
                                  blob.getBinaryStream()); 
          // 輸入流
                          response.setContentType("bin:charset=utf-8");
                          response.setHeader(
                                          
          "Content-Disposition",
                                          
          "attachment; filename=temp.doc"); // 設置保存頁面提示信息
                          byte[] b = new byte[100];
                          
          int len;
                          
          while ((len = in.read(b)) > 0) {
                              response.getOutputStream().write(b, 
          0, len);
                          }
                          in.close();
                      }
                  } 
          catch (Exception e) {
                      
          return null;
                  }
                  
          return null;
              }

          posted on 2008-11-07 01:24 lanjh 閱讀(3010) 評論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 镇原县| 康马县| 武城县| 石河子市| 布拖县| 涪陵区| 嘉黎县| 连南| 喀喇| 丰台区| 丁青县| 浦北县| 隆昌县| 大余县| 大理市| 朝阳市| 三原县| 大邑县| 长宁区| 洛川县| 广德县| 文化| 济阳县| 临汾市| 晴隆县| 集贤县| 揭阳市| 青阳县| 浑源县| 彝良县| 漳州市| 安塞县| 平山县| 济阳县| 竹山县| 资源县| 花垣县| 垫江县| 通城县| 铜梁县| 荆门市|