samba远程读写文件

//远程Windows系统下得创建一个共享文件夹,其权限是可读写。

//远程linux系统得启动smb服务,并进行配置

package yzh;
 
import java.io.*;
import java.net.*;
import java.util.*;
import jcifs.smb.*;
 
public class Test5 {
 public static final String REMOTE_HOST_IP="10.11.1.230";
 public static final String LOGIN_ACCOUNT="administrator";
 public static final String LOGIN_PASSWORD="cssodps2008__";
 public static final String SHARE_DOC_NAME="a";
// public static final String REMOTE_HOST_IP="127.0.0.1";
// public static final String LOGIN_ACCOUNT="administrator";
// public static final String LOGIN_PASSWORD="administrator";
// public static final String SHARE_DOC_NAME="a";
//     
 private String remoteHostIp; //远程主机IP   
 private String account;        //登陆账户   
 
 private String password;       //登陆密码   
 private String shareDocName; //共享文件夹名称   
 
 /** 
      * 默认构造函数 
 */ 
 public Test5(){   
       this.remoteHostIp = REMOTE_HOST_IP;  
       this.account = LOGIN_ACCOUNT;  
         this.password = LOGIN_PASSWORD;  
         this.shareDocName = SHARE_DOC_NAME;  
        }  
             
      /** 
           * 构造函数 
           * @param remoteHostIp 远程主机Ip 
           * @param account       登陆账户 
           * @param password      登陆密码 
           * @param sharePath     共享文件夹路径 
           */ 
          public Test5(String remoteHostIp, String account, String password,String shareDocName) {  
               this.remoteHostIp = remoteHostIp;  
              this.account = account;  
              this.password = password;  
              this.shareDocName = shareDocName;  
          }     
             
          /** 
           * 对远程共享文件进行读取所有行 
           * @param remoteFileName 文件名 说明:参数为共享目录下的相对路径 
           *                                若远程文件的路径为:shareDoc/test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
           *                                若远程文件的路径为:shareDoc/doc/text.txt,则参数为doc/text.txt; 
           * @return 文件的所有行 
           */ 
          public List<String> readFile(String remoteFileName){  
              SmbFile smbFile = null;  
              BufferedReader reader = null;  
              List<String> resultLines = null;  
              //构建连接字符串,并取得文件连接   
              String conStr = null;  
              conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;  
              try {  
                  smbFile = new SmbFile(conStr);  
              } catch (MalformedURLException e) {  
                  e.printStackTrace();  
              }  
                 
              //创建reader   
              try {  
                  reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(smbFile)));  
              } catch (SmbException e) {  
                  e.printStackTrace();  
              } catch (MalformedURLException e) {  
                  e.printStackTrace();  
              } catch (UnknownHostException e) {  
                  e.printStackTrace();  
              }         
              //循环对文件进行读取   
              String line;  
              try {  
                  line = reader.readLine();  
                  if(line != null && line.length()>0){  
                      resultLines = new ArrayList<String>();  
                  }  
                  while (line != null) {  
                      resultLines.add(line);  
                       line = reader.readLine();  
                  }  
              } catch (IOException e) {  
                  e.printStackTrace();  
              }  
              //返回   
              return resultLines;  
          }  
             
          /** 
           * 对远程共享文件进行写入 
           * @param is                本地文件的输入流 
           * @param remoteFileName    远程文件名     说明:参数为共享目录下的相对路径 
           *                                             若远程文件的路径为:shareDoc/test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
           *                                             若远程文件的路径为:shareDoc/doc/text.txt,则参数为doc/text.txt; 
           * @return   
           */ 
          public boolean writeFile(InputStream is, String remoteFileName){  
              SmbFile smbFile = null;  
              OutputStream os = null;  
              byte[] buffer = new byte[1024*8];  
              //构建连接字符串,并取得文件连接   
              String conStr = null;  
              conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;  
              try {  
                  smbFile = new SmbFile(conStr);  
              } catch (MalformedURLException e) {  
                  e.printStackTrace();  
                  return false;  
              }  
                 
              //获取远程文件输出流并写文件到远程共享文件夹   
              try {  
                  os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));  
                  while((is.read(buffer))!=-1){  
                      os.write(buffer);         
                  }  
              } catch (Exception e) {  
                  e.printStackTrace();  
                  return false;  
              }   
                 
              return true;  
          }  
             
             
          /** 
           * 对远程共享文件进行写入重载 
           * @param localFileName   要写入的本地文件全名 
           * @param remoteFileName 远程文件名     说明:参数为共享目录下的相对路径 
           *                                           若远程文件的路径为:shareDoc/test.txt,则参数为test.txt(其中shareDoc为共享目录名称);  
           *                                           若远程文件的路径为:shareDoc/doc/text.txt,则参数为doc/text.txt; 
           * @return 
           */ 
          public boolean writeFile(String localFileFullName ,String remoteFileName){  
              try {  
                  return writeFile(new FileInputStream(new File(localFileFullName)),remoteFileName);  
              } catch (FileNotFoundException e) {  
                  e.printStackTrace();  
                  return false;  
              }  
          }  
             
          /** 
           * 对远程共享文件进行写入重载 
           * @param localFileName   要写入的本地文件 
           * @param remoteFileName 远程文件名     说明:参数为共享目录下的相对路径 
           *                                           若远程文件的路径为:shareDoc/test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
           *                                           若远程文件的路径为:shareDoc/doc/text.txt,则参数为doc/text.txt; 
           * @return 
           */ 
          public boolean writeFile(File localFile ,String remoteFileName){  
              try {  
                  return writeFile(new FileInputStream(localFile),remoteFileName);  
              } catch (FileNotFoundException e) {  
                  e.printStackTrace();  
                  return false;  
              }  
          }  
             
             
          public static void main(String[] args){  
          Test5 test = new Test5();
//           System.out.println("aa.txt:----->");
//         List<String> lines = test.readFile("aa.txt");  
//         for (String string : lines) {  
//             System.out.println(string);  
//         }  
              test.writeFile(new File("d:/read.txt"), "bb.txt");  
          }  
 } 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值