3

I have a complicated issue.

I save a pdf file under WEB-INF/folder/test.pdf But now, I want to be able to download that pdf file in a JSP file. I know that files in WEB-INF are not visible by URL, but I have tried all the answers possible in this page and none work.

String pathDownload = ((ServletContext) ActionContext.getContext().get(StrutsStatics.SERVLET_CONTEXT)).getRealPath("/WEB-INF/folder/")+ "\\";

And in my JSP file I got:

<a href="<%=pathDownload%>/test.pdf" >

So, URL route is correct, but it does nothing when clicking, returning null.

I tried to write something in the file web.xml, but nothing works.

Also tried InputStream in the execute() in that class who extends ActionSupport implements SessionAware

Can anyone help with an explanation what must I do?

Roman C
  • 49,761
  • 33
  • 66
  • 176
DeathGun
  • 79
  • 2
  • 10
  • 5
    You *know that files in WEB-INF are not visible by URL*, so you need to open this file in action and stream it to client. – Aleksandr M Feb 09 '16 at 10:48

2 Answers2

2

It's not relevant that the client side "knows" about actual path on the server side. So, it won't help if you "tell" the client side about the actual path. Instead of: <a href="<%=pathDownload%>/test.pdf" >, just do <a href="download.action?file=folder/test.pdf" >. To make this example to work, you have to map the download url action with an action that have a stream result type:

<action name="download">
    <result name="success" type="stream">
        <param name="contentType">application/pdf</param>
        <param name="inputName">fileStream</param>
        <param name="contentDisposition">attachment;filename="document.pdf"</param>
        <param name="bufferSize">1024</param>
    </result>
</action>

In your DownloadAction.java, you have just to read the file mentioned by the file parameter and create a property called fileStream with the file content:

public class DownloadAction extends ActionSupport{

    private InputStream fileStream;
    private String file;

    public InputStream getFileStream() {
        return fileStream;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public String execute() throws Exception {
        fileInputStream = new FileInputStream(
            new File(
                ((ServletContext) ActionContext.getContext().get(StrutsStatics.SERVLET_CONTEXT))
                .getRealPath("/WEB-INF/folder/")
                +
                file
            )
        );
        return SUCCESS;
    }
}
Hamdi Douss
  • 1,033
  • 1
  • 8
  • 17
0

You need to pass the path of the file to the action method of your action class like this

<s:a href="download.action?file=%{pathDownload}/test.pdf">Download</s:a>

Property file of type String will be used populate the parameter value.

Then use to initialize a inputStream property as getResourceAsStream() and return stream type result providing a getter for the inputStream property.

If you are using convention configuration you can find similar implementation here.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Great! Please, accept the answer, if you don't know how to do it read [this](http://stackoverflow.com/tour). – Roman C Feb 10 '16 at 12:31