How to use addResourcHandler when deploying as war

Hi,
when I use addResourceHandler and running in the Idea environment,
it works fine.
File file = new File(“files/myfile.pdf”);
file.exists = true

But when I deploy to war/Tomcat it is not working any more.
file.exist = false
I cannot find the reason…

public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/files/**").addResourceLocations("/my/local/filepath/");
}

I also tried it in another way, works fine in Idea as bootRun,
but not when deployed to Tomcat 10.

Resource res = resourceLoader.getResource(“file:/mypath/myfile.pdf”);
InputStream inStream = res.getInputStream();
byte[] pdf = inStream.readAllBytes();
ByteArrayInputStream in = new ByteArrayInputStream(pdf);
StreamResource resource = new StreamResource(fileName, () → in);
image.setsrc(resource);

like I said, works fine when run the app in Idea,
but throws and ioexecption when running in Tomcat, broken pipes…

:frowning: actually I got stucked

Hi!

This is because our IDE uses our project directory as its current working directory and the src/main/resources directory is right there for the application to read.

An artical with ways to work with filed with WAR/JAR may help you: How to Avoid the Java FileNotFoundException When Loading Resources | Baeldung

Regards,
Dmitriy

Hi Dmitriy,
I went through that doc and many others :slight_smile:

Actually I found out, that I only get the error when using Chrome or Firefox
and not when using Safari browser.
And there it is the same behavior when I run the app in Idea,
as java -jar or as a deployed war in Tomcat.

Which means from my view,
the issue is caused by the browsers I use.

I found a page where solution might be helpful for me too.
But would be great, if you could also look into that…

KR
Roland

2023-09-13T10:57:26.619+02:00 ERROR 48922 — [nio-8081-exec-9] i.j.f.exception.UiExceptionHandlers : There is no io.jmix.flowui.exception.UiExceptionHandler can handle the exception

org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:344) ~[catalina.jar:10.1.13]
at org.apache.catalina.connector.OutputBuffer.flushByteBuffer(OutputBuffer.java:773) ~[catalina.jar:10.1.13]
at org.apache.catalina.connector.OutputBuffer.append(OutputBuffer.java:676) ~[catalina.jar:10.1.13]
at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:379) ~[catalina.jar:10.1.13]
at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:357) ~[catalina.jar:10.1.13]
at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:97) ~[catalina.jar:10.1.13]
at org.springframework.security.web.util.OnCommittedResponseWrapper$SaveContextServletOutputStream.write(OnCommittedResponseWrapper.java:639) ~[spring-security-web-6.1.0.jar:6.1.0]

Interesting note. Surprisingly it depends on the browser.

Thanks for the link. :grinning:

Regards,
Dmitriy

again here the code I use to read from filesystem:
resPath: /Users/roland/…/filedir/
fileName: some.pdf

        Path pdfPath = Paths.get(resPath + fileName);
        byte[] pdf = Files.readAllBytes(pdfPath);
        ByteArrayInputStream in = new ByteArrayInputStream(pdf);
        StreamResource resource = new StreamResource(fileName, () -> in);
        imageID.setSrc(resource);
1 Like

and, I also tried it via iPhone… there the Chrome browser works fine…
so it looks like in my environment it is only an issue with the Chrome and Firefox browser
which are running on my Mac…
if you will ever find the root cause or how to handle it… pls tell me :slight_smile: