Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

Writng a text file from the PC to a Unix/Solaris Server

Hello,
The PC and the Unix Server are in the same network.
In the server I have a directory /MyData/Test  
created.
All I am trying to do is to write a file of test data into the /MyData/Test directory.
I am using the URL class. But seems does nto work. Here is the code

import java.net.*;
import java.io.*;

public class DownloadFile
{public static void main(String [] args)
 {
  String theURL = "http://999.999.999.99/MYDIR/data/test.dat";

  String aTest = "Testing Some Data";
  byte byteBuffer[] = aTest.getBytes();
        

  try{//establish a connection
      URL sourceURL = new URL(theURL);
      URLConnection connection = sourceURL.openConnection();
      connection.connect();
      //if the files does not exist, the following will throw an exception
      OutputStream out = connection.getOutputStream();
      //read it
      out.write(byteBuffer);
     }
    catch(Exception ex){       System.out.println("Couldn't open the specified files");
                    System.out.println("Bye!");
                    System.exit(1);
       }
 
 }
}

Any Ideas?. I am not writing the actual IP address and showing a fake one 999.999.999.99

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can only do that with the File class. Use a path to a shared file on the network
Try

File f = new File("//machine_name/share_name/file_name");
Is an http server like apache running on the solaris box, is that directory you are trying to write to accessible via the http server, and is the user account with which you are trying to write the file as allowed to write files to that directory?
what you are doing would require:
1. a web container running on the server
2. that it was setup to handle uploads

Instead use Runtime.exec() to execute scp to copy the file across
Avatar of prain

ASKER

I tried CEHJ's method. Throws me an exception. Seems cannot write.
But this location in the Server is "Open". That's for sure. And we are inside a firewall and when I ping from PC, I can see this server and pings well.

This is nothing to do with web development. It is just that we are tring to save some important data of ours into the more secured server than keeping them in a PC.

import java.net.*;
import java.io.*;

public class DownloadFile
{
 public static void main(String [] args)
 {
  try{
        File f = new File("//999.999.999.99/MYDIR/data/testfile.dat");
        BufferedWriter afile =  new BufferedWriter(new FileWriter(f));
           
        afile.write("Hello There");
        afile.close();
      }
  catch (IOException ioe)
            {
              ioe.printStackTrace();
            }
 }
}

Will investigate more.


> public class DownloadFile

your code appears to actually be trying to *upload* file
which your standard web server is probably not going to support
>>File f = new File("//999.999.999.99/MYDIR/data/testfile.dat");

You would be better with the machine name above. If you can't browse it from your machine you won't be able to write to it
Avatar of prain

ASKER

java.io.FileNotFoundException: \\MachineName\MYDIR\data\testfile.dat (Logon failure: unknown user name or bad password)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    at java.io.FileWriter.<init>(FileWriter.java:73)
    at DownloadFile.main(DownloadFile.java:39)

So obviously this simple program running on the pc is trying to login first to the Unix based machine. May be logical.

Is there a java IO class that I can login to a system first and then read/write data?.
Are you running a Windows client?
easier to just use Runtime.exec() to run the appropriate command as I suggested earlier.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

CEHJ, That's exact what I found out just now. Will update. Thanks
> CEHJ, That's exact what I found out just now. Will update. Thanks

you'll still find runtime.exec() works better, Java is very bad at copying files.

But if you want to do it then heres the code you'll need:

http://www.objects.com.au/java/qa/1579575043.html
Avatar of prain

ASKER

CEHJ, Thanks. I got it working.
No problem
:-)