`
snoopy7713
  • 浏览: 1124865 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

Eclipse rcp/rap 开发经验总结(9) - rap上传与下载

    博客分类:
  • RAP
阅读更多

上传

上传即将文件上传到服务器上,在客户端需要写相应的脚本,服务器端需要注册相应的 handle 接受客户端的请求。

原理:

  Rap 的上传和下载是通过普通的 web 的方式进行上传和下载的 , 但是和传统的 wen 还不相同

1、  rap 本身就单线程在跑 , 和上传下载的线程不能进行混淆

所以采用的方式如下:

上传:通过传统的方式上传到指定目录 ,rap 不能直接操作上传的文件流 , 如果想获得上传的数据必须要先上传到指定文件 , 然后让 rap 去加载指定文件即可

下载: 先通过 rap 程序生成需要下载的文件到指定目录 , 然后通过 rap 加载成文件流的形式发送给客户端

 

1 在服务器端注册相应的 handler

  // 注册上传处理事件

  IServiceManager manager = RWT.getServiceManager ();

  IServiceHandler uploadHandler = new UploadServiceHandler();

manager.registerServiceHandler( "uploadServiceHandler" , uploadHandler); //$NON-NLS-1$

 

2 在客户端的脚本调用

目前的做法是创建上传的 dialog, dialog 里面添加 browser 控件,然后 browser 里书写上传的 javaScript 脚本,脚本请求的 url 格式可以通过以下类似的代码创建:

   private String createUploadUrl(String uploadFileName) {

      StringBuffer url = new StringBuffer();

      url.append(RWT.getRequest ().getContextPath());

      url.append(RWT.getRequest ().getServletPath());

      url.append( "?" ); //$NON-NLS-1$

      url.append(IServiceHandler . REQUEST_PARAM );

      url.append( "=uploadServiceHandler" ); //$NON-NLS-1$

      url.append( "&fileName=" ); //$NON-NLS-1$

      url.append(uploadFileName);

      return url.toString();

   }

3 服务器端 handler 的写法

public class UploadServiceHandler implements IServiceHandler {

   public void service() throws IOException, ServletException {

      HttpServletRequest request = RWT.getRequest ();

      request.setCharacterEncoding( "UTF-8" );

      String fileName = request.getParameter( "fileName" );

      FileOutputStream o = null ;

      BufferedReader bufferReader = null ;

      InputStream in = null ;

      try {

         in = request.getInputStream();

         File f = null ;

         try {

          f = new File(FileUtil.getTempFilePathAndName (RWT.getRequest ()

            .getSession().getAttribute( "userName" ).toString(),

                   fileName));

         } catch (Exception e) {

            throw new IOException(e);

         }

         o = new FileOutputStream(f);

         bufferReader = new BufferedReader( new InputStreamReader(in));

         String line = null ;

         boolean beginWrite = false ;

         boolean endWrite = false ;

         while ((line = bufferReader.readLine()) != null ) {

            if (line.indexOf(PriceDomainBean. class .getName()) != -1) {

                if (!beginWrite) {

                   beginWrite = true ;

                } else {

                   endWrite = true ;

                }

            }

            if (beginWrite) {

                o.write((line + "\r\n" ).getBytes());

            }

            if (endWrite) {

                break ;

            }

         }

      } catch (IOException e) {

         throw e;

      } finally {

         if ( null != o) {

            o.close();

         }

         in.close();

         if ( null != bufferReader) {

            bufferReader.close();

         }

      }

      HttpServletResponse response = RWT.getResponse ();

      response.setContentType( "text/html;charset=UTF-8" );

      response.getWriter().write(

            "<br><br><br><DIV align=center><h2> 上传成功 !</h2>" );

   }

}

下载

下载和上传采用的方式基本相同,只不过是将服务器文件读取到本地,和上传是一个相反的过程。

1 在服务器端注册相应的 handler

  // 注册下载处理事件

  IServiceManager manager = RWT.getServiceManager ();

  IServiceHandler downloadHandler = new DownloadServiceHandler();

  manager.registerServiceHandler( "downloadServiceHandler" , downloadHandler);

 

2 在客户端节本的调用

bowser 控件中书写 js 请求脚本,脚本请求的 url 如下

    private String createDownloadUrl(String fileName) {

        StringBuffer url = new StringBuffer();

        url.append (RWT.getRequest ().getContextPath());

        url.append (RWT.getRequest ().getServletPath());

        url.append ( "?" );

        url.append (IServiceHandler. REQUEST_PARAM );

        url.append ( "=downloadServiceHandler" );

        url.append ( "&fileName='+encodeURI('" );

        url.append (fileName);

        url.append ( "')" );

        return url.toString();

    }

3 服务器端 handler 的写法

public class DownloadServiceHandler implements IServiceHandler {

   public void service() throws IOException, ServletException {

      String fileName = URLDecoder.decode (

            RWT.getRequest ().getParameter( "fileName" ), "UTF-8" );

      String filePathAndName = null ;

      try {

         filePathAndName = FileUtil

         .getTempFilePathAndName (RWT.getRequest ().getSession()

                      .getAttribute( "userName" ).toString(), fileName);

      } catch (Exception e) {

         throw new IOException(e);

      }

      File file = new File(filePathAndName);

      if (!file.exists()) {

         return ;

      }

      HttpServletResponse response = RWT.getResponse ();

      response.setHeader( "pragma" , "no-cache" );

      response.setHeader( "cache-control" , "no-cache" );

      response.setDateHeader( "Expires" , 0);

      response.setCharacterEncoding( "UTF-8" );

      response.setContentType( "text/html;charset=UTF-8" );

      response.setHeader( "Content-Disposition" , "attachment;filename="

            + new String(fileName.getBytes( "gb2312" ), "ISO8859-1" ));

      try {

         BufferedInputStream in = new BufferedInputStream(

                new FileInputStream(filePathAndName));

         ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

         byte [] temp = new byte [1024];

         int size = 0;

         while ((size = in.read(temp)) != -1) {

            out.write(temp, 0, size);

         }

         in.close();

         byte [] content = out.toByteArray();

         response.setContentLength(content. length );

         response.getOutputStream().write(content);

      } catch (IOException ioe) {

         throw new RuntimeException(ioe);

      } finally {

         try {

            FileUtil.deleteTempFile (RWT.getRequest ().getSession()

                   .getAttribute( "userName" ).toString(), fileName);

         } catch (Exception e) {

            throw new IOException(e);

         }

      }

   }

}

分享到:
评论

相关推荐

    eclipse-rcp-2022-06-R-linux-gtk-x86_64.tar.gz

    Eclipse IDE for RCP and RAP Developers(eclipse-rcp-2022-06-R-linux-gtk-x86_64.tar.gz) 适用于Linux x86_64: A complete set of tools for developers who want to create Eclipse plug-ins, Rich Client ...

    eclipse-rcp-2022-06-R-win32-x86_64.zip

    Eclipse IDE for RCP and RAP Developers(eclipse-rcp-2022-06-R-win32-x86_64.zip) 适用于Windows x86_64: A complete set of tools for developers who want to create Eclipse plug-ins, Rich Client ...

    eclipse-rcp-2022-06-R-linux-gtk-aarch64.tar.gz

    Eclipse IDE for RCP and RAP Developers(eclipse-rcp-2022-06-R-linux-gtk-aarch64.tar.gz) 适用于Linux aarch64: A complete set of tools for developers who want to create Eclipse plug-ins, Rich Client ...

    eclipse-rcp-2022-06-R-macosx-cocoa-x86_64.dmg

    Eclipse IDE for RCP and RAP Developers(eclipse-rcp-2022-06-R-macosx-cocoa-x86_64.dmg) 适用于macOS x86_64: A complete set of tools for developers who want to create Eclipse plug-ins, Rich Client ...

    eclipse-rcp-2022-06-R-macosx-cocoa-aarch64.dmg

    Eclipse IDE for RCP and RAP Developers(eclipse-rcp-2022-06-R-macosx-cocoa-aarch64.dmg) 适用于macOS aarch64: A complete set of tools for developers who want to create Eclipse plug-ins, Rich Client ...

    eclipse 2020-06 国际化资源包

    Eclipse IDE for RCP and RAP Developers (includes Incubating components) Version: 2020-06 (4.16.0) Build id: 20200615-1200

    eclipse-rcp-2022-06-R-linux-gtk-x86_64.tar

    Eclipse IDE for RCP and RAP linux x86_64 适用于 RCP 和 RAP 开发的工具,需要JDK11及以上运行环境。

    eclipse-rcp-galileo-SR2-win32.zip

    Eclipse For RCP and RAP Developers开发包主要针对开发Eclipse插件,Eclipse RCP(富客户端应用程序),RAP(富客户端ajax应用程序)的程序员,还包含CVS、Mylyn和xml编辑器,EGit分布式版本控制等插件。...

    Instant Eclipse 4 RCP Development How-to.pdf

    Instant Eclipse 4 RCP Development How-to 不错的一本书

    Eclipse RAP2.1部署到Tomcat总结

    本人只会用RCP开发个C/S的程序,最近看到RAP项目想试一下,在百度上看了很多文章关于如何部署RAP到Tomcat,有些文章已经过时的了,还是没能理解清楚,最后弄了2天时间,搞来搞去没办法调试出来,最后都想放弃了。...

    Eclipse Rich Ajax Platform: Bringing Rich Client to the Web

    Eclipse Rich Ajax Platform is the first book on the new Eclipse RAP, and it introduces the required RCP and OSG/i concepts used by Eclipse RAP. This firstPress title demonstrates the functionality ...

    java源码剖析-maracas:Maracas是用Rascal编写的源代码和字节码分析框架,旨在支持JavaAPI和客户端代码的共同发展。

    RCP和RAP。 从其更新站点https://update.rascal-mpl.org/unstable安装不稳定版本的Rascal插件。 单击以获取更多信息。 从其中克隆Rascal项目java-build-manager并将其导入到您的工作空间中。 去! 准备好环境后: 将...

    CodeMirror-Eclipse:CodeMirror-Eclipse

    CodeMirror Eclipse 使您能够在三个上下文中使用 codemirror:Eclipse SWT、RCP 和 RAP。 它在 SWT 浏览器中嵌入了 javascript codemirror 编辑器。 它提供了几个功能: 仅使用带有 SWT/RWT 的 codemirror。 这是...

    archiv-editor

    安装 Eclipse RCP/RAP 发行版。 克隆 git,即从 archiv-editor.git 导入所有项目 转到插件 org.bbaw.pdr.ae.standalone,在 Product-Editor (PDE-Tools) 中打开 Product-Configuration 文件 org.bbaw.pdr.ae....

    java在线安装包.rar

    可以直接安装 官方地址:http://www.eclipse.org/downloads/ ...Eclipse IDE for RCP and RAP Developers Eclipse Modeling Tools Eclipse IDE for Scientific Computing Eclipse IDE for Scout Developers

    packages:构建Enide 2015和AjsIDE软件包

    例如,从Git存储库根目录开始的以下命令将针对同时发布暂存p2存储库构建RCP / RAP软件包: mvn clean verify -Pepp.package.rcp 此版本在两个位置创建输出: tar.gz / zip归档文件,其中的软件包位于archive/和 ...

    JBizMo:Java应用程序的强大生成器(JPA,Java EE ...)-开源

    JBizMo支持Angular,Eclipse RCP / RAP,JavaFX,JSF(Primefaces),Swing和Vaadin应用程序的构建。 生成的应用程序可以独立运行,也可以部署在Java EE服务器(Wildfly 16,Payara 5)上。 另外,还支持Spring平台...

Global site tag (gtag.js) - Google Analytics