自定义远程服务

阅读(500) 标签: server, 连接报表中心,

背景说明:

很多时候用户不仅需要操作本地的报表模板,还要操作远程服务器上的报表模板,比如修改远程服务器的报表或者将本地模板上传至远程服务器等。为了满足这种需求,润乾报表提供了远程服务接口。可在设计器中通过用户权限控制远程服务,用户可以根据自己的需求自定义远程服务接口,实现登陆、注销、打开文件、连接数据源、上传文件等操作。

 

自定义远程服务需要实现润乾报表提供的com.raqsoft.ide.custom.Server接口,请看如下示例:

 

代码示例

 

package api; //指定类路径

 

import com.raqsoft.ide.custom.Server;

import ......;

 

public class CustomreportServer implements Server{ //实现Server接口,自动引入接口中的所有方法

  … …

  public boolean login(String user, String pwd) {

    … …

    //通过httpclient等工具,根据user参数请求业务系统中提供的servlet,获取到对应用户密码PWD

    if (PWD == null)

      if (pwd != null)

        return false;

    if(PWD.equals(pwd) || PWD2.equals(pwd)){

      loginState = LOGIN;

      return true;

    }

    return false;

  }

  public InputStream open(String fileName) {

    …… //通过httpclient等类似工具,向web服务器发起报表文件流请求,读取web服务器上的报表文件,这里需要告诉用户所请求应用读取报表的url,如果没有登陆就调用 这个方法,需要弹出对话框提示用户没有登陆。下面这行代码没用,去掉

    File remoteFile = new File(url, fileName);

    return new FileObject(remoteFile.getAbsolutePath()).getInputStream();

  }

 

  /**

   * 保存文件到服务器

   *

   * @param fileName

   *  服务端文件名

   * @param fileBytes

   *  文件转成的字节数组

   */

  public void save(String fileName, byte[] fileBytes) {

    ……//这个方法是保存当前打开的报表的字节流到远程web端。字节流通过httpclient等工具发送到web端的servlet

    … …

    JOptionPane.showConfirmDialog(null, "保存文件:"+fileName+"至服务器"+getName()+"成功!");

  }

 

  /**

   * 上传本地文件到服务器

   *

   * @param fileName

   *  服务端文件名

   * @param localFile

   *  本地文件

   */

  public void save(String fileName, File localFile) {

    … …

  }

 

  /**

   * 返回指定目录下的文件信息

   *

   * @param path

   *  path=null/时表示根

   * @return 指定目录下文件信息

   */

  public List<FileInfo> listFiles(String path) {

    … …

    File remoteFile;

    if (path == null) {

      remoteFile = new File(url);

    } else {

      remoteFile = new File(url, path);

    }

    if (remoteFile.isDirectory()) {

      File[] files = remoteFile.listFiles();

      if (files != null && files.length > 0) {

        List<FileInfo> fileInfos = new ArrayList<FileInfo>();

        … …

        return fileInfos;

       

      }

    }

    return null;

  }

 

  /**

   * 取当前用户可用的数据源配置

   *

   * @return 数据源列表(用户有权限访问的)

   */

  public List<DBConfig> getDBConfigList() {

… …

    try {

      RaqsoftConfig config = ConfigUtil.load(new FileInputStream(

          CONFIG_PATH), false);

      return config.getDBList();

    } catch (Exception e) {

      e.printStackTrace();

    }

    return null;

  }

 

  /**

   * 注销

   */

  public void logout() {

    loginState = LOGOUT;

    System.out.println("注销");

  }

… …

}

 

 

参考文件:CustomServer.java