Opening PDF file using window.open from server

我们两清 提交于 2019-12-02 09:18:59

Think about what you're doing. Do you really think that typing http://www.google.com/tmp/secretReport.pdf in your browser address bar would give you access to some secret report left in the /tmp directory on the file system of the google server?

You can't access the file system of a server over HTTP like this. What you need is a servlet, invoked by the browser, that reads the PDF file and sends it in the HTTP response.

What you have wrong:

  1. The file is created on server, thus it can't be opened on the client using JavaScript
  2. The file needs to download first, you need to provide the url mapped to the action, probably with parameter which file you want to download. If you create a file in another action, you could show the name of the file in the JSP to download. Just use the action property for file name.
  3. Java code in scriptlets is discouraged, use this code to implement the action used to return a stream result.

To your happiness there's an example to download a file with struts2. All you need is to apply it to your scenario.

I see other's have already answered your result, but I see you are having difficulty in understanding their answer. Well, then I have something simpler to start from.

Simply create a folder named /temp in your Web Pages (site root) directory and then put a PDF (report.pdf) then that link will work.

Now all you have to do is simply put your report in that file using simple File IO.

For e.g. if you wrote code to write a PDF file named xyz.pdf in the temp directory (already available in your web-root) then the access url will be /temp/xyz.pdf

Hope so this one helps.

Here's a small quick example on how to download file using struts2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!