Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to associate local file system with the path setting in WAR file.

Posted on 2004-08-10
5
Medium Priority
?
215 Views
Last Modified: 2009-12-16
I am using JBoss as my web server(on Win 2K) and have a web application( using Struts) in war file.
In my application, the client need to download some PDF files on server. These PDF files were created by another applcation(Jasper) and stored in server file system. I want to have a link on page point to the file, but I could not find a way to associate local file system with the path configuration in WAR file.
I searched forum, there are some topics about absolute path in WAR,

http://www.experts-exchange.com/Web/Application_Servers/Weblogic/Q_21004006.html

but I guess mine problem should be easier.


Can anyone give me some hint about this?
Thanks in advance!
0
Comment
Question by:leeson1125
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
kumvjuec earned 375 total points
ID: 11776812
I guess you will not want the web application to encapsulate those files. It will be very easy to open the files inside a servlet and redirect the stream to the browser when someone clicks on the links of files.
You will have to use a path string which is your path to the pdf files. then use something like this to get the linsting of the files(which can act as links too)

public static List getRecursiveList(File dir)
{
      List result = new ArrayList();
      List fd = Arrays.asList(dir.listFiles());
      Iterator iter = fd.iterator();
      while (iter.hasNext())
      {
            File file = (File)iter.next();
            if (file.isDirectory())
                  result.addAll(getRecursiveList(file));
            else
                  result.add(file);
      }
      return result;
}

Now, you can use the [paths - (folderpath)] i.e. the realtive paths as the hyperlinks, and whenever a user clicks that hyperlink, just open that file and redirect the fileoutputstream to the out stream.

out = new BufferedOutputStream( response.getOutputStream() );
is = new BufferedInputStream(new FileInputStream(outFile));
int c;
while( ( c=is.read() ) != -1 )
      out.write( c );

Make the content header inline or attachment depndin on whther you want to open in browser or download it.
0
 

Author Comment

by:leeson1125
ID: 11790080
Hi,
here is my implementation:

When system generated file, the absolute path will be store into database
On jsp page, using <a href="Download?reportId=324890" TARGET="_blank">Download </a>.
URL pattern \Download will be mapped to DownLoadServlet where java io is used to  "open that file and redirect the fileoutputstream to the out stream", but using absolute path of file.

It seems my implementation is different from your suggestion. I can't understand your "Now, you can use the [paths - (folderpath)] i.e. the realtive paths as the hyperlinks, and whenever a user clicks that hyperlink".
Could you give me more detailed information?
Thanks in advance. :-)
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11790995
ahhh,
I thought that you are implementing without db. Well, now as i understand you, u want to use relative path instead of absolute path for your files with respect to your web application path.
Is that right???
0
 

Author Comment

by:leeson1125
ID: 11806731
No, I have DB and most of my implementation is similiar to your suggestion.

my servlet:
    public void doPost(HttpServletRequest request,
              HttpServletResponse response){

        WebApplicationContext ctx
            = WebApplicationContextUtils.getWebApplicationContext(
                request.getSession().getServletContext());
        //I am using Spring framework.

        Report report = (Report)ctx.getBean("report");        
        String reportKey = request.getParameter("reportId");
       
        OutputStream outStream = response.getOutputStream();                
        String filePath = report.getReportPathByHashKey(reportKey);

       
        File file = new File(filePath);
        FileInputStream fin = new FileInputStream(file);
                                                                                                     
        response.setContentType("application/pdf");
        //response.setContentType("application/OCTET-STREAM");
        //response.setHeader("Content-Disposition", "filename=\"Report.pdf\"");
        response.setHeader("Content-Disposition", "attachment; filename=\"Report.pdf\"");
        //response.setHeader("Content-Disposition", "filename=\"Report.pdf\"");
       // response.setHeader("Cache-Control", "no-cache");
       
        byte[] buf = new byte[1024];
       
        int sizeRead = 0;
        while ((sizeRead = fin.read(buf, 0, buf.length)) != -1){
            outStream.write(buf, 0, sizeRead);
        }
         
        outStream.flush();
        fin.close();
        out.close();
}

The link on my JSP:
<a href="Download?reportId=324890" TARGET="_blank">Download </a>

When user click the link on jsp, it will popup a blank window and a download dialog asking user to choose"Open", "Save". If user click Open, then the popup window will close and the file will be opened by Acrobat reader, but if Save is clicked, after downloading finish, the popup window is still there.
Do you know how to close that popup window?
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11807167
Why are you using
>>TARGET="_blank"

Since your servlet puts the stream as attachment, the browser will itself throw the dialog of open/save. The above line opens a new window. Try removing it.
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question