Solved

Can I download .txt, .html, jpg, gif

Posted on 2000-05-10
13
230 Views
Last Modified: 2010-04-01
I have made a servlet which can downloads files from server, but when I tried to download .txt, .html, .jpg, or .gif files, browser open that files not download (even I set the Content Type("application/octet-stream")). I want to save these files on the disk not open it in the browser. Can anyone help me in this regards, I will be very thankful......
0
Comment
Question by:siayubi
  • 6
  • 6
13 Comments
 
LVL 4

Accepted Solution

by:
jerch earned 100 total points
ID: 2799269
Hi siayubi...
I'm not sure if this is what you need. Check it out.  This utility triggers a download dialog box in the browser.  

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

public class ServletUtils {
    public static void returnFileForDownload(HttpServletResponse res,
            String fileName) throws IOException {
         
        if ((fileName == null) || (res == null)) {
            throw new IllegalArgumentException();
        }
             
        byte[] buf = new byte[4 * 1024];
        File file = new File(fileName);
        res.setHeader("Content-Disposition", "attachment; filename=" + file.getName() + ";");
        res.setContentType("application/octet-stream");
        res.setHeader("Cache-Control", "no-cache");
     
        FileInputStream inStream = new FileInputStream(file);
        OutputStream outStream = res.getOutputStream();

        int sizeRead = 0;
        while ((sizeRead = inStream.read(buf, 0, buf.length)) != -1) {
            outStream.write(buf, 0, sizeRead);
        }
         
        inStream.close();
        outStream.close();
    }
}

You can use this in your servlet like this.

ServletUtils.returnFileForDownload(res, "c:\\yourfile.jpg");

best regards...
Jerson
0
 

Author Comment

by:siayubi
ID: 2799862
Dear Jerson

Even your code opens the files (of extention .txt, .jpg, .gif and .html) in the browser. It cannot download that files. My problem is to download these files on the disk .........
0
 
LVL 4

Expert Comment

by:jerch
ID: 2799872
I've tested the code. Isn't a download dialog box shown?

cheers...
Jerson
0
 

Author Comment

by:siayubi
ID: 2800001
I am working in IE4 browser it show the files of having above extention. and in NS 4.72 it will download a garbeg but in IE5 it work fine.

What's your point of view.

 
0
 
LVL 4

Expert Comment

by:jerch
ID: 2800145
Hi siayubi...
I think the reason is that because Content-Disposition header is not a standard HTTP header so it's not supported by the browsers you mentioned.  It's actually a de facto standard, which means it's widely used but not defined in HTTP spec.  But as far as my concern, the content-diposition is working with IE 4 fine.  Try to remove res.setHeader("Cache-Control", "no-cache"); since it's a http 1.1 specific header maybe it will help. You can use pragma instead.

cheers...
Jerson
     
       
0
 

Expert Comment

by:yoavdo
ID: 2809377
another option is to remove the key of txt etc. from your registry :

  [Version]
     signature="$CHICAGO$"
 
      [DefaultInstall]
      DelReg = RegChange
 
      [RunIt]
     DelReg= RegChange
 
      [RegChange]
     HKCR,MIME\Database\Content Type\text/plain,Extension
     
u can copy this into a .inf file, then install it, (right click). this small file remove the keys of the txt extention. after u insatll such a file, your browser (btw, u should change it to ie5) will not open any more txt files!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:jerch
ID: 2809701
Hi siayubi...
Is it working?

Jerson
0
 

Author Comment

by:siayubi
ID: 2810316
Hi Dear Jerson

It is not working with IE4 or NS4.72, by my experience "content-diposition" is not working with IE4. I have tried to do this with the removal of res.setHeader("Cache-Control", "no-cache") and also with res.setHeader("pragma", "no-cache") but all in vain. Can you help me  anywhere else.....
0
 
LVL 4

Expert Comment

by:jerch
ID: 2812802
Hi siayubi...
Check this site http://www.javaworld.com/javaworld/javatips/jw-javatip94.html.  I just found this site last week.  It says that there's a bug in IE 4 which doesn't recognize the content-disposition header.  Actually, it's working with my IE4.

cheers...
Jerson
0
 

Author Comment

by:siayubi
ID: 2828330
Dear yoavdo!

It is not my requirment that user(client) should do some thing for download. Mr. Jerson solution is right.

I am very thankful to you.
0
 

Author Comment

by:siayubi
ID: 2828333
can you guys give me your emails
my email is siayubi@hotmail.com
0
 

Author Comment

by:siayubi
ID: 2828336
Thanks Jerson!
Can you provide me your email.
my is siayubi@hotmail.com
0
 
LVL 4

Expert Comment

by:jerch
ID: 2828550
jerson@rocketmail.com
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 53
maven project error 5 48
GUI builder for Eclipse? 8 27
Error trying to install RTMT Win7 5 41
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

920 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

12 Experts available now in Live!

Get 1:1 Help Now