Total members 11895 |It is currently Sun Dec 22, 2024 9:36 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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

   
   
%>
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

For this message the author DrRakha has received thanks - 2: jestinmj, usermani

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.

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Thanks


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

thanks


Author:
Newbie
User avatar Posts: 1
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


Author:
Newbie
User avatar Posts: 3
Have thanks: 0 time

you have done good job in your site

http://www.websitedesignerschennai.com

_________________
http://www.websitedesignerschennai.com


Author:
Newbie
User avatar Posts: 3
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!!!!!


Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

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


Author:

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> 



Author:
Post new topic Reply to topic  [ 10 posts ] 

  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.     -  
 Java download error     -  
 download ftp client softwares     -  
 create download link     -  
 upload and download images in my jsp page.     -  
 How to download and save .mov files from this website ?     -  
 I download the phone book coding from your site     -  



Topic Tags

Servlets/JSP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com