Results 1 to 7 of 7
- 07-21-2008, 02:02 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
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.
- 07-21-2008, 02:04 PM #2
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Since I can't post URL's attached is the message
- 07-21-2008, 03:59 PM #3
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.
- 07-21-2008, 05:29 PM #4
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Ok now I have started coding but facing a problem.
When I run this code locally it works fine but when run in browser it shows security exceptionPHP 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; }
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.
- 07-21-2008, 10:25 PM #5
Here's some code from one of my applets that sends a request to a server for a file and reads the response:
The applet can only connect to the server it came from. In the above that address is given by getCodeBase().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()));
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 10:27 PM.
- 06-03-2012, 08:38 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,606
- Rep Power
- 5
- 06-03-2012, 08:45 PM #7
Similar Threads
-
How to use FileDialog to prompt for a file name (to save)
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 07:59 PM -
How to Save/Load Vector to/from file
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:37 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM -
save file to database
By katerinaaa in forum New To JavaReplies: 0Last Post: 08-14-2007, 12:15 PM -
How can I make the file ALWAYS save as .txt by default
By paul in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 05:09 AM


LinkBack URL
About LinkBacks


Bookmarks