Sealyu

          --- 博客已遷移至: http://www.sealyu.com/blog

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          <2010年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28123456
          78910111213

          常用鏈接

          留言簿(14)

          隨筆分類

          隨筆檔案

          友情鏈接

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          Per some requests for the File Download opposite of File Upload.

          On the server side I implemented the doGet method of the HttpServlet class. I grab the raw data from the database and set it into the response (with the appropriate response type):

          BufferedOutputStream output = null;
          try {
          RawAttachmentItem attachment = attachmentFileDao.retrieveContents(fileid);
          ByteArrayInputStream input = new ByteArrayInputStream(attachment.getContents());
          int contentLength = input.available();

          resp.reset();
          resp.setContentType("application/octet-stream");
          resp.setContentLength(contentLength);
          resp.setHeader("Content-disposition", "attachment; filename=""" + attachment.getFilename()
          + """");
          output = new BufferedOutputStream(resp.getOutputStream());
          for(int data; (data=input.read()) != -1;) {
          output.write(data);
          }
          output.flush();
          }
          catch (IOException e) {

          e.printStackTrace();
          }
          finally {
          close(output);
          }
          On the client side, you simple create a new iFrame with its 'src' attribute set to the servlet url for downloading the file:

          boolean frameExists = (RootPanel.get("downloadiframe") != null);
          if(frameExists) {
          Widget widgetFrame = (Widget)RootPanel.get("downloadiframe");
          widgetFrame.removeFromParent();
          }

          NamedFrame frame = new NamedFrame("downloadiframe");
          frame.setUrl(GWT.getModuleBaseURL()
          + "/attachmentHandler?action=dl&fileid="
          + model.getFileId());
          frame.setVisible(false);

          RootPanel.get().add(frame);
          When the file gets sent back to the iFrame, the browser will treat it as a file download and prompt you to do something with it (open, save, cancel, etc).

          If anyone has questions or requires more detail, please do not hesitate to ask!!
          posted on 2010-02-01 21:21 seal 閱讀(628) 評論(0)  編輯  收藏 所屬分類: GWT
          主站蜘蛛池模板: 汪清县| 皮山县| 大洼县| 苏尼特右旗| 灵川县| 濉溪县| 朝阳区| 友谊县| 夏津县| 蚌埠市| 红安县| 枣庄市| 泽普县| 南江县| 资兴市| 萍乡市| 巴彦淖尔市| 田林县| 五莲县| 德惠市| 牙克石市| 玉树县| 屯昌县| 本溪市| 荃湾区| 柳州市| 玛纳斯县| 个旧市| 长泰县| 长武县| 普兰店市| 大邑县| 巢湖市| 濮阳市| 麟游县| 凤山市| 民勤县| 深泽县| 象山县| 乌拉特前旗| 元谋县|