Sealyu

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

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            618 隨筆 :: 87 文章 :: 225 評(píng)論 :: 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 閱讀(622) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): GWT
          主站蜘蛛池模板: 灵山县| 如东县| 双江| 平阴县| 大名县| 滦南县| 津市市| 维西| 南阳市| 冕宁县| 尚志市| 河源市| 会宁县| 海兴县| 兰溪市| 建德市| 蒙城县| 肇东市| 余姚市| 灯塔市| 来凤县| 双牌县| 莱州市| 武夷山市| 建宁县| 莎车县| 九龙坡区| 铅山县| 高邑县| 册亨县| 金山区| 昌都县| 陈巴尔虎旗| 石台县| 夹江县| 西城区| 黑龙江省| 兴山县| 天全县| 富宁县| 科技|