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>