?
Solved

Compressed file download using servlets

Posted on 2002-06-26
16
Medium Priority
?
514 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 200 total points
ID: 7112461
Try getting rid of this line:

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

0
DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

 
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

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

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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