Solved

Compressed file download using servlets

Posted on 2002-06-26
16
513 Views
Last Modified: 2013-11-24
Hi all,

I am trying to download a compressed file (GZIP format)in my unix server using servlet. But it is not sending the file. It is just sending 1K data(junk). I am tired of trying all input /output stream combinations. I am able to download normal text files. I am having problem with binary file.Any help would be appreciated.

*******************

import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Download extends HttpServlet
{
        public void doPost(HttpServletRequest req ,HttpServletResponse res) throws IOException,ServletException
       {

String downloadFile = "/home/hqdev/test.txt.gz";
ServletOutputStream outputstream = res.getOutputStream();
BufferedInputStream inputstream = new BufferedInputStream(new FileInputStream(downloadFile));

 res.setHeader("Content-encoding","x-gzip");
 res.setContentType("text/plain");
 res.setHeader("Content-Disposition","attachment; filename=\"" +file+ "\";");
 res.setHeader("Accept-ranges","bytes");

  int data;
 while((data = inputstream.read()) != -1)
 {
         outputstream.write(data);
 }
}
}
0
Comment
Question by:subbaiah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +2
16 Comments
 

Expert Comment

by:pothuganti_srinivas
ID: 7111517
files with gz extension are not plain text files. To download them set the content type to "application/gzip".

res.setContentType("application/gzip");

0
 
LVL 1

Author Comment

by:subbaiah
ID: 7111550
thanks for the reply

I tried all the possible content types

application/gzip
application/octet
application/octet-stream
application/x-filler
text/plain

still i am getting the same 1k junk file. :( :(



0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 7112461
Try getting rid of this line:

res.setHeader("Content-encoding","x-gzip");

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:rjackman
ID: 7112940
if u are accessing through browser then y r u trying to write the data using stream
u can try just a simple ahref in HTML
String downloadFile = "/home/hqdev/test.txt.gz";
res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
out.println("<html>");
out.println("<body>");
out.println("<a href="+downloadFile +">download</a>");
out.println("</body>");
out.println("</html>");
browser will automatically try to download the file
cheers
RJ
0
 
LVL 3

Expert Comment

by:rjackman
ID: 7112943
hi again in case of text/word files u need to read the file explicitly coz browser will open those files
but in case of zip/gzip files browser tries to download file so u will get the download dialog
RJ
0
 
LVL 1

Author Comment

by:subbaiah
ID: 7113472
Hi objects,

I removed that line. still it is not working.
res.setHeader("Content-encoding","x-gzip");

can you try the same program in your environment?

Hi rjackman,

For security reasons, the download file is in the application server, not in the webserver. In my setup, web server and application server are in different unix boxes. Only valid users should be able to download the file.
0
 
LVL 92

Expert Comment

by:objects
ID: 7115471
The above code doesn't actually compile, the 'file' variable is not defined.

try *just* setting the following header, it works for me:

response.setContentType("application/octet-stream" + "; name=\"" + filename + "\"");
response.setHeader("Content-Disposition","attachment; filename=" +filename+";");
0
 
LVL 3

Expert Comment

by:rjackman
ID: 7116015
try with this 1
response.setContentType("application/save");
response.addHeader("Content-Disposition:attachment", "filename=abc.xxx");
0
 
LVL 1

Author Comment

by:subbaiah
ID: 7117380
Hi objects,

i tried your headers. still no luck. Actually my program is working fine with any text file. The problem is only with compressed binary files. did you try to download the file which is compressed using gzip?

Hi rjackman,

i tried your content type. its not working.


0
 
LVL 92

Expert Comment

by:objects
ID: 7117545
Post your current code and i'll try it here.
0
 
LVL 1

Author Comment

by:subbaiah
ID: 7121648
My current code

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Download extends HttpServlet
{
        public void doPost(HttpServletRequest req ,HttpServletResponse res) throws IOException,ServletException
        {
// file is the gzip file name whose relative path
// is /web/home/subbaiah

          String file = req.getParameter("file");
          String downloadFile  = "/web/home/subbaiah/"+file;                
         
                ServletOutputStream outputstream = res.getOutputStream();
                BufferedInputStream inputstream = new BufferedInputStream(new FileInputStream(downloadFile));


                res.setContentType("application/x-gzip");
                res.setHeader("Content-Disposition","attachment; filename=\"" +file+ "\";");
               
                int data;

             byte[] buffer = new byte[4096];
             while((data = inputstream.read(buffer))!=-1) {
                     outputstream.write(buffer);

             }
                inputstream.close();
                outputstream.close();
     }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 7123356
You don't check how many bytes are actually read, you write line should be:

outputstream.write(buffer, 0, data);
0
 
LVL 1

Author Comment

by:subbaiah
ID: 7124486
i changed write() method. but still its not working ...


am i the first one doing this (reading gzip file)???
0
 
LVL 92

Expert Comment

by:objects
ID: 7125530
Works ok here.
Exactly what is your problem?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8893392
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to objects

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question