prain
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
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
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
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");
File f = new File("//machine_name/share
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
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
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/MYD IR/data/te stfile.dat ");
BufferedWriter afile = new BufferedWriter(new FileWriter(f));
afile.write("Hello There");
afile.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
Will investigate more.
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/MYD
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
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/MYD IR/data/te stfile.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
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
ASKER
java.io.FileNotFoundExcept ion: \\MachineName\MYDIR\data\t estfile.da t (Logon failure: unknown user name or bad password)
at java.io.FileOutputStream.o pen(Native Method)
at java.io.FileOutputStream.< init>(File OutputStre am.java:17 9)
at java.io.FileOutputStream.< init>(File OutputStre am.java:13 1)
at java.io.FileWriter.<init>( FileWriter .java:73)
at DownloadFile.main(Download File.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?.
at java.io.FileOutputStream.o
at java.io.FileOutputStream.<
at java.io.FileOutputStream.<
at java.io.FileWriter.<init>(
at DownloadFile.main(Download
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
CEHJ, Thanks. I got it working.
No problem
:-)