0

iam reading file from classpath config/sample-real.pdf, real size of file is 106 041 bytes, but when i run the same code in one spring boot app v2.4.3 it content length is good 106 041 bytes but on the other spring boot app v2.3.2.RELEASE it return contentlength = 149372 bytes and the pdf is corrupted when return to ResponseEntity<byte[]>

iam posting the code which working on one app and the same not working on the other.

public ResponseEntity<byte[]> generatePdf(String uid) throws IOException {
ClassPathResource file2 = new ClassPathResource("config/sample-real.pdf");

long lengthFile2 = file2.contentLength();//length here is corruputed 149372 instead of 106041
byte[] byteArr = IOUtils.toByteArray(file2.getInputStream());

HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders
    .put("Content-Disposition", Collections.singletonList("attachment; filename=sample.pdf"));

return ResponseEntity.ok()
    .headers(responseHeaders)
    .contentType(MediaType.APPLICATION_PDF)
    .contentLength(byteArr.length)
    .body(byteArr);

}

working sample has spring-core:5.3.4, other spring boot app which does not work with this code is spring-core:5.2.8.RELEASE - i tried to have the same spring-core but it does not work either.

UPDATE ive got update, reading from classpath return corruput pdf, reading from file C://sample-real.pdf returning good content

File f ile1= new File("C:\\sample-real.pdf");
byte[] fileContent = Files.readAllBytes(file1.toPath());   //good 106k bytes

File resource = new ClassPathResource("config/sample-real.pdf").getFile();
byte[] fileContent2 = Files.readAllBytes(resource.toPath());  //bad 146k bytes

c:sample-real.pdf has size = 103 KB (106 041 bytes) and config/sample-real.pdf the same!

UPDATE2: On jar there are corrputed length of 146k bytes when i made clean install

SOLUTION files got corrupted because <filtering>false</filtering> in pom

  <build>
    <finalName>${project.artifactId}</finalName>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering> //this WAS TRUE! i will need to check why, thanks everyone
        <excludes>
          <exclude>**/*.jks</exclude>
        </excludes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>false</filtering>
        <includes>
          <include>**/*.jks</include>
        </includes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

2 Answers 2

0

Update your spring-boot and spring-framework.

While we're lacking the minimal reproducing error code, reasons for not upgrading spring and actual error messages there's no way to tell the actual reason behind the "corruption".

Sign up to request clarification or add additional context in comments.

3 Comments

@user2859429: have you manually inspected the produced jar files to see if the resource in the finally built jar is already corrupted? It's possible that your build applies some transformation.
@JoachimSauer youve got right, on jar there are corrupted filesize 146k bytes
@user2859429: then the culprit is most likely the build system: something is probably processing your PDF file, assuming it's text or something like that. Your code is probably not the issue.
0

As the "filter" of the resources enable variable sostitution, the pom you have was correct (see https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html).

As you can see, you have two resource section (for the same folder), one with filtering enabled and the other with filtering disabled.

The most correct answer shoulbe to add **/*.pdf to the exclude list of the first resource tag and to the include list of the second include tag.

<build>
<finalName>${project.artifactId}</finalName>
<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering> //this SHOULD BE TRUE to enable variable sostitution
    <excludes>
      <exclude>**/*.jks</exclude>
      <exclude>**/*.pdf</exclude> //added pdf to exclude list
    </excludes>
  </resource>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>false</filtering>
    <includes>
      <include>**/*.jks</include>
      <include>**/*.pdf</include> //added pdf to include list
    </includes>
  </resource>
</resources>
<plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
</plugins>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.