1. http://z-jianwen.iteye.com/blog/616059 感谢博主z_jianwen的无私分享。

  1. package com.yss.test.FileReadWriter;  

  2. import java.io.BufferedInputStream;  

  3. import java.io.BufferedOutputStream;  

  4. import java.io.File;  

  5. import java.io.FileInputStream;  

  6. import java.io.FileOutputStream;  

  7. import java.io.IOException;  

  8. import java.io.InputStream;  

  9. import java.io.OutputStream;  

  10. import java.net.MalformedURLException;  

  11. import jcifs.smb.SmbFile;  

  12. import jcifs.smb.SmbFileInputStream;  

  13. import jcifs.smb.SmbFileOutputStream;  

  14. publicclass RemoteAccessData {  

  15. /**

  16.     * @param args

  17.     * @throws IOException

  18.     */

  19. publicstaticvoid main(String[] args) throws IOException {  

  20.        smbGet1("smb://192.168.75.204/test/新建 文本文档.txt");  

  21.        smbGet("smb://192.168.75.204/test/新建 文本文档.txt","e:/");  

  22.    }  

  23. /**

  24.     * 方法一:

  25.     *

  26.     * @param remoteUrl

  27.     *            远程路径 smb://192.168.75.204/test/新建 文本文档.txt

  28.     * @throws IOException

  29.     */

  30. publicstaticvoid smbGet1(String remoteUrl) throws IOException {  

  31.        SmbFile smbFile = new SmbFile(remoteUrl);  

  32. int length = smbFile.getContentLength();// 得到文件的大小

  33. byte buffer[] = newbyte[length];  

  34.        SmbFileInputStream in = new SmbFileInputStream(smbFile);  

  35. // 建立smb文件输入流

  36. while ((in.read(buffer)) != -1) {  

  37.            System.out.write(buffer);  

  38.            System.out.println(buffer.length);  

  39.        }  

  40.        in.close();  

  41.    }  

  42. // 从共享目录下载文件

  43. /**

  44.     * 方法二:

  45.     *    路径格式:smb://192.168.75.204/test/新建 文本文档.txt

  46.     *              smb://username:password@192.168.0.77/test

  47.     * @param remoteUrl

  48.     *            远程路径

  49.     * @param localDir

  50.     *            要写入的本地路径

  51.     */

  52. publicstaticvoid smbGet(String remoteUrl, String localDir) {  

  53.        InputStream in = null;  

  54.        OutputStream out = null;  

  55. try {  

  56.            SmbFile remoteFile = new SmbFile(remoteUrl);  

  57. if (remoteFile == null) {  

  58.                System.out.println("共享文件不存在");  

  59. return;  

  60.            }  

  61.            String fileName = remoteFile.getName();  

  62.            File localFile = new File(localDir + File.separator + fileName);  

  63.            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));  

  64.            out = new BufferedOutputStream(new FileOutputStream(localFile));  

  65. byte[] buffer = newbyte[1024];  

  66. while (in.read(buffer) != -1) {  

  67.                out.write(buffer);  

  68.                buffer = newbyte[1024];  

  69.            }  

  70.        } catch (Exception e) {  

  71.            e.printStackTrace();  

  72.        } finally {  

  73. try {  

  74.                out.close();  

  75.                in.close();  

  76.            } catch (IOException e) {  

  77.                e.printStackTrace();  

  78.            }  

  79.        }  

  80.    }  

  81. // 向共享目录上传文件

  82. publicstaticvoid smbPut(String remoteUrl, String localFilePath) {  

  83.        InputStream in = null;  

  84.        OutputStream out = null;  

  85. try {  

  86.            File localFile = new File(localFilePath);  

  87.            String fileName = localFile.getName();  

  88.            SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);  

  89.            in = new BufferedInputStream(new FileInputStream(localFile));  

  90.            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));  

  91. byte[] buffer = newbyte[1024];  

  92. while (in.read(buffer) != -1) {  

  93.                out.write(buffer);  

  94.                buffer = newbyte[1024];  

  95.            }  

  96.        } catch (Exception e) {  

  97.            e.printStackTrace();  

  98.        } finally {  

  99. try {  

  100.                out.close();  

  101.                in.close();  

  102.            } catch (IOException e) {  

  103.                e.printStackTrace();  

  104.            }  

  105.        }  

  106.    }  

  107. // 远程url smb://192.168.0.77/test

  108. // 如果需要用户名密码就这样:

  109. // smb://username:password@192.168.0.77/test

  110. }