Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Compressed file download using servlets

Posted on 2002-06-26
16
Medium Priority
?
516 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
  • 5
  • 5
  • 3
  • +2
15 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 Month14 days, 21 hours left to enroll

577 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