Results 1 to 6 of 6
  1. #1
    ShiZhi is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Saving data to a file on my website

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Save {
    	public static void main(String[] args){
    		saveToServer("ContentShouldSayThis");
    	}
        public static void saveToServer(String data) {
            try {
                if (data.length() < 1) {
                    System.err.println("Usage:  java ReverseTest string_to_reverse");
                    System.exit(1);
                }
                String stringToReverse = URLEncoder.encode(data);
    
                URL url = new URL("http://testwebsite.webatu.com/publicsave/test");
                URLConnection connection = url.openConnection();
                connection.setDoOutput(true);
                PrintStream outStream = new PrintStream(connection.getOutputStream());
                outStream.println("string=" + stringToReverse);
                outStream.close();
    
                DataInputStream inStream = new DataInputStream(connection.getInputStream());
                String inputLine;
    
                while ((inputLine = inStream.readLine()) != null) {
                    System.out.println(inputLine);
                }
                inStream.close();
            } catch (MalformedURLException me) {
                System.err.println("MalformedURLException: " + me);
            } catch (IOException ioe) {
                System.err.println("IOException: " + ioe);
            }
        }
    }
    I have made a website a freewebhost and I have set the permissions of the file "test" in the directory "publicsave" on mywebsite to chmod = 777, which I think means that anyone should be able to read from or write to the file, though I'm not positive. I've never tried saving data to a website through java before so forgive my ignorance. I'm not sure why this isn't working; I'm not getting any errors, and the program is reading the data from the file on the website, but is failing to save data to that file, so that it always returns "[write code here]" (which is by default the content of that file.) Can someone please help me out? I really appreciate it.

    Again, to be clear, the problem is that I am unable to successfully save the string "ContentShouldSayThis" to the file on the website.

    Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Saving data to a file on my website

    You can't just write to a web file like that. You'd have to run code on the server to create the file for you.

    Google found me this discussion: Help writing to a text file on a website!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ShiZhi is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Saving data to a file on my website

    Thanks, I have looked at the site you linked me to. In the end this is what I came up with:

    Java Code:
    import java.io.*;
    import java.net.*;
    
    import org.apache.commons.net.ftp.FTPClient;
    
    public class Save {
    	public static void main(String[] args){
    		saveToServer();
    	}
        public static void saveToServer() {
        	FTPClient client = new FTPClient();
        	FileInputStream fis = null;
    
        	try {
        	    client.connect("thesite.webatu.com");
        	    client.login("myusername", "mypassword");  //I have confirmed that the username and password are correct
        	    String filename = "C:/Users/User/Documents/textfile.txt";
        	    fis = new FileInputStream(filename);
        	    client.storeFile(filename, fis);
        	    System.out.println("File should have been uploaded by now.");
        	    client.logout();
        	} catch (IOException e) {
        	    e.printStackTrace();
        	} finally {
        	    try {
        	        if (fis != null) {
        	            fis.close();
        	        }
        	        client.disconnect();
        	    } catch (IOException e) {
        	        e.printStackTrace();
        	    }
        	}
        }
    }
    Unforunately, however, the file still isn't appearing in the file manager of my site. The free site I have set up has access to free FTP hosting, and the hostname, username, and password were copied directly from the FTP details listed in the control panel of my site (made with 000webhost.com, if this detail helps). I'm afraid I'm not sure what I'm doing wrong here. I don't receive any errors, but in the end the file just isn't on the site. Am I missing something? Thanks for the help I've received so far, more help would be much appreciated!

  4. #4
    ShiZhi is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Saving data to a file on my website

    Just a sidenote, to show how little I know about this,
    Do I need to be running a client such as filezilla for this to work? If so, is there a way to save/upload to a website without using java without running a third-party program?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Saving data to a file on my website

    I'm unfamiliar with the FTPClient class, but if I was you, I would take a look at its API documentation: FTPClient (Commons Net 3.3 API)

    The documentation contains example code with more functionality than you show, such as checking the connection. That's where I'd start if I were you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Saving data to a file on my website

    apache commons libraries tend to have a source download with plenty of example programs in there too.

Similar Threads

  1. Encrypting data and saving to a file?
    By OllyHal in forum New To Java
    Replies: 4
    Last Post: 04-26-2012, 12:42 PM
  2. how to extract specific data from image file and saving in to database
    By Balasubrahmanyam in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-27-2011, 02:19 PM
  3. Saving data from a socket to a file in real time
    By colossusdub in forum New To Java
    Replies: 3
    Last Post: 03-03-2010, 07:32 PM
  4. Saving data...?
    By easyRyder in forum New To Java
    Replies: 8
    Last Post: 07-15-2008, 03:14 AM
  5. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM

Posting Permissions

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