Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

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

0
prain
Asked:
prain
  • 7
  • 4
  • 4
  • +1
1 Solution
 
CEHJCommented:
You can only do that with the File class. Use a path to a shared file on the network
0
 
CEHJCommented:
Try

File f = new File("//machine_name/share_name/file_name");
0
 
rindiCommented:
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
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
0
 
prainAuthor Commented:
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.


0
 
objectsCommented:
> public class DownloadFile

your code appears to actually be trying to *upload* file
which your standard web server is probably not going to support
0
 
CEHJCommented:
>>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
0
 
prainAuthor Commented:
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?.
0
 
CEHJCommented:
Are you running a Windows client?
0
 
objectsCommented:
easier to just use Runtime.exec() to run the appropriate command as I suggested earlier.
0
 
CEHJCommented:
If you are, you should map a drive to that network share permanently. Then you can do

Filef = new File("X:/thefile.txt");
0
 
prainAuthor Commented:
CEHJ, That's exact what I found out just now. Will update. Thanks
0
 
objectsCommented:
> 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
0
 
prainAuthor Commented:
CEHJ, Thanks. I got it working.
0
 
CEHJCommented:
No problem
0
 
CEHJCommented:
:-)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now