Problem in downloading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vikasini
    New Member
    • Sep 2007
    • 6

    Problem in downloading

    Hi friends,

    I have a servlet coding where i can download a folder from FTP.
    The folder is zipped and stored in the FTP server.
    While the client downloads the zip file the it should be unzipped and stored in the client machine.
    Here is the code..


    Code:
    import javax.servlet.http.*;
    import javax.servlet.*;
    import javax.servlet.ServletOutputStream;
    import java.io.*;
    import java.awt.*;
    import java.util.*;
    import java.util.zip.*;
    import java.net.*;
    
    public class TempDownload1 extends HttpServlet{
    
    public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException
    {}
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
     throws ServletException, IOException
     {
        byte[] buffer = new byte[16384];
        //source
        ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");
        Component parentComponent=null;
        //destination
        File outputDir = new File("C:/");
        Enumeration entries = zip.entries();
        for ( int i = 0; entries.hasMoreElements(); )
                {
    	ZipEntry entry = (ZipEntry) entries.nextElement();
    	File File = new File(outputDir, entry.getName());
    	File.getParentFile().mkdirs();
    	if (!entry.isDirectory())
    	  {
    	    InputStream in = null;
    	    OutputStream out = null;
    	try {
    	     in = zip.getInputStream(entry);
    	     out = new FileOutputStream(File);
    	     for (int n = 0; (n = in.read(buffer)) > -1; )
    	     out.write(buffer, 0, n);
    	     }
                     finally {
    		try {
    		       out.close();
    	                     }
    		catch (IOException e) {}
    	           }
    	    }
    	 }
    unzip(zip, parentComponent ,outputDir);
    
        }
    }
    While trying to compile the servlet code it's fine but when the servlet is executed the following exception occurs

    Exception : java.util.zip.Z ipException: The filename, directory name, or volume label syntax is incorrect.

    Can anyone help me out..
    Thanks in advance
    Last edited by vikasini; Sep 7 '07, 10:02 AM. Reason: to use code tag
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by vikasini
    Hi friends,

    I have a servlet coding where i can download a folder from FTP.
    The folder is zipped and stored in the FTP server.
    While the client downloads the zip file the it should be unzipped and stored in the client machine.
    Here is the code..


    Code:
    import javax.servlet.http.*;
    import javax.servlet.*;
    import javax.servlet.ServletOutputStream;
    import java.io.*;
    import java.awt.*;
    import java.util.*;
    import java.util.zip.*;
    import java.net.*;
    
    public class TempDownload1 extends HttpServlet{
    
    public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException
    {}
    
    public void doGet(HttpServletRequest req, HttpServletResponse res)
     throws ServletException, IOException
     {
        byte[] buffer = new byte[16384];
        //source
        ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");
        Component parentComponent=null;
        //destination
        File outputDir = new File("C:/");
        Enumeration entries = zip.entries();
        for ( int i = 0; entries.hasMoreElements(); )
                {
    	ZipEntry entry = (ZipEntry) entries.nextElement();
    	File File = new File(outputDir, entry.getName());
    	File.getParentFile().mkdirs();
    	if (!entry.isDirectory())
    	  {
    	    InputStream in = null;
    	    OutputStream out = null;
    	try {
    	     in = zip.getInputStream(entry);
    	     out = new FileOutputStream(File);
    	     for (int n = 0; (n = in.read(buffer)) > -1; )
    	     out.write(buffer, 0, n);
    	     }
                     finally {
    		try {
    		       out.close();
    	                     }
    		catch (IOException e) {}
    	           }
    	    }
    	 }
    unzip(zip, parentComponent ,outputDir);
    
        }
    }

    While trying to compile the servlet code it's fine but when the servlet is executed the following exception occurs

    Exception : java.util.zip.Z ipException: The filename, directory name, or volume label syntax is incorrect.

    Can anyone help me out..
    Thanks in advance
    At which line?

    Comment

    • vikasini
      New Member
      • Sep 2007
      • 6

      #3
      Originally posted by r035198x
      At which line?

      At line num 20
      i.e., ZipFile zip=new ZipFile("ftp://username:pswd@x xx.xx.xxx.xx/location");

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by vikasini
        Hi friends,

        I have a servlet coding where i can download a folder from FTP.
        The folder is zipped and stored in the FTP server.
        While the client downloads the zip file the it should be unzipped and stored in the client machine.
        Here is the code..


        Code:
        import javax.servlet.http.*;
        import javax.servlet.*;
        import javax.servlet.ServletOutputStream;
        import java.io.*;
        import java.awt.*;
        import java.util.*;
        import java.util.zip.*;
        import java.net.*;
        
        public class TempDownload1 extends HttpServlet{
        
        public static void unzip(ZipFile zip, Component parentComponent, File outputDir) throws IOException
        {}
        
        public void doGet(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException
         {
            byte[] buffer = new byte[16384];
            //source
            ZipFile zip=new ZipFile("ftp://username:pswd@xxx.xx.xxx.xx/location/filename.zip");
            Component parentComponent=null;
            //destination
            File outputDir = new File("C:/");
            Enumeration entries = zip.entries();
            for ( int i = 0; entries.hasMoreElements(); )
                    {
        	ZipEntry entry = (ZipEntry) entries.nextElement();
        	File File = new File(outputDir, entry.getName());
        	File.getParentFile().mkdirs();
        	if (!entry.isDirectory())
        	  {
        	    InputStream in = null;
        	    OutputStream out = null;
        	try {
        	     in = zip.getInputStream(entry);
        	     out = new FileOutputStream(File);
        	     for (int n = 0; (n = in.read(buffer)) > -1; )
        	     out.write(buffer, 0, n);
        	     }
                         finally {
        		try {
        		       out.close();
        	                     }
        		catch (IOException e) {}
        	           }
        	    }
        	 }
        unzip(zip, parentComponent ,outputDir);
        
            }
        }
        While trying to compile the servlet code it's fine but when the servlet is executed the following exception occurs

        Exception : java.util.zip.Z ipException: The filename, directory name, or volume label syntax is incorrect.

        Can anyone help me out..
        Thanks in advance
        IDoes ZipFile really support ftp adresses? I doubt it. "File" doesn't seem to support them.

        Can't you give a relative adress? If it's the same filesystem, it won't be so problematic. This way, it tries to connect to
        C:\workspace\ft p:\username:psw d@xxx.xx.xxx.xx \location\filen ame.zip (when "C:\workspa ce" is your working directory), which is of course not what you're looking for.

        Greetings,
        Nepomuk

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by vikasini
          At line num 20
          i.e., ZipFile zip=new ZipFile("ftp://username:pswd@x xx.xx.xxx.xx/location");
          Why don't use a ZipInputStream as explained here.

          Comment

          • vikasini
            New Member
            • Sep 2007
            • 6

            #6
            Originally posted by r035198x
            Why don't use a ZipInputStream as explained here.


            I tried with the ZipInputStream also. Still the same exception occurs

            Comment

            • Nepomuk
              Recognized Expert Specialist
              • Aug 2007
              • 3111

              #7
              Originally posted by vikasini
              I tried with the ZipInputStream also. Still the same exception occurs
              Is it an external file you're accessing? If so, I guess, you'd have to download the class to your server, unzip it there and then send it to the client. It's the same, when you want to open a file from the web on your computer - you have to download it first.
              If the file is local, access it in a local way. That's much easier than all of this ftp stuff.

              Greetings,
              Nepomuk

              Comment

              • vikasini
                New Member
                • Sep 2007
                • 6

                #8
                Originally posted by nepomuk
                Is it an external file you're accessing? If so, I guess, you'd have to download the class to your server, unzip it there and then send it to the client. It's the same, when you want to open a file from the web on your computer - you have to download it first.
                If the file is local, access it in a local way. That's much easier than all of this ftp stuff.

                Greetings,
                Nepomuk



                Yes it's an external file that i am trying to access.
                When i use it for a local file it works perfectly


                Eg: ZipFile zip=new ZipFile("C:/filename.zip");

                But it is not working for the FTP

                So i tried using this way

                URL zip=new URL("ftp://username:pswd@x xx.xx.xxx.xx/location/filename.zip");

                But while trying to use this way it is not supporting the 'Enumeration' and the getInputStream( ) method

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by vikasini
                  Yes it's an external file that i am trying to access.
                  When i use it for a local file it works perfectly

                  Eg: ZipFile zip=new ZipFile("C:/filename.zip");

                  But it is not working for the FTP

                  So i tried using this way

                  URL zip=new URL("ftp://username:pswd@x xx.xx.xxx.xx/location/filename.zip");

                  But while trying to use this way it is not supporting the 'Enumeration' and the getInputStream( ) method
                  OK, seems you'll have to download it first. If you don't have ftp classes in your project already, have a look at the commons-net lib from Apache (or, if you choose to, any other ftp lib). With this, you can download your external file and as soon as it's local, you have the solution!

                  Greetings,
                  Nepomuk

                  Comment

                  • vikasini
                    New Member
                    • Sep 2007
                    • 6

                    #10
                    Originally posted by nepomuk
                    OK, seems you'll have to download it first. If you don't have ftp classes in your project already, have a look at the commons-net lib from Apache (or, if you choose to, any other ftp lib). With this, you can download your external file and as soon as it's local, you have the solution!

                    Greetings,
                    Nepomuk

                    I am not able to download the file even after i tried to use that.
                    The problem is with the URL method..
                    Is there any other way to alter the code to make it perfect

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by vikasini
                      I am not able to download the file even after i tried to use that.
                      The problem is with the URL method..
                      Is there any other way to alter the code to make it perfect
                      Perhaps you should go through a tutorial on how to copy a file over the network

                      Comment

                      Working...