• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing download - XLS files - JSP scriptlet

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Servlets to set the content type instead of JSp and it will work
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic