Results 1 to 7 of 7
  1. #1
    nitinmukesh is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default Read file from URL and save to FTP

    Hi,

    I have worked in java few years back, now I am out of touch. My friend asked me to create an applet program for him which he can use to download a file from remote location to his ftp server.

    For e.g.
    File


    Saved to


    I was searching for the classes which I can use to achieve this functionality.

    To save to FTP

    {code}
    URL url = new URL("");
    URLConnection urlconnection = url.openConnection();
    long l = urlconnection.getContentLength();
    OutputStream outputstream = null;
    outputstream = urlconnection.getOutputStream();
    {code}

    Using the above code I can get the outputstream to which I can write.

    I am now stuck here, have some doubts.

    What all classes should I use to read a file like


    I know I can use URL to open connection to URLS and then streams to read the url.
    I am interested in knowing the class which I should use to read any type of file. File can be zip/avi/rar or anything else.
    What all method of the class should I use to read the file which will support all type of files.

    Which classes should I use to write the files. The files are going to be huge in terms of size.

    I have searched everywhere but every place different classes are used.

    Any help is appreciated.

  2. #2
    nitinmukesh is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Since I can't post URL's attached is the message
    Attached Thumbnails Attached Thumbnails Read file from URL and save to FTP-1.jpg  

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You could use InputStream and OutputStream to read and write bytes.
    Write a small test program that reads and writes a file (copies) to verify you are using the classes/methods correctly before using them in you program. You can use a URL to reference the files by using the file:// protocol.

  4. #4
    nitinmukesh is offline Member
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Default

    Ok now I have started coding but facing a problem.


    PHP Code:
    try {
    
    	URL url = new URL("url_to_file/test.txt");
    	URLConnection urlconnection = url.openConnection();
    	long l = urlconnection.getContentLength();
    	fTextArea.append("Content Length = " + l);
    	BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    	String line;
    	while ((line = in.readLine()) != null)
    	{
    		fTextArea.append("\n"+line);
    	}
    	in.close();
    
    } catch (Exception e) {
       fTextArea.append(e);
       return;
    }
    When I run this code locally it works fine but when run in browser it shows security exception

    java.security.AccessControlException: access denied (java.net.SocketPermission site:80 connect,resolve)

    What should I do to resolve this error. Sorry for being such a noob but do not have much info about this.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Here's some code from one of my applets that sends a request to a server for a file and reads the response:

    Java Code:
      
            URL u = null;
            BufferedReader in;
     .....
                    // We're in an applet - go to server for the file
    //                u = new URL(getCodeBase() + CGI_DIR + "/GetFile.pl?" + fn);
                    u = new URL(getCodeBase() + fn + (addSfx ==  null ? "" : addSfx));   // do HTTP GET directly
                    URLConnection uc = u.openConnection();
                    in = new BufferedReader(new InputStreamReader(
                                    uc.getInputStream()));
    The applet can only connect to the server it came from. In the above that address is given by getCodeBase().

    It is possible to give an applet permission to connect via the policytool program. It creates an entry in the .java.policy file giving permissions for connect and resolve.
    Also it might be possible by signing the jar file the applet is in.
    Last edited by Norm; 07-21-2008 at 11:27 PM.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Read file from URL and save to FTP

    Quote Originally Posted by bigjavageek View Post
    For saving to FTP, this can easily be done using Zehon FTP. Look at this sample, ...
    Please don't resurrect four year old posts to place links to commercial sites. Link removed and thread closed.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Read file from URL and save to FTP

    Quote Originally Posted by doWhile View Post
    Please don't resurrect four year old posts to place links to commercial sites. Link removed and thread closed.
    The same spammer spammed another Java forum at around the same time, so I've applied a suitable remedy.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 07-02-2008, 08:59 PM
  2. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:37 PM
  3. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  4. save file to database
    By katerinaaa in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 01:15 PM
  5. Replies: 1
    Last Post: 08-07-2007, 06:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •