【问题标题】:Read write data in J2me在 J2me 中读写数据
【发布时间】:2011-12-30 07:56:45
【问题描述】:

我正在编写一个将用户数据存储到文件中的应用程序。但是,当我尝试打开手机内存时,我的应用程序会引发安全异常,并且不允许我写入或读取数据。
这是我的代码。

 try
      {
              FileConnection fc = (FileConnection)Connector.open("file:///C:/myfile.dat",Connector.READ_WRITE);
              // If no exception is thrown, the URI is valid but the folder may not exist.
             if (!fc.exists())
             {
                 fc.create();  // create the folder if it doesn't exist
             }
      OutputStream   os=fc.openOutputStream();
      String s="hello how r u..";
      byte[] b=s.getBytes();

      os.write(b);
      os.flush();
             fc.close();
      }
      catch(Exception error )
       {

         Alert alert = new Alert(error.getMessage(), error.toString(), null, null);
         alert.setTimeout(Alert.FOREVER);
         alert.setType(AlertType.ERROR);
         display.setCurrent(alert);

       }

但是我使用 SDycard 来保存数据并且效果很好。但是,当我尝试访问手机内存时,是否有任何解决方案可以从 SecurityException 中逃脱?当我每次将数据存储在 SDCARD 中时,都会提示一条消息,要求用户允许应用程序读取或写入数据。我也不想要这个提示信息。
如何摆脱这种情况?

【问题讨论】:

    标签: java security java-me midp


    【解决方案1】:

    您必须签署并认证您的 J2ME 应用程序。这将涉及购买证书。我没有这样做,所以你必须确认这一点或等待 SO 中的另一个答案。但我很确定,除非您在 midlet 上签名,否则手机的安全策略会阻止这种情况发生。

    一个关于如何签署您的 midlet 的 URL:

    http://m-shaheen.blogspot.com/2009/07/1.html

    【讨论】:

    • 您的链接指向仅适用于模拟器而不适用于设备的自签名认证,请查看我的答案以获得深入的问题解决方案。
    【解决方案2】:

    同意@Sethu 的观点,即 OP 是由于 midlet 签名。

    OP 分为逻辑阶段来解决问题:

    1. 问题/原因: 每当受限制的 API(在本例中为 JSR 75 api)被 midlet 访问时,它需要权限来验证其真实性,这有助于阻止恶意代码。在您的情况下,midlet 未签名(在 #2 如何签名中说明),因此它没有必要的权限,因此应用程序管理系统会提示用户同意您的 midlet 进行的每个此类敏感操作。阅读this link了解更多详情。

    2. 分辨率 它是一个多步骤过程,(a) 获取证书,参考 this link,(b) 在 JAD 文件中的 MIDlet-Permissions 下添加必要的权限(读取:javax.microedition.io.Connector.file.read,写入:javax.microedition.io.Connector.file.write)

    3. 证书采购 这个链接Java ME signing for dummies

    4. 给出了详细的解释

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2012-08-23
      相关资源
      最近更新 更多