Page 1 of 2 12 LastLast
Results 1 to 20 of 24
Like Tree1Likes

Thread: Uploading a file to webspace through a java applet

  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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);
            }
            
        }
    }
    Thanks, I hope you understand what i am trying to explain :)
    Last edited by Teedo; 08-07-2013 at 03:04 PM.

  2. #2
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default 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

  4. #4
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default 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

  6. #6
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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 **

  8. #8
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Uploading a file to webspace through a java applet

    Yes it exists, i'm testing it all through netbeans now so my computer is the server/client.

    Uploading a file to webspace through a java applet-accountsexist.jpg

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default 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

  10. #10
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Uploading a file to webspace through a java applet

    Quote Originally Posted by Teedo View Post
    ...but I want it to create the file on the server/webspace and not locally on the clients computer.
    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.
    Teedo likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #12
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Uploading a file to webspace through a java applet

    Quote Originally Posted by Tolls View Post
    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.
    Thank you very much, that's what I was after :)

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default 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

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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 **

  15. #15
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default 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

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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 **

  17. #17
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default 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 :)

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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 **

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,367
    Rep Power
    6

    Default 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

  20. #20
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Uploading a file to webspace through a java applet

    Okay thanks, I shall research and try that this weekend. I really have no idea about how I would go about doing that, but after a quick search it sounds like I need to learn about apache or something similar....Would I be on the right track?

Page 1 of 2 12 LastLast

Similar Threads

  1. Uploading files to IIS from Applet
    By dominicthoppil in forum Java Applets
    Replies: 0
    Last Post: 05-30-2013, 03:42 PM
  2. uploading applet with multiple classes help
    By alacn in forum New To Java
    Replies: 3
    Last Post: 08-02-2010, 07:37 AM
  3. Uploading in an ftp applet help
    By d4dirty in forum Java Applets
    Replies: 0
    Last Post: 01-07-2010, 10:24 AM
  4. Java Applet + File Uploading
    By Moncleared in forum New To Java
    Replies: 4
    Last Post: 02-02-2009, 07:33 PM
  5. uploading sound file in java
    By po0oker in forum Advanced Java
    Replies: 8
    Last Post: 11-04-2007, 12:00 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
  •