Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Name of file send by a servlet to a client browser

Status
Not open for further replies.

DeSn

Programmer
Nov 20, 2001
111
BE
What is wrong with the folowing code ?
The name of the file in htm always gets 'servletname'.html

I also tried to change inline to attachment but that dont solves the problem


------------------- code snippet --------

public void fileKlaarmaken(java.io.File file, int
outputformaat,HttpServletResponse res) throws ServletException,
IOException, FileNotFoundException
{
String fileName = file.getName();
ServletOutputStream out = res.getOutputStream();

if(outputformaat ==
be.vlaanderen.dov.prnlib.Renderer.RENDERER_HTML)
res.setContentType( "text/html" );
else
res.setContentType( "application/pdf" );


res.setContentLength((int) file.length());
res.setHeader("Content-Disposition","inline; filename="+fileName);

returnFile(file, out);
}



public static void returnFile(java.io.File file, OutputStream out) throws
FileNotFoundException, IOException
{
// A FileInputStream is for bytes

FileInputStream fis = null;
BufferedInputStream buffered = null;
try {
fis = new FileInputStream(file);
buffered = new BufferedInputStream(fis);
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = buffered.read(buf)) != -1)
//while ((bytesRead = fis.read(buf)) != -1)
{
out.write(buf, 0, bytesRead);
}
}

finally {
if (fis != null) fis.close();
if (buffered != null) buffered.close();
}
}

public static void returnFile(java.io.File file,
OutputStream out) throws FileNotFoundException, IOException
{
// A FileInputStream is for bytes

FileInputStream fis = null;
BufferedInputStream buffered = null;
try {
fis = new FileInputStream(file);
buffered = new BufferedInputStream(fis);
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = buffered.read(buf)) != -1)
//while ((bytesRead = fis.read(buf)) != -1)
{
out.write(buf, 0, bytesRead);
}
}

finally {
if (fis != null) fis.close();
if (buffered != null) buffered.close();
}
}


------------------- code snippet --------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top