T
TomG
Hi All,
I am replacing a Netscape Application Server appLogic with a servlet.
The servlet is supposed to take a URL and stream the file to the
user's browser. I checked a number of examples from post in this news
group to get examples of how to do this (thanks for the good info).
The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
the current browser session), but always pops up the file download box
in IE 6. In IE the file is always downloaded and opened. This
happens even for html files. This is a problem, since the appLogic
opens most files in the browser, Word docs get opened in a separate
instance of MS Word. However, the user never sees the download box.
I tried modifiying the web.xml for this servelet to allow adding the
filename to to be appended to the servlet name,
GetContentItem/filename.jpg, but that didn't make any difference. The
content type and filename appear appear in the download dialog.
Here is the servelt code, any suggestions will be appreciated:
public class GetContentItem extends HttpServlet {
// initalise the servlet within init()
private static Log log = LogFactory.getLog(GetContentItem.class);
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
if (log.isDebugEnabled())
log.debug("IN " + GetContentItem.class + " doGet" );
String sCRBaseURLRoot =
"http://supporttest1.web.lucent.com:20101/cr/";
String sDocPath = "Live/Product/5ESS/R16/Retrofit_Information/090094058001c628.jpg";
String sDocPath2 =
"Live/Product/5ESS/R16/Release_Information/0900940580026c41/french.pdf";
String sDocPath3 =
"/Live/Product/5ESS/R16/Release_Information/0900940580026cc1/spanish.doc";
String sDocPath4 =
"/Live/Product/5ESS/R16/Release_Information/090094058002791c/61402_Release_Deployment.html";
String filename = "090094058001c628.jpg";
String filename2= "french.pdf";
String filename3= "spanish.doc";
String filename4 = "61402_Release_Deployment.html";
sDocPath=sDocPath4;
filename = filename4;
String sUrl = sCRBaseURLRoot + sDocPath;
java.io.InputStream in=null;
ServletOutputStream stream = null;
try{
URL u = new URL(sUrl);
URLConnection c = u.openConnection();
c.connect();
in = new DataInputStream(c.getInputStream());
String sHeaderField=null;
String sHeaderFieldKey=null;
int i = 1;
stream = response.getOutputStream();
response.setContentType(c.getContentType());
response.setContentLength(c.getContentLength());
response.setHeader("Content-disposition","attachment;filename=\"" +
filename + "\"");
int bytes_read;
int BUFSIZE = 8192;
byte[] buffer = new byte[BUFSIZE];
while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
stream.write(buffer,0,bytes_read);
}
} catch(FileNotFoundException e){
log.error("FileNotFoundException accessing document " + sDocPath +
e );
} catch(Exception e){
log.error("Exception accessing document " + sDocPath + e );
} finally{
try{
in.close();
stream.flush();
stream.close();
} catch (Exception e){}
}
}
}
I hope the code display format is not too ugly in the post.
Thanks,
Tom
I am replacing a Netscape Application Server appLogic with a servlet.
The servlet is supposed to take a URL and stream the file to the
user's browser. I checked a number of examples from post in this news
group to get examples of how to do this (thanks for the good info).
The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
the current browser session), but always pops up the file download box
in IE 6. In IE the file is always downloaded and opened. This
happens even for html files. This is a problem, since the appLogic
opens most files in the browser, Word docs get opened in a separate
instance of MS Word. However, the user never sees the download box.
I tried modifiying the web.xml for this servelet to allow adding the
filename to to be appended to the servlet name,
GetContentItem/filename.jpg, but that didn't make any difference. The
content type and filename appear appear in the download dialog.
Here is the servelt code, any suggestions will be appreciated:
public class GetContentItem extends HttpServlet {
// initalise the servlet within init()
private static Log log = LogFactory.getLog(GetContentItem.class);
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
if (log.isDebugEnabled())
log.debug("IN " + GetContentItem.class + " doGet" );
String sCRBaseURLRoot =
"http://supporttest1.web.lucent.com:20101/cr/";
String sDocPath = "Live/Product/5ESS/R16/Retrofit_Information/090094058001c628.jpg";
String sDocPath2 =
"Live/Product/5ESS/R16/Release_Information/0900940580026c41/french.pdf";
String sDocPath3 =
"/Live/Product/5ESS/R16/Release_Information/0900940580026cc1/spanish.doc";
String sDocPath4 =
"/Live/Product/5ESS/R16/Release_Information/090094058002791c/61402_Release_Deployment.html";
String filename = "090094058001c628.jpg";
String filename2= "french.pdf";
String filename3= "spanish.doc";
String filename4 = "61402_Release_Deployment.html";
sDocPath=sDocPath4;
filename = filename4;
String sUrl = sCRBaseURLRoot + sDocPath;
java.io.InputStream in=null;
ServletOutputStream stream = null;
try{
URL u = new URL(sUrl);
URLConnection c = u.openConnection();
c.connect();
in = new DataInputStream(c.getInputStream());
String sHeaderField=null;
String sHeaderFieldKey=null;
int i = 1;
stream = response.getOutputStream();
response.setContentType(c.getContentType());
response.setContentLength(c.getContentLength());
response.setHeader("Content-disposition","attachment;filename=\"" +
filename + "\"");
int bytes_read;
int BUFSIZE = 8192;
byte[] buffer = new byte[BUFSIZE];
while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
stream.write(buffer,0,bytes_read);
}
} catch(FileNotFoundException e){
log.error("FileNotFoundException accessing document " + sDocPath +
e );
} catch(Exception e){
log.error("Exception accessing document " + sDocPath + e );
} finally{
try{
in.close();
stream.flush();
stream.close();
} catch (Exception e){}
}
}
}
I hope the code display format is not too ugly in the post.
Thanks,
Tom