aspose file tools*
The moose likes Servlets and the fly likes Implementing download - XLS files - JSP scriptlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Implementing download - XLS files - JSP scriptlet" Watch "Implementing download - XLS files - JSP scriptlet" New topic
Author

Implementing download - XLS files - JSP scriptlet

Jax Desi
Greenhorn

Joined: Dec 03, 2003
Posts: 2
Hello
I've a strange problem.
I displaying a directory listing to the user. The user selects a file to be downloaded. The files are of diff formats - txt, csv, xls, pdf, zip
The download works for pdf, txt but for XLS, when I save the file, its has junk.
Any pointer ???

----------------Attached source. It is part of JSP not a servlet

String _fileLocation = "C:/test.xls";

String _fileName = "ttt.XLS";
String _fileType = "application/vnd.ms-excel";

BufferedInputStream bufferedInputStream = null;
File inputFile = new java.io.File(_fileLocation);
int fileSize = (int) inputFile.length();
System.out.println("fileLocation");
System.out.println(_fileLocation);
System.out.println("fileSize");
System.out.println(fileSize);
if (fileSize > 0)
{
FileInputStream fis = new FileInputStream(inputFile);
bufferedInputStream =
new BufferedInputStream(new FileInputStream(inputFile));
byte[] fByte = new byte[fileSize];
if (fis.read(fByte, 0, fileSize) >= 0)
{
//Committing the
response.setContentType(_fileType);
//response.setContentLength(fByte.length);
response.setHeader(
"Content-Disposition",
"attachment; filename="+ _fileName);
response.getOutputStream().write(fByte);
response.getOutputStream().flush();
response.getOutputStream().close();
}
fis.close();
}
else
{
System.out.println("The report file is empty");
}
-----------------------
Thanks for help
[ December 03, 2003: Message edited by: Jax Desi ]
Jax Desi
Greenhorn

Joined: Dec 03, 2003
Posts: 2
It doesnt work for word (.doc) and image(.gif). It works for csv, pdf, txt, zip
Originally posted by Jax Desi:
Hello
I've a strange problem.
I displaying a directory listing to the user. The user selects a file to be downloaded. The files are of diff formats - txt, csv, xls, pdf, zip
The download works for pdf, txt but for XLS, when I save the file, its has junk.
Any pointer ???

----------------Attached source. It is part of JSP not a servlet

String _fileLocation = "C:/test.xls";

String _fileName = "ttt.XLS";
String _fileType = "application/vnd.ms-excel";

BufferedInputStream bufferedInputStream = null;
File inputFile = new java.io.File(_fileLocation);
int fileSize = (int) inputFile.length();
System.out.println("fileLocation");
System.out.println(_fileLocation);
System.out.println("fileSize");
System.out.println(fileSize);
if (fileSize > 0)
{
FileInputStream fis = new FileInputStream(inputFile);
bufferedInputStream =
new BufferedInputStream(new FileInputStream(inputFile));
byte[] fByte = new byte[fileSize];
if (fis.read(fByte, 0, fileSize) >= 0)
{
//Committing the
response.setContentType(_fileType);
//response.setContentLength(fByte.length);
response.setHeader(
"Content-Disposition",
"attachment; filename="+ _fileName);
response.getOutputStream().write(fByte);
response.getOutputStream().flush();
response.getOutputStream().close();
}
fis.close();
}
else
{
System.out.println("The report file is empty");
}
-----------------------
Thanks for help
[ December 03, 2003: Message edited by: Jax Desi ]
Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362
Use Servlets to set the content type instead of JSp and it will work
Amol Takate
Greenhorn

Joined: Sep 20, 2003
Posts: 15
you will have to set a _fileType according to corresponding file.
For xml/xsl you will have to set it as text/xml
For doc file it is application/msword
For gif file it is image/gif
 
 
subject: Implementing download - XLS files - JSP scriptlet