• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

Compressed file download using servlets

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
subbaiah
Asked:
subbaiah
  • 5
  • 5
  • 3
  • +2
1 Solution
 
pothuganti_srinivasCommented:
files with gz extension are not plain text files. To download them set the content type to "application/gzip".

res.setContentType("application/gzip");

0
 
subbaiahAuthor Commented:
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
 
objectsCommented:
Try getting rid of this line:

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

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
rjackmanCommented:
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
 
rjackmanCommented:
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
 
subbaiahAuthor Commented:
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
 
objectsCommented:
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
 
rjackmanCommented:
try with this 1
response.setContentType("application/save");
response.addHeader("Content-Disposition:attachment", "filename=abc.xxx");
0
 
subbaiahAuthor Commented:
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
 
objectsCommented:
Post your current code and i'll try it here.
0
 
subbaiahAuthor Commented:
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
 
objectsCommented:
You don't check how many bytes are actually read, you write line should be:

outputstream.write(buffer, 0, data);
0
 
subbaiahAuthor Commented:
i changed write() method. but still its not working ...


am i the first one doing this (reading gzip file)???
0
 
objectsCommented:
Works ok here.
Exactly what is your problem?
0
 
girionisCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 5
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now