Results 1 to 20 of 24
- 08-07-2013, 02:07 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Uploading a file to webspace through a java applet
Hello,
I have some webspace that came with my ISP for free, and that I can access using FTP and host a website or files on. I want to make a program that writes a text file to the webspace, and then another program that will be reading the text file and displaying the information to the user. I have spent the past hour trying to google on how to do this, but i did not understand the answers properly. One answer was to run a php server, though i'm not sure how php works and because it is just webspace, i believe all i can do is upload and store files to it? wouldn't someone with access to the server/webspace computer need to run the php server?
Is there another way?
I have tried the following test which prints 'file does not exist' to the screen. I tried getting the documentbase (where the index.html file is)and write a file to it, but it cannot find the file and i think cannot create it.
Java Code:package testing; import java.applet.Applet; import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class Testing extends Applet { String s; public void init() { s = "not updated"; updateFiles(); } public void stop() { } public void paint(Graphics g) { g.drawString(s, 20, 10); } public void updateFiles(){ s = "updated"; File file = new File(this.getDocumentBase()+ "/accounts.txt"); if (!file.exists()){ try { System.out.println("no exist"); s = "file does not exist"; file.createNewFile(); } catch (IOException ex) { Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex); } } try { System.out.println(file); FileWriter write = new FileWriter(file); s = "FILE FOUND"; write.write("it worked..."); write.close(); } catch (IOException ex) { Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex); } } }
Last edited by Teedo; 08-07-2013 at 03:04 PM.
- 08-08-2013, 10:52 AM #2
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: Uploading a file to webspace through a java applet
I have done a little debugging and changed the code to the following.
I printed out the location of where it is looking for accounts.txt and it is correct, though it displays the IOException "java.io.IOException: The filename, directory name, or volume label syntax is incorrect."
Is what I am trying to do, possible? Or would I have to upload a php script in order to be able to have access to write files to the webspace?
Java Code:package testing; import java.applet.Applet; import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class Testing extends Applet { String s; public void init() { s = "not updated"; updateFiles(); } public void stop() { } public void paint(Graphics g) { g.drawString(s, 20, 10); } public void updateFiles(){ s = "updated"; File file = new File(this.getCodeBase() + "accounts.txt"); if (!file.exists()){ try { s = (this.getCodeBase() + "accounts.txt"); file.createNewFile(); } catch (IOException ex) { s = ex.toString(); Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex); } } try { System.out.println(file); FileWriter write = new FileWriter(file); s = "FILE FOUND"; write.write("it worked..."); write.close(); } catch (IOException ex) { Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex); } } }
Last edited by Teedo; 08-08-2013 at 11:12 AM.
- 08-08-2013, 10:58 AM #3
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Uploading a file to webspace through a java applet
Why are you using an ancient AWT applet? Use a Swing JApplet at least.
The error seems to indicate that the filename you are generating is not correct. For example you assume that getDocumentBase() ends with a path separator; it may just not have that. System.out.println() the filename you are generating to see what it is, probably it is not what you expect it to be."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-08-2013, 11:10 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: Uploading a file to webspace through a java applet
I'm not using SWING so i thought i should use Applet, though i will change it to JApplet instead. There is no separator in my 2nd post and I made sure the path was correct, though it still does not work.
- 08-08-2013, 11:13 AM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Uploading a file to webspace through a java applet
Windows does not agree with your assessment. Do all the directories noted in the path actually physically exist?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-08-2013, 11:21 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: Uploading a file to webspace through a java applet
They exist on my webspace where the applet is uploaded to, and I can view the file via the same link that (this.getCodeBase() + "accounts.txt") gives.
Is it not some kind of permission problem where it won't allow me to create a file?
EDIT: I have worked out how to run applets in Netbeans. If I place an 'accounts.txt' file in that directory, then it still thinks it does not exist even though the printed directory is exactly the same.
Java Code:package testing; import java.awt.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JApplet; public class Testing extends JApplet { String s; public void init() { s = "not updated"; updateFiles(); } public void stop() { } public void paint(Graphics g) { g.drawString(s, 20, 10); } public void updateFiles(){ s = "updated"; File file = new File(this.getCodeBase()+ "accounts.txt"); if (!file.exists()){ try { System.out.println("file does not exist, try creating file"); s = (this.getCodeBase() + "accounts.txt"); file.createNewFile(); System.out.println("File created!"); } catch (IOException ex) { //s = ex.toString(); System.out.println(file.getPath()); System.out.println(ex); Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex); } } } }
Java Code:run-applet: file does not exist, try creating file file:C:\Users\Teedo\Documents\NetBeansProjects\Testing\build\classes\accounts.txt java.io.IOException: The filename, directory name, or volume label syntax is incorrect Aug 8, 2013 5:07:23 PM testing.Testing updateFiles SEVERE: null java.io.IOException: The filename, directory name, or volume label syntax is incorrect at java.io.WinNTFileSystem.createFileExclusively(Native Method) at java.io.File.createNewFile(File.java:883) at testing.Testing.updateFiles(Testing.java:34) at testing.Testing.init(Testing.java:17) at sun.applet.AppletPanel.run(AppletPanel.java:425) at java.lang.Thread.run(Thread.java:662) BUILD SUCCESSFUL (total time: 6 seconds)
Last edited by Teedo; 08-08-2013 at 12:12 PM.
- 08-08-2013, 12:20 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Uploading a file to webspace through a java applet
Does this directory structure:
C:\Users\Teedo\Documents\NetBeansProjects\Testing\ build\classes
exist on the machine that you are running the browser?
Not the server where the applet is loaded from...the client machine.
And yes, it could be permissions, except you would (I'm pretty sure) get a security exception rather than an IOException.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-08-2013, 12:35 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
- 08-08-2013, 12:39 PM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Uploading a file to webspace through a java applet
You print out the path and it ends up as this:
file:C:\Users\Teedo\Documents\NetBeansProjects\Tes ting\build\classes\accounts.txt
Notice that extra file: bit at the beginning?"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-08-2013, 01:01 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: Uploading a file to webspace through a java applet
I see....Though i'm unsure on how to fix that. I tested File file = new File("accounts.txt");.....which worked, but I want it to create the file on the server/webspace and not locally on the clients computer.
EDIT: Ah, I see. getCodeBase() adds "file:" to the front, so I guess I need to create a string and remove it, then create a URL from that string to use.Last edited by Teedo; 08-08-2013 at 01:06 PM.
- 08-08-2013, 01:13 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Uploading a file to webspace through a java applet
The only way to do that is to have something on the server that does that.
In the Java world this would be (eg) a servlet that would do the job of creating the file.
The client does not have direct access to the server except via http, and http does not give you the power to create stuff on the server unless there's code there to do it.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-08-2013, 01:21 PM #12
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
- 08-08-2013, 01:52 PM #13
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Uploading a file to webspace through a java applet
Or FTP, as was described in the original post. I would prefer FTP over HTTP myself. I've never used that in combination with an applet though, so I wouldn't know in what ways the sandbox is going to be in the way.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-08-2013, 02:13 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Uploading a file to webspace through a java applet
I'm a bit hesitant since we're talking Applet here, and using FTP opens up the whole security thing.
But, if the "webspace" is just a dumping ground and not actually a web server with something like PHP then it's all a bit moot, and FTP will be the only answer.
... put another way, I should have re-read the original post...:)Please do not ask for code as refusal often offends.
** This space for rent **
- 08-08-2013, 02:17 PM #15
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Uploading a file to webspace through a java applet
Well you make a good point because you'd need to store the credentials somewhere, but HTTP has similar security limitations :) If you create a webservice that needs to be invoked from an applet AND you manage to somehow get it running on the server itself, you're also basically opening it up to the world.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-08-2013, 02:26 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Uploading a file to webspace through a java applet
I was thinking more of the whole applet security and permissions model. I hate that...:)
It's fine talking back to where it came from, but I don't think FTP is covered in that.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-08-2013, 02:29 PM #17
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Re: Uploading a file to webspace through a java applet
My website hosting has the following.....PHP, Microsoft Front Page Server Extensions, Website statistics, PERL
I guess i'll be attempting to learn and use PHP :)
- 08-08-2013, 02:33 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Uploading a file to webspace through a java applet
You could just try the FTP route first and see if you can get that working, since that would not involve learning a new language.
If that suits your situation then you're done...otherwise you could then look into PHP.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-08-2013, 02:35 PM #19
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Uploading a file to webspace through a java applet
@Tolls: I think it restricts the host, not the protocol used. If the host name of the FTP server is the same as where the applet is downloaded from, the sandbox should allow the connection. Maybe you need to sign the applet however.
Anyway, there are existing FTP applets available for download so it must work somehow :)"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-08-2013, 02:42 PM #20
Member
- Join Date
- Apr 2012
- Posts
- 61
- Rep Power
- 0
Similar Threads
-
Uploading files to IIS from Applet
By dominicthoppil in forum Java AppletsReplies: 0Last Post: 05-30-2013, 03:42 PM -
uploading applet with multiple classes help
By alacn in forum New To JavaReplies: 3Last Post: 08-02-2010, 07:37 AM -
Uploading in an ftp applet help
By d4dirty in forum Java AppletsReplies: 0Last Post: 01-07-2010, 10:24 AM -
Java Applet + File Uploading
By Moncleared in forum New To JavaReplies: 4Last Post: 02-02-2009, 07:33 PM -
uploading sound file in java
By po0oker in forum Advanced JavaReplies: 8Last Post: 11-04-2007, 12:00 AM
Bookmarks