Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes Copy file from window machine to Linux machine using Java.io.* Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Copy file from window machine to Linux machine using Java.io.*" Watch "Copy file from window machine to Linux machine using Java.io.*" New topic
Author

Copy file from window machine to Linux machine using Java.io.*

Zhiling Spicer
Greenhorn

Joined: Feb 02, 2006
Posts: 4
I am trying to copy a file from Linux machine to window machine by using java.io.*. I don't see any exceptions in logs, but the file just didn't get copied to the destination. The application is running on Linux machine, the destination folder on windows is shared with everyone with full control. Can someone give me some ideas how I can copy a file from Linux to windows by using java? Thanks a lot,

Following is my code....

try{
//input file name... exist in Linux machine...
File inputFile=new File=("/home/test/testing.txt");
FileInputStream instream=new FileInputStream(inputFile);
//output file name...
String outputFile="\\\\networkDrive\\sharedFolder\testing.txt";
OutputStream bos = new FileOutputStream(outputFile);
//FileWriter bos=new FileWriter(realName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = instream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();

}catch(Exception e){

Logging.logException(e, "Upload file has problem");
throw new Exception("Upload file failed!!");
//throw new Exception

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
Hello "Zhiling L."-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

Enjoy your time here.


Ping & DNS - my free Android networking tools app
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
As to your question, the java.io package doesn't know about the Windows SMB/CIFS network file protocol. You can use the jCIFS library. It has an API that is modeled after java.io.File, so if you know that, you should have few problems converting your code to jCIFS.
Zhiling Spicer
Greenhorn

Joined: Feb 02, 2006
Posts: 4
Thank you, Ulf,

Do you know any URL for good documentation or sample code that I can look at it? Thanks in advance.

Zhiling
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
The jCIFS API provides SmbFile, SmbFileInputStream and SmbFileOutputStream classes, which have the same methods as the respective classes of the java.io package. What kind of examples are you looking for beyond that?
Zhiling Spicer
Greenhorn

Joined: Feb 02, 2006
Posts: 4
Ulf, thanks. I just realized that.

I am trying to write to /SharedSeverName/sharedFolder, and my code look like following:

SmbFile sfile=new SmbFile("smb://mydomain;userName assword@SharedSeverName/sharedFolder/");
System.out.println("******** can write ="+sfile.canWrite());
SmbFileOutputStream sfileStream=new SmbFileOutputStream (sfile);

print out shows that sfile has write=true, I get access denied exceptions, is this because userName and password are not right?
Any thought? Thanks


jcifs.smb.SmbException: Access is denied.
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:503)
at jcifs.smb.SmbTransport.send(SmbTransport.java:603)
at jcifs.smb.SmbSession.send(SmbSession.java:229)
at jcifs.smb.SmbTree.send(SmbTree.java:102)
at jcifs.smb.SmbFile.send(SmbFile.java:688)
at jcifs.smb.SmbFile.open0(SmbFile.java:828)
at jcifs.smb.SmbFile.open(SmbFile.java:846)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:139)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:97)
at jcifs.smb.SmbFileOutputStream.<init>(SmbFileOutputStream.java:67)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
I just looked at some old code using jCIFS, and saw a comment that canWrite() didn't work correctly; although in my case it returned false when a file actually was writeable. Try if you can create a file in that directory by hand (from the Linux machine).

The URL I used was "smb://username assword@192.168.110.80/directory/".
Zhiling Spicer
Greenhorn

Joined: Feb 02, 2006
Posts: 4
Thank you, Ulf.

I found that the code is wrong at :
SmbFile sfile=new SmbFile("smb://userName assword@serverName/sharedFolder/");
SmbFileOutputStream sfileStream=new SmbFileOutputStream (sfile);

The code was really trying to create sharedFolder that already exists. That caused access denied error. once I added a file name after sharedFolder, the code works fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Copy file from window machine to Linux machine using Java.io.*