Switch to full style
What's behind JSP & Servlets
Post a reply

JSP to Download File

Fri Apr 06, 2007 9:59 am

This program allow you to download applications from your JSP server host .
The user will not knew the locations of your files :lol: .
Code:
/*
By : Sami & El-dib
*/


   

<%@ page  import="java.io.FileInputStream" %>
<%@ 
page  import="java.io.BufferedInputStream"  %>
<%@ 
page  import="java.io.File"  %>
<%@ 
page import="java.io.IOException" %>


<%

   
// you  can get your base and parent from the database
   
String base="e1";
   
String parent="e2";   
   
String filename=parent+"_codemiles.zip";
// you can  write http://localhost
   
String filepath="http://www.codemiles.com/example/"+base+"/";

BufferedInputStream buf=null;
   
ServletOutputStream myOut=null;

try{

myOut response.getOutputStream( );
     
File myfile = new File(filepath+filename);
     
     
//set response headers
     
response.setContentType("text/plain");
     
     
response.addHeader(
        
"Content-Disposition","attachment; filename="+filename );

     
response.setContentLength( (int) myfile.length( ) );
     
     
FileInputStream input = new FileInputStream(myfile);
     
buf = new BufferedInputStream(input);
     
int readBytes 0;

     
//read from the file; write to the ServletOutputStream
     
while((readBytes buf.read( )) != -1)
       
myOut.write(readBytes);

} catch (
IOException ioe){
     
        throw new 
ServletException(ioe.getMessage( ));
         
     } 
finally {
         
     
//close the input/output streams
         
if (myOut != null)
             
myOut.close( );
          if (
buf != null)
          
buf.close( );
         
     }

   
   
%>
 




Re: JSP to Download File

Tue Apr 17, 2007 12:58 am

You Can use this code to hide the actual place of your files on the server so visitors can't takes links and download from your site with out visiting your web site.

Re: JSP to Download File

Wed Apr 01, 2009 5:44 pm

Thanks

Re: JSP to Download File

Thu May 06, 2010 11:30 am

thnx...but wer to get d code from??

Re: JSP to Download File

Fri May 14, 2010 5:14 am

thanks

Re: JSP to Download File

Mon May 17, 2010 4:24 pm

hi,


I need an example of a code in java that would be as follows:

In my webpage has a link to the download area (directory on my server with some files to users., Restricted area).

Do you have any examples? please ..

thanks

hugs .

fabio alonso

Re: JSP to Download File

Tue Jul 13, 2010 2:25 pm

you have done good job in your site

http://www.websitedesignerschennai.com

Re: JSP to Download File

Thu Jul 15, 2010 7:14 am

I suggested to give us example that we gonna know whats going on on thats code.In my webpage has a link to the download area (directory on my server with some files to users.so I just give you hint just click here under!!!!!

Re: JSP to Download File

Fri Aug 12, 2011 4:39 am

I had use this code and its work to docx and pdf file, but i can't upload the zip file. isn't this code can download zip file as well? plz, if somebody can help me out.tq

Re: JSP to Download File

Tue Aug 23, 2011 6:03 am

Code:
<html>

<
head>
<%@
 page  import="java.io.FileInputStream" %>
<%@
 page  import="java.io.BufferedInputStream"  %>
<%@
 page  import="java.io.File"  %>
<%@
 page import="java.io.IOException" %>
<%@
 page import="java.util.List" %>
<%@
 page import="java.net.URLConnection" %>
<%@
 page import="java.io.BufferedOutputStream" %>
<%@
 page import="java.util.zip.ZipOutputStream" %>
<%@
 page import="java.io.FileOutputStream" %>
<%@
 page import="java.util.zip.ZipEntry" %>

<%@
 page import="java.io.InputStream" %>
<%@
 page import="java.net.URL" %>

<%
    Object filename = request.getAttribute("filename");
    ZipOutputStream zipOut = null;
    BufferedInputStream buf = null;
    ServletOutputStream myOut = null;
    String tempFolder = System.getProperty("user.dir")+ File.separator +"upload"+ File.separator + "temp.zip";

    try {
        File inputFolder = new File(filename.toString());
        File outFolder = new File(tempFolder);
        zipOut = new ZipOutputStream(new BufferedOutputStream(
                new FileOutputStream(outFolder)));
        byte[] data = new byte[1000];
        String files[] = inputFolder.list();
        for (int i = 0; i < files.length; i++) {
            buf = new BufferedInputStream(new FileInputStream(
                    inputFolder.getPath() + "/" + files[i]), 1000);
            zipOut.putNextEntry(new ZipEntry(files[i]));
            int count;
            while ((count = buf.read(data, 0, 1000)) != -1) {
                zipOut.write(data, 0, count);
            }
            zipOut.closeEntry();
        }
        zipOut.flush();
        zipOut.close();
        myOut = response.getOutputStream();
        response.setContentType("binary/data");
        response.addHeader("Content-Disposition",
                "attachment; filename=XMLs.zip");
        try {
            URL url = new URL("file:///" + outFolder);
            URLConnection con = url.openConnection();
            buf = new BufferedInputStream(con.getInputStream());
            int readByte = 0;
            while ((readByte = buf.read()) != -1) {
                myOut.write(readByte);
            }
            buf.close();
        } catch (Exception e) {
        }
    } catch (Exception ioe) {
        throw new ServletException(ioe.getMessage());

    } finally {
        if (myOut != null)
            myOut.close();
        if (buf != null)
            buf.close();
        
    
}
%>
</
head>
</
html> 


Post a reply
  Related Posts  to : JSP to Download File
 Link to download in my website..jsp or jsf to download file     -  
 How to download any file in jsp.     -  
 Download a PDF file using JSP or servlet     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 create download link     -  
 Java download error     -  
 download ftp client softwares     -  
 How to download and save .mov files from this website ?     -  
 upload and download images in my jsp page.     -  
 passguide vcp-410 questions dumps download links     -  

Topic Tags

Servlets/JSP