[Reprinted] access the file shared by network peers through the java jcifs Class Library [Code]

Source: Internet
Author: User

Reprinted fromStupid BLOG:Http://www.mkv8.com /? P = 42

 

The following describes how to use the java jcifs class library to access the shared file code on network peers.
Related Class Library: http://www.mkv8.com /? P = 48

1 public class UploadDownloadUtil
2 {
3
4 /**
5 * Copy files from the shared directory to the local device
6 * @ param remoteUrl: file path in the shared directory
7 * @ param localDir local directory
8 */
9 public void smbGet (String remoteUrl, String localDir)
10 {
11 InputStream in = null;
12 OutputStream out = null;
13 try
14 {
15 SmbFile remoteFile = new SmbFile (remoteUrl );
16 // This sentence is very important
17 remoteFile. connect ();
18 if (remoteFile = null)
19 {
20 System. out. println ("shared file does not exist ");
21 return;
22}
23 String fileName = remoteFile. getName ();
24 File localFile = new File (localDir + File. separator + fileName );
25 In = new bufferedinputstream (New smbfileinputstream (remotefile ));
26 out = new bufferedoutputstream (New fileoutputstream (localfile ));
27 byte [] buffer = new byte [1, 1024];
28 while (in. Read (buffer )! =-1)
29 {
30 out. Write (buffer );
31 buffer = new byte [1024];
32}
33}
34 catch (exception E)
35 {
36 E. printstacktrace ();
37}
38 finally
39 {
40 try
41 {
42 out. Close ();
43 in. Close ();
44}
45 catch (ioexception E)
46 {
47 E. printstacktrace ();
48}
49}
50}
51
52 /**
53 * upload files locally to the shared directory
54 * @ version1.0 Sep 25,200 9 3:49:00
55 * @ param remoteUrl share the file directory
56 * @ param localFilePath: absolute local file path
57 */
58 public void smbPut (String remoteUrl, String localFilePath)
59 {
60 InputStream in = null;
61 OutputStream out = null;
62 try
63 {
64 File localFile = new File (localFilePath );
65
66 String fileName = localFile. getName ();
67 SmbFile remoteFile = new SmbFile (remoteUrl + "/" + fileName );
68 in = new BufferedInputStream (new FileInputStream (localFile ));
69 out = new BufferedOutputStream (new SmbFileOutputStream (remoteFile ));
70 byte [] buffer = new byte [1, 1024];
71 while (in. read (buffer )! =-1)
72 {
73 out. write (buffer );
74 buffer = new byte [1024];
75}
76}
77 catch (Exception e)
78 {
79 e. printStackTrace ();
80}
81 finally
82 {
83 try
84 {
85 out. close ();
86 in. close ();
87}
88 catch (IOException e)
89 {
90 e. printStackTrace ();
91}
92}
93}
94
95 public static void main (String [] args)
96 {
97 uploaddownloadutil test = new uploaddownloadutil ();
98 // SMB: domain name; User name: password @ Destination IP/folder/file name. xxx
99 // test. smbget ("SMB: // szpcg; Jiang. T: xxx@192.168.193.13/Jake/test.txt ",
100 // "C ://");
101
102 // test. smbput ("SMB: // szpcg; Jiang. T: xxx@192.168.193.13/Jake ",
103 // "C: // test.txt ");
104
105
106 // the user name and password cannot contain strong characters, that is, they cannot contain special characters. Otherwise, they will be treated as disconnections.
107 test. smbget ("SMB: // China; xieruilin: 123456Xrl@10.70.36.121/project/report/online problem intelligence analysis help use document .doc ",
108 "C: // temp /");
109
110}
111
112}

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.