Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 3:11 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 10 posts ] 
Author Question
 Question subject: JSP to Download File
PostPosted: Fri Apr 06, 2007 9:59 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time

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( );
         
     }

   
   
%>
 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )



For this message the author msi_333 has received thanks - 2: jestinmj, usermani
TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Tue Apr 17, 2007 12:58 am 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time
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.

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Wed Apr 01, 2009 5:44 pm 
Offline
Newbie
User avatar

Joined: Wed Apr 01, 2009 5:40 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
Thanks


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Thu May 06, 2010 11:30 am 
Offline
Newbie
User avatar

Joined: Thu May 06, 2010 11:12 am
Posts: 1
Has thanked: 1 time
Have thanks: 0 time
thnx...but wer to get d code from??


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Fri May 14, 2010 5:14 am 
Offline
Newbie
User avatar

Joined: Fri May 14, 2010 5:12 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
thanks


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Mon May 17, 2010 4:24 pm 
Offline
Newbie
User avatar

Joined: Mon May 17, 2010 4:21 pm
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
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


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Tue Jul 13, 2010 2:25 pm 
Offline
Newbie
User avatar

Joined: Fri Jun 25, 2010 7:14 am
Posts: 3
Has thanked: 0 time
Have thanks: 0 time
you have done good job in your site

http://www.websitedesignerschennai.com

_________________
http://www.websitedesignerschennai.com


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: Thu Jul 15, 2010 7:14 am 
Offline
Newbie
User avatar

Joined: Thu Jul 15, 2010 7:05 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time
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!!!!!


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: 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


TOP
  
Reply with quote  
 Question subject: Re: JSP to Download File
PostPosted: 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> 


TOP
  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 10 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 How to download and save .mov files from this website ?  General Discussion  Sandy1984  7
 How to make PHP form data saved into txt file  PHP  Radu  6
 Call of duty 4 Mobile Game Free Download  General Discussion  padang  1
 I download the phone book coding from your site  PHP  Anonymous  0
 file exists in upper level folder link  HTML examples  msi_333  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team