木纹转印,氟碳喷涂 熊毅 2013-08-05 12:09
浏览 237
已采纳

同样的文件,在Tomcat和Resin环境下,读取的流居然不一样?

先上读取文件的代码:

  public static String getHexString(byte[] b) {
    String str = "";
    for (int i = 0; i < b.length; i ++) {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1)
        hex = '0' + hex;
      str += hex.toUpperCase();
    }
    return str;
  }

  public static final void getP() {
    InputStream bmpInput = null;
    try {
      Resource resource = new ClassPathResource("com/mww/action/test.bmp", Utils.class.getClassLoader());
      if (resource == null || ! resource.exists())
        return;

      System.out.println(resource.contentLength());
      System.out.println(resource.getDescription());
      System.out.println(resource.getFilename());
      System.out.println(resource.getURI().toString());
      System.out.println(resource.getURL().toString());

      StringBuilder sb = new StringBuilder();

      byte[] bytes = new byte[8];
      bmpInput = resource.getInputStream();

      while (bmpInput.read(bytes) != - 1)
        sb.append(getHexString(bytes));

      FileUtils.writeStringToFile(new File("D:\\key.txt"), sb.toString());
    } catch (IOException e) {
      return;
    } finally {
      IOUtils.closeQuietly(bmpInput);
    }
  }

 其中打印resource信息部分,输出的内容完全一样,包括文件长度:

-- Tomcat
25014
class path resource [com/mww/action/test.bmp]
test.bmp
jar:file:/D:/wwwroot/WEB-INF/lib/webapp.jar!/com/mww/action/test.bmp
jar:file:/D:/wwwroot/WEB-INF/lib/webapp.jar!/com/mww/action/test.bmp

-- Resin
25014
class path resource [com/mww/action/test.bmp]
test.bmp
jar:file:/D:/wwwroot/WEB-INF/lib/webapp.jar!/com/mww/action/test.bmp
jar:file:/D:/wwwroot/WEB-INF/lib/webapp.jar!/com/mww/action/test.bmp

但是,输出的key.txt文件内容居然不一样!!Resin比Tomcat在文件的中间和结尾多出了8个字节。。。如下图:


 

这是咋子回事???

 

  • 写回答

1条回答 默认 最新

  • weixin_42518082 2013-08-05 14:17
    关注

    [color=darkblue][b]
    while (bmpInput.read(bytes) != - 1)

    sb.append(getHexString(bytes));

    这一段有问题哦
    bmpInput.read(bytes)返回的是读入字节的长度, 有可能是0 !

    而且你getHexString(bytes), 按你的程序, bytes数组里面不一定是全部读取出来的哦[/b][/color]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效