Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-21-2008, 04:02 PM
Member
 
Join Date: Jul 2008
Posts: 3
nitinmukesh is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-21-2008, 04:04 PM
Member
 
Join Date: Jul 2008
Posts: 3
nitinmukesh is on a distinguished road
Since I can't post URL's attached is the message
Attached Images
File Type: jpg 1.JPG (80.4 KB, 7 views)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-21-2008, 05:59 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-21-2008, 07:29 PM
Member
 
Join Date: Jul 2008
Posts: 3
nitinmukesh is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-22-2008, 12:25 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Here's some code from one of my applets that sends a request to a server for a file and reads the response:

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-22-2008 at 12:27 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use FileDialog to prompt for a file name (to save) Java Tip SWT 0 07-02-2008 09:59 PM
How to Save/Load Vector to/from file Java Tip java.lang 0 04-14-2008 10:37 PM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 11:13 AM
save file to database katerinaaa New To Java 0 08-14-2007 02:15 PM
How can I make the file ALWAYS save as .txt by default paul AWT / Swing 1 08-07-2007 07:09 AM


All times are GMT +3. The time now is 01:37 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org