Total members 11864 |It is currently Thu Dec 09, 2021 5:53 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This Code allow you the following :
    1.Upload a file to your server.
    2. Compress it as .zip


To run :

make your form of upload in html
you must have JSP container like (Tomcat ,JBoss,...etc) to run .JSP files

remember that you have to determine your location to upload.


Code:
<%@ page import="java.io.*" %>
<%@ page import="org.apache.tools.zip.ZipEntry" %>
<%@ page import="org.apache.tools.zip.ZipOutputStream" %>
<%
/* By Sami and El-dib */

  String filename = "";
    ServletInputStream in = request.getInputStream();
    byte[] line = new byte[128*2];
    long file_size = 0;
    int i = in.readLine(line, 0, 128*2);
    int boundaryLength = i - 2;
    String boundary = new String(line, 0, boundaryLength); //-2
     
    while (i != -1) {
    String newLine = new String(line, 0, i);
    if (newLine.startsWith("Content-Disposition: form-data; name=\""))
    {
    String s = new String(line, 0, i-2);
    int pos = s.indexOf("filename=\"");
    if (pos != -1) {
    String filepath = s.substring(pos+10, s.length()-1);
    // Windows browsers include the full path on the client
    // But Linux/Unix and Mac browsers only send the filename
    // test if this is from a Windows browser
    pos = filepath.lastIndexOf("\\");
    if (pos != -1)
    filename = filepath.substring(pos + 1);
    else
    filename = filepath;
    }
    //this is the file content
    i = in.readLine(line, 0, 128*2);
    i = in.readLine(line, 0, 128*2);
    // blank line
    i = in.readLine(line, 0, 128*2);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    newLine = new String(line, 0, i);

    while (i != -1 && !newLine.startsWith(boundary)) {
    // the problem is the last line of the file content
    // contains the new line character.
    // So, we need to check if the current line is
    // the last line.
    buffer.write(line, 0, i);
    i = in.readLine(line, 0, 128*2);
    newLine = new String(line, 0, i);
    }
    // save the uploaded file 
     
    byte[] bytes = buffer.toByteArray();
         
    // Create a buffer for reading the files
     String[] filenames = new String[]{filename};
   
    // Create a buffer for reading the files
     
     
// Compress the files
    // Add ZIP entry to output stream.

        try
        {
           
            File myfile = new File("/example/1/");
            myfile.mkdir();
            // Transfer bytes from the file to the ZIP file
          //  ZipOutputStream outz = new ZipOutputStream(new FileOutputStream("home/content/M/o/h/Mohamed2007/html/1/1_subarabia.zip"));
             ZipOutputStream outz = new ZipOutputStream(new FileOutputStream("/example/1/"));
           outz.putNextEntry(new ZipEntry(filename));
            outz.write( bytes,0, bytes.length-2);
             outz.closeEntry();
              outz.close();
        } catch (IOException ex) {
           out.println(ex.toString());
        }
    }
    i = in.readLine(line, 0, 128*2);
    }

out.println("Finish");
%>




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


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : JSP to Upload File
 Upload PDF file in php     -  
 File Upload in JSP     -  
 upload file in ajax     -  
 Upload PHP file code     -  
 Upload File to a new directory     -  
 PHP HTML form with to file upload     -  
 Struts2 upload file and ajax     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Upload Form     -  
 Servlets how to upload Files     -  



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