欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-02 21:07:49
...

 Eclipse rcp/rap 开发经验总结(9) - rap上传与下载
            
    
    博客分类: Eclipse rcp/rap 开发 eclipseraprcpeclipse rcpeclipse rap 

欢迎大家访问我的个人网站 萌萌的IT人,后续所有的文章都会在此发布

--------------------------------------------------------------------------------------------

上传

上传即将文件上传到服务器上,在客户端需要写相应的脚本,服务器端需要注册相应的 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);

         }

      }

   }

}