Sealyu

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks
          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 閱讀(629) 評論(0)  編輯  收藏 所屬分類: GWT
          主站蜘蛛池模板: 循化| 怀柔区| 巴塘县| 西丰县| 睢宁县| 乡宁县| 宜兰市| 巨鹿县| 萨迦县| 东方市| 嘉荫县| 乌鲁木齐县| 海南省| 克什克腾旗| 庄浪县| 土默特左旗| 凤阳县| 华阴市| 罗源县| 永顺县| 石河子市| 通州市| 泉州市| 桐梓县| 措勤县| 盐池县| 罗江县| 紫金县| 天门市| 大余县| 金寨县| 黄龙县| 东源县| 曲阳县| 太康县| 信宜市| 平顺县| 沅江市| 抚宁县| 自治县| 岳阳市|