java - maven打包导致二进制文件大小被改变
伊谢尔伦
伊谢尔伦 2017-04-18 10:47:12
0
1
936

使用class.getClassLoader().getResourceAsStream()这种方法获取classpath下的文件流,读取出来的文件比写main方法读出来的文件大小更大。

问题已经解决。

本地main方法测试

使用tomcat做为容器运行同样代码时

相关代码:

 synchronized (PhoneNumberGeo.class) {
        if (dataByteArray == null) {
          ByteArrayOutputStream byteData = new ByteArrayOutputStream();
          byte[] buffer = new byte[1024];

          int readBytesLength;
          try {
            InputStream inputStream = PhoneNumberGeo.class.getClassLoader()
                    .getResourceAsStream("phone.dat");
              while ((readBytesLength = inputStream.read(buffer)) != -1) {
              byteData.write(buffer, 0, readBytesLength);
            }
            inputStream.close();
          } catch (Exception e) {
            System.err.println("Can't find phone.dat in classpath phone.dat");
            e.printStackTrace();
            throw new RuntimeException(e);
          }

          dataByteArray = byteData.toByteArray();
        }
      }
    }

    byteBuffer = ByteBuffer.wrap(dataByteArray);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    int dataVersion = byteBuffer.getInt();
    indexAreaOffset = byteBuffer.getInt();

完整代码:
开源代码github

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
黄舟

The problem has been solved~!
Summary: It is caused by placing a binary file on the classpath and using the maven-resources-plugin plug-in to copy the resource file.

In detail, there should be an option for the maven-resources-plugin plugin

  <filtering>true</filtering>

If it is turned on, any file to be copied in the classpath will be replaced by default, which means it will be mapped to properties
Then it can be used in the xml configuration, such as the jdbc.properties. But this operation is not suitable for binary files, such as png, gif, pdf, etc.
We need to exclude these file formats.

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>dat</nonFilteredFileExtension>
                        <nonFilteredFileExtension>swf</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

Thinking process:
I started to take a lot of detours. I thought it was because of the problem of the project’s referenced jar package, but after searching for a long time, I still couldn’t find the reason. Finally, I found out the md5 of each file and found that the files in the target directory were inconsistent with those in the resources directory, and finally found the problem.

Reference:
Maven Binary filtering
Other ways to get files under classpath

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!