• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1312
  • Last Modified:

URL to open a Word document works on IE-7 but not on FF-4 or Chrome 10.0

I have a JSP page that has this link:

file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc

This is on a corporate intranet and everyone has access to this directory.

The file opens as expected in IE-7, but it won't open in FF-4 or Chrome 10.0. On FF and Chrome, it looks like a link but nothing happens when I click on it.
0
Alaska Cowboy
Asked:
Alaska Cowboy
  • 25
  • 13
4 Solutions
 
Alaska CowboyAuthor Commented:
When I enter the URL above in a browser (FF and Chrome), the link works, as expected.

But the link doesn't work when it's embedded in a JSP page running on Tomcat.

I have verified the links are the same (by copying them out), but it doesn't work when running from the JSP rendered page.

file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc - works from browser a test HTML page
file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc - doesn't work from JSP / Tomcat
0
 
rrzCommented:
Maybe this Servlet will work. What is your web server ? Tomcat 7 ? Do you know how to map a servlet in your web app's web.xml file  or use annotations ?
package rrz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
             InputStream in = null;
             OutputStream out = null;
             try {
                  File file = new File("R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc");
                  String filename = "test.doc";
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/msword");
                  ServletOutputStream sos = response.getOutputStream();
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND );
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
    }
}

Open in new window

0
 
Alaska CowboyAuthor Commented:
rrz, ok, thank you, I will give it a try.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Alaska CowboyAuthor Commented:
rrz, it looks like you wrote what I need, but not sure how to wire everything.

I'm running Tomcat 5.5.

What you wrote looks like it might do the trick, but I have a few questions:

- where does this code go ?
- how is it called ? I would have to pass in "R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc"

Thanks.

0
 
rrzCommented:
It would nice of you to increase the points for this question to 500.  
Since you didn't answer my questions, I assume you  need to see  
   <servlet>
        <servlet-name>fileServlet</servlet-name>
        <servlet-class>rrz.DownloadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>fileServlet</servlet-name>
        <url-pattern>/getDoc</url-pattern>
    </servlet-mapping>
which needs to be copied into your web app's web.xml file.
In your JSP, you need to have something like  
<%
   String fileName = "R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_035_2011_04_26_at_10_33.doc";
%>
<html>
<head>
    <title>Get Doc</title>
</head>
<body>
<a href="getDoc?fileName=<%=fileName%>">R drive, April 2011</a>
 </body>
</html>

Open in new window

The Servlet code is
package rrz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         InputStream in = null;
         OutputStream out = null;
         String fileName = request.getParameter("fileName");
         if(fileName != null){
             try {
                  File file = new File(fileName);
                  String filename = "test.doc"; 
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/msword");
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
         } else response.sendError( HttpServletResponse.SC_BAD_REQUEST); 
    }
}

Open in new window

0
 
Alaska CowboyAuthor Commented:
Yes, I needed the web.xml info, thanks.

Still need to know where the code goes and how to pass the file name.

I will increase the points when I'm back on the computer, I don't think I can do it in mobile.
0
 
rrzCommented:
>Still need to know where the code goes
You have to compile the Servlet and put the class file into your web app's classes folder. For example
  WEB-INF/classes/rrz/DownloadServlet.class  
>how to pass the file name.  
For testing just use the JSP I posted.  
Where is the file name coming from ? Are you going to have a list of them for the user to choose from ?

0
 
Alaska CowboyAuthor Commented:
Ok, thanks. The file name comes from Oracle.

I will get back to this Monday.
0
 
Alaska CowboyAuthor Commented:
increasing points.
0
 
Alaska CowboyAuthor Commented:
rrz, in your servlet code, is this line needed -->String filename = "test.doc";                

I'm assuming that's left over and now shouldn't be there.
0
 
Alaska CowboyAuthor Commented:
where should I put the servlet ?

in my java components, I have
- dao
- domain
- web

I'm guessing web, but is there a standard place ?
0
 
Alaska CowboyAuthor Commented:
well, it's set up but not working properly, I get this: "Error 404: SRVE0190E: File not found: /getDoc"

I removed the "test.doc" reference, and I double-verified the file actually exists and can be opened.

here's the link: http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

and this opens from FF
file:///R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc
/********* Call getDoc *********/

<c:otherwise><a href="getDoc?filename=<c:out value="${hist.cdrReportLoc}"/>"><c:out value="${hist.procDescription}"/></a></c:otherwise>	

/******** Servlet, only thing I changed was removing "test.doc" ********/
package com.carefirst.edw.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         InputStream in = null;
         OutputStream out = null;
         String fileName = request.getParameter("fileName");
         if(fileName != null){
             try {
                  File file = new File(fileName);
                  // String filename = "test.doc"; 
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/msword");
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
         } else response.sendError( HttpServletResponse.SC_BAD_REQUEST); 
    }
}

Open in new window

0
 
rrzCommented:
>in your servlet code, is this line needed -->String filename = "test.doc";  
Yes, it is needed because that is the file name that  is used in "Content-Disposition". It will appear in the dialog box that asks the user to open MS Word.  You can name it anything you want to name it. You could also parse the file name from the url that came from the database and use that.
>where should I put the servlet ?
I don't know your development environment. Are you using a IDE ?  Anyway the Servlet needs to be compiled and put into its package folder and that should placed into your web app's classes folder.
WEB-INF/classes/com/carefirst/edw/web/DownloadServlet.class  
 Or if it is being put into a jar that should be placed into your web app's lib folder.
>well, it's set up but not working properly,
Please post how you mapped the Servlet in your web app's web.xml file ?
>I get this: "Error 404: SRVE0190E: File not found: /getDoc"  
This is telling us that the Servlet is not being found.  
For testing, begin with a Simple Servlet. Try using the Servlet below here. Try browsing to  
http://localhost:9080/edw/getDoc
or use  
<a href="getDoc"></a>  
in a JSP that is in your web app's root folder.
The init method was included for debugging. It can be removed later. But for now you should see the init message in your console when the Servlet is loaded.
package com.carefirst.edw.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/plain");
        response.getWriter().write("This is a test!);
    }
}

Open in new window

0
 
Alaska CowboyAuthor Commented:
oh, I'm pretty sure I didn't map this in the web.xml file.

so everything is looking good so far, hopefully this will fix it. Unfortunately, I won't be able to get to this until later today or possibly not until tomorrow.
0
 
rrzCommented:
You never told me which web server you are using. If you using Servlet 3.0 API then you could use annotations instead of mapping.  If you using anything less than Servlet 3.0  then you will have to use mapping.
0
 
Alaska CowboyAuthor Commented:
I'm using Tomcat 5.5.

How would I know if I'm using Servlet 3.9 API or not ?

also, another question, what does this mean: "@Override" - - - I've never seen anything like that.
0
 
rrzCommented:
>I'm using Tomcat 5.5.
You will have to use mapping.  
>How would I know if I'm using Servlet 3.9 API or not ?  
Put the following in a JSP  
Server is <%= application.getServerInfo()%><br/>
JSP version is <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion()%><br/>
Servlet API is <%= application.getMajorVersion()%>.<%=application.getMinorVersion()%>  

>@Override  
That is an annotation. In this case it tells compiler that the method is overriding a method in HttpServlet class.
0
 
Alaska CowboyAuthor Commented:
ok on mapping and determining what I am using.

also ok on override, so I'm learning a variety of things here, thanks.

Still tied up though, won't be able to try and fix this until later today or tomorrow morning.
0
 
Alaska CowboyAuthor Commented:
Ok, I got the web.xml updated, here:
   <servlet>
        <servlet-name>fileServlet</servlet-name>
        <servlet-class>com.carefirst.edw.web.DownloadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>fileServlet</servlet-name>
        <url-pattern>/getDoc</url-pattern>
    </servlet-mapping>

but now I get the non-informative message: "Error 400: Error reported: 400"
0
 
Alaska CowboyAuthor Commented:
well, I modified the classpath to this:

<servlet-class>WEB-INF.classes.com.carefirst.edw.web.DownloadServlet</servlet-class>

and got this error message:

Error 404: SRVE0203E: Servlet [fileServlet]: WEB-INF.classes.com.carefirst.edw.web.DownloadServlet was found, but is missing another required class. SRVE0206E: This error typically implies that the servlet was originally compiled with classes which cannot be located by the server. SRVE0187E: Check your classpath to ensure that all classes required by the servlet are present.SRVE0210I: This problem can be debugged by recompiling the servlet using only the classes in the application's runtime classpath SRVE0234I: Application classpath=
0
 
rrzCommented:
>"Error 400: Error reported: 400"
That is telling us that the request was bad (syntactically incorrect).
What did you request ?  
Are you using the simple test Servlet  in my last post ?  
Did you browse to  
http://localhost:9080/edw/getDoc 
?
Is edw the name of your web app ?
><servlet-class>WEB-INF.classes.com.carefirst.edw.web.DownloadServlet</servlet-class>
That is wrong. You had it right before. Use
<servlet-class>com.carefirst.edw.web.DownloadServlet</servlet-class>  
The classes folder is in Tomcat's  classpath.  
Is the file  DownloadServlet.class  in  
<yourTomcatHome>/webapps/edw/WEB-INF/classes.com/carefirst/edw/web  
?  
Is the first line of your Servlet
package com.carefirst.edw.web;
?
0
 
Alaska CowboyAuthor Commented:
sorry, here's the request:
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

the application edw works great, just trying to get this servlet to work.

>Are you using the simple test Servlet  in my last post ?  
- yes, see below, pretty much as is

>Use <servlet-class>com.carefirst.edw.web.DownloadServlet</servlet-class>  
 -ok

>Is the file  DownloadServlet.class  in  <yourTomcatHome>/webapps/edw/WEB-INF/classes.com/carefirst/edw/web
- yes

now getting this again: "Error 400: Error reported: 400 "

We can do this tomorrow if that's better.
package com.carefirst.edw.web;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {
    @Override
    public void init(){
              System.out.println("init of DownloadServlet");
    }
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         InputStream in = null;
         OutputStream out = null;
         String fileName = request.getParameter("fileName");
         if(fileName != null){
             try {
                  File file = new File(fileName);
                  String filename = "cdr_report.doc"; 
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/msword");
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
         } else response.sendError( HttpServletResponse.SC_BAD_REQUEST); 
    }
}

Open in new window

0
 
rrzCommented:
Ok.  Tomorrow(morning only) is good for me.
I see the problem.  In the code we have

if(fileName != null){
...
} else response.sendError( HttpServletResponse.SC_BAD_REQUEST);

fileName was null  therefore the SC_BAD_REQUEST was sent .
You used  
  http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 
But the Servlet is looking for the parameter "fileName"  
Try using
http://localhost:9080/edw/getDoc?fileName=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 
Java is case sensitive.  
0
 
Alaska CowboyAuthor Commented:
I don't get what you are saying . . . fileName should be populated from "request.getParameter("fileName");"

Here's how I call the servlet in the jsp:
<c:otherwise><a href="getDoc?filename=<c:out value="${hist.cdrReportLoc}"/>"><c:out value="${hist.procDescription}"/></a></c:otherwise>                                    

and it gets formed properly as
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 


0
 
Alaska CowboyAuthor Commented:
got it ! woo-hoo !

I had to get the parameter filename in the model, then I had to fiddle with the case and finally got it, yea . . .
0
 
Alaska CowboyAuthor Commented:
from my Controller:

String fileName = request.getParameter("filename");
...
model.put("fileName",fileName);

0
 
rrzCommented:
I should have named the form parameter something else so it would not have been confusing.  
One more thing that might be a problem later.
You should really encode the parameter's value when inserting into the query string.  The value you posted worked without doing so but you might change it later and it might break the code.  
<a href="<c:url value="getDoc"> 
             <c:param name="fileName" value="${hist.cdrReportLoc}" />
       </c:url>">${hist.procDescription}</a> 

Open in new window

This way the <c:url> and <c:param> tags will encode any special characters. As you can see I didn't use <c:out> tag around your EL values. That should not be necessary.
0
 
Alaska CowboyAuthor Commented:
rrz, ok, thanks again, the users will love it :-)
0
 
Alaska CowboyAuthor Commented:
shucks, I got this error when I moved it out of my local development . . .

HTTP Status 404 -

type Status report

message

description The requested resource () is not available.
Apache Tomcat/5.5.31


The URL should work . . .
http://xpc-120268/edwdev/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc

same as local development
http://localhost:9080/edw/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc
0
 
rrzCommented:
>description The requested resource () is not available.  
This message tells us that the file could not be found. In the code  
catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND);
             }

Open in new window

 I guess we could improve this by using  
catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND,"The file " + fileName + " could not be found.");
             }

Open in new window

0
 
rrzCommented:
Or if you changed the variable to filename, then use
catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND,"The file " + filename + " could not be found.");
             }

Open in new window

This will send the message along with error.
0
 
Alaska CowboyAuthor Commented:
ok, but it's the same data (development), just on a different server.  My local development environment and then the regular public dev environment both point to the same database . . .

I'll put the error message in as you describe.

I can open another question for this . . .  
0
 
rrzCommented:
>I can open another question for this . . .  
That is not necessary for me. But I will be going offline soon. So if you want someone else to help later today then go ahead.
>ok, but it's the same data (development), just on a different server.
There must some difference. Recheck the file location.
Add the debug code
System.out.println("file path is " + fileName);
right after the line that gets the parameter from the request.
0
 
Alaska CowboyAuthor Commented:
ok, I will debug some more but will open another question if I can't fix it. thanks a lot.
0
 
Alaska CowboyAuthor Commented:
this is the error message on local development when the file name is bad:
Error 404: The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_XXX_17_02.doc could not be found.

it shows up just as written above on a white screen inside a "thick-box" (I forced it to be bad with "XXX")

on the Tomcat server, I get this, also inside the thick-box
HTTP Status 404 - The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc could not be found.

type Status report

message The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc could not be found.

description The requested resource (The file R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc could not be found.) is not available.
Apache Tomcat/5.5.31


something's obviously different, I'll open another question
0
 
rrzCommented:
I'm on my out, but I did see your other question.  Your url
http://xpc-120268/edwdev/getDoc?filename=R:/FTP/Highmark/data/test/ods_cdr_tab_cnt_104_2011_04_26_at_17_02.doc 
still has
filename
when the Servlet is looking for
fileName  
Also you should add the <c:url> and <c:param> tags that I posted.
0
 
Alaska CowboyAuthor Commented:
but why would it work on WebSphere and not Tomcat ? I thought I had resolved the fileNname thing since it worked in development.

Ok, I'll add the <c:url> and <c:parm> and move to the new question, thanks again.
0
 
Alaska CowboyAuthor Commented:
first servlet, aw . . .
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 25
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now