Solved

Compressed file download using servlets

Posted on 2002-06-26
16
503 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
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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
for loop with Set 4 45
countX 22 69
noX challenge 17 76
solarwind tftp server 2 30
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now