vjame

          優(yōu)化代碼是無(wú)止境的
          隨筆 - 65, 文章 - 9, 評(píng)論 - 26, 引用 - 0
          數(shù)據(jù)加載中……

          使用FormFile處理文件上傳下載

          1.首先界面設(shè)計(jì),自己搞定 .。。。



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



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



          4.  ,這里要先在po類里面定義個(gè)字節(jié)變量content 接收上傳來(lái)的文件內(nèi)容


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

          6. 文件需要提交下載時(shí),在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,不要對(duì)返回值進(jìn)行處理
                  return null;
              }

          7. 程序運(yùn)行效果
          另外一種方法在Action中添加download方法也是可以達(dá)到下載功能的目的
              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"); // 設(shè)置保存頁(yè)面提示信息
                          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 閱讀(3005) 評(píng)論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 墨江| 东平县| 盐池县| 吉木萨尔县| 望都县| 会昌县| 湖北省| 琼结县| 澄迈县| 水富县| 蒲江县| 五指山市| 修水县| 北辰区| 永清县| 古交市| 贵德县| 威海市| 溧阳市| 纳雍县| 吉首市| 四子王旗| 淮阳县| 安西县| 宁波市| 玉溪市| 资阳市| 杭锦后旗| 清流县| 安西县| 读书| 安义县| 东莞市| 乐亭县| 饶阳县| 库尔勒市| 苏尼特右旗| 刚察县| 潜山县| 云浮市| 海伦市|