Java uses jcifs to upload and download files from ubuntu to windows Server

Source: Internet
Author: User
Tags auth

CIFS (Common Internet File System) is a universal Internet File System. Because network File sharing between windows hosts is achieved through Microsoft's own CIFS service, therefore, sharing can be achieved directly through the format of // ip/folder/file.txt, but smb: // ip/folder/file.txt must be used across linux, that is, smb protocol. In the java program, it is necessary to implement it in a roundabout way, that is, jcifs.

Jcifs is an implementation of CIFS in JAVA. It is an open-source project that samba organizes to maintain and develop in the spirit of linux. This project focuses on the design and implementation of the cifs protocol using java. They designed jcifs as a complete, rich, scalable, and thread-safe client library. This library can be applied to various Java virtual machines to access network resources that follow the CIFS/SMB network transmission protocol. Similar to the java. io. File interface, it is proved effective and easy to use in the multi-thread mode.
1. usage

Go to Ghost (use zip for win and tgz for linux ).

Decompress the package and you will see some example and docs instruction files, as well as the jcifs jar package. Copy the downloaded jcifsxx. xx. jar to the project directory and include the jar package in the builder path. Like other third-party libraries, you can call the interfaces provided by the jar package in a program.
2. auth

Authentication is the most common part of the program. Because the company has a local domain authentication relationship, it has been a lot of effort and encountered many strange problems. To sum up, there are three errors:

1. The local file or path does not exist.

Path + (No such file or directory)

2. The target file/path does not exist.

Access denied
The system cannot find the path specified.

3. The user name and password used for verification are incorrect.

Logon failure: unknown user name or bad password.
The specified network password is not correct.

3. upload

The complete program is uploaded as follows:

Public static void main (String argv []) throws Exception {
String local = "/home/Desktop/yourlocalFile. name ";
String remote = "smb: // yourappsfolder/SMBtest.doc ";
CopyTo (local, remote );
}

Private static void copyTo (String localName, String remoteName ){
OutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication ("",
"Username", "password ");
Try {
SmbFile remoteFile = new SmbFile (remoteName, auth );
SmbFile remoteDir = new SmbFile (remoteFile. getParent (), auth );
// Create remote folder if not exist
If (! RemoteDir. exists ())
RemoteDir. mkdirs ();
// Create remote file
If (! RemoteFile. exists ())
RemoteFile. createNewFile ();
RemoteFile. setReadWrite ();
Byte [] buf;
Int len;
System. out. println ("Now copying" + localName );
Try {
FileInputStream = new FileInputStream (localName );
FileOutputStream = remoteFile. getOutputStream ();

Buf = new byte [16*1024*1024];
While (len = fileInputStream. read (buf)> 0 ){
FileOutputStream. write (buf, 0, len );
            }
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
FileInputStream. close ();
FileOutputStream. close ();
System. out. println ("Upload" + remoteName + ", done ");
        }
} Catch (Exception e ){
System. err. println ("Exception occured:" + e. getMessage ());
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.