Windows访问Linux共享文件(夹),Linux中Samba的安装

samba安装:
检查安装了samba:rpm -qa | grep samba

在光盘(redhat系统安装盘)中查找samba的rpm文件路径:[root@yangfan /]# find -name samba*

按顺序安装samba的rpm文件:
[root@yangfan Packages]# rpm -ivh samba-3.5.10-125.el6.x86_64.rpm
warning: samba-3.5.10-125.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
error: Failed dependencies:
samba-common = 0:3.5.10-125.el6 is needed by samba-0:3.5.10-125.el6.x86_64
[root@yangfan Packages]# find -name samba*
find: paths must precede expression: samba4-libs-4.0.0-23.alpha11.el6.i686.rpm
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@yangfan Packages]# rpm -ivh samba-common-3.5.10-125.el6.x86_64.rpm
warning: samba-common-3.5.10-125.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:samba-common           ########################################### [100%]
[root@yangfan Packages]# rpm -ivh samba-3.5.10-125.el6.x86_64.rpm
warning: samba-3.5.10-125.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:samba                  ########################################### [100%]
[root@yangfan Packages]# rpm -ivh samba-client-3.5.10-125.el6.x86_64.rpm
warning: samba-client-3.5.10-125.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing...                ########################################### [100%]
   1:samba-client           ########################################### [100%]
[root@yangfan Packages]#

Windows访问Linux共享文件(夹),Linux中Samba的安装_第1张图片


启动samba:service smb start

重行运行Linux Samba:终端运行命令 service smb restart

关闭samba:
[root@yangfan /]# service smb stop
Shutting down SMB services:                                [  OK  ]

关闭防火墙:iptables -F

添加一个samba账号://注意bill必须是已经存在的linux账号
[root@yangfan /]# smbpasswd -a bill
New SMB password:billsmb
Retype new SMB password:billsmb
Added user bill.

不能在共享目录创建文件解决方法:
/etc/selinux/config中修改
#SELINUX=enforcing
SELINUX=disabled



Windows访问Linux共享文件(夹):

package d20130401;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.Date;

import org.omg.PortableInterceptor.SUCCESSFUL;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
//jar package can get from attachment file

public class RemoteShareFile {
  public static void main(String[] args) {
    String smbUrls = "smb://username:password@computername/opt/webhost/pn/etc/pn.conf";
    smbUrls = "smb://ASIAPACIFIC;username:password@computerIp/share/";//GetComputerAddress/PnSnapshotPerformanceTest
    smbUrls = "smb://bill:billsmb@computerIp/bill_share/GetComputerAddress.java";
    
    String localPath = "C:/bill/testShare";
    localPath = "C:/bill/testShare";
    //File file = readFromSmb(smbMachine, localPath);
    
    //readFileFromSever(smbUrls, localPath);
    
    Date startDate = new Date();
    smbUrls = "smb://bill:[email protected]/bill_share";
    localPath = "C:/bill/share/RemoteShareFile.java";
    boolean writeFileSuccess = writeFileToServer(smbUrls, localPath);
    System.out.println("Write file to server is success? " + writeFileSuccess);
    Date endDate = new Date();
    long useMillisecond = endDate.getTime() - startDate.getTime();
    System.out.println("Use millisecond: " + useMillisecond);
    System.out.println("Use second: " + useMillisecond/1000);
    
    System.out.println();
    startDate = new Date();
    //smbUrls = "smb://bill:[email protected]/bill_share";
    localPath = "C:/bill/share/XXXXXX_XXXXXXXXXX_2013_03_26_23_04_08_951.xml";
    writeFileSuccess = writeFileToServer(smbUrls, localPath);
    System.out.println("Write file to server is success? " + writeFileSuccess);
    endDate = new Date();
    useMillisecond = endDate.getTime() - startDate.getTime();
    System.out.println("Use millisecond: " + useMillisecond);
    System.out.println("Use second: " + useMillisecond/1000);
    
    System.out.println("\n-------- Create folder -----------");
    startDate = new Date();
    //smbUrls = "smb://bill:[email protected]/bill_share";
    localPath = "C:/bill/share/test";
    writeFileSuccess = writeFileToServer(smbUrls, localPath);
    System.out.println("Write file to server is success? " + writeFileSuccess);
    endDate = new Date();
    useMillisecond = endDate.getTime() - startDate.getTime();
    System.out.println("Use millisecond: " + useMillisecond);
    System.out.println("Use second: " + useMillisecond/1000);
  }
  
  private static boolean writeFileToServer(String smbUrls, String localPath) {
    Boolean writeFileSuccess = null;
    InputStream is = null;
    OutputStream os = null;
    try {
      File localFile = new File(localPath);
      String fileName = localFile.getName();
      System.out.println(fileName + " is a directory? " + localFile.isDirectory());
      System.out.println(fileName + "'s length is: " + localFile.length());
      /*SmbFile smbFile = null;
      if (localFile.isDirectory()) {
        smbFile = new SmbFile(smbUrls + "/" + fileName + "/");
      } else {
        smbFile = new SmbFile(smbUrls + "/" + fileName);
      }*/
      SmbFile smbFile = new SmbFile(smbUrls + "/" + fileName);
      if (localFile.isDirectory()) {
        if (!smbFile.exists()) {
          smbFile.mkdir();
          System.out.println("Create romote folder successfully: " + smbFile.getURL());
        } else {
          System.out.println("Create romote folder successfully: " + smbFile.getURL());
        }
        return true;
      }
      
      is = new BufferedInputStream(new FileInputStream(localFile));
      os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));
      
      byte[] buffer = new byte[1024];
      int n;
      while((n = is.read(buffer)) != -1) {
        os.write(buffer, 0, n);
      }
      
      writeFileSuccess = true;
    } catch (MalformedURLException e) {
      System.err.println("------------- Create samba file fail ------------------");
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      System.err.println("------------- File not found ------------------");
      e.printStackTrace();
    } catch (SmbException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (UnknownHostException e) {
      System.err.println("------------- Samba file not found ------------------");
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("------------- Read file fail ------------------");
      e.printStackTrace();
    } finally {
      if (writeFileSuccess == null) {
        writeFileSuccess = false;
      }
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          System.err.println("------------- Close input stream fail ------------------");
        }
      }
      
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          System.err.println("------------- Close output stream fail ------------------");
        }
      }
    }
    
    return writeFileSuccess;
  }

  private static File readFileFromSever(String smbUrls, String localPath) {
    File localFile = null;
    InputStream is = null;
    OutputStream os = null;
    
    try {
      SmbFile smbFile = new SmbFile(smbUrls);
      System.out.println("smbFile is directory: " + smbFile.isDirectory());
      /*if (smbFile.isDirectory()) {
        String[] smbFileNameArr = smbFile.list();
        System.out.println("----------- smbFile.list() -----------");
        for (String per : smbFileNameArr) {
          System.out.println(per);
        }
        
        SmbFile[] smbFileArr = smbFile.listFiles();
        System.out.println("----------- smbFile.list() -----------");
        for (SmbFile per : smbFileArr) {
          System.out.println(per.getName());
        }
      }
      
      if (true) {
        return null;
      }*/
      
      String fileName = smbFile.getName();
      is = new BufferedInputStream(new SmbFileInputStream(smbFile));
      
      localFile = new File(localPath + File.separator + fileName);
      os = new BufferedOutputStream(new FileOutputStream(localFile));
      
      byte[] buffer = new byte[1024];
      int n;
      while((n = is.read(buffer)) != -1) {
        os.write(buffer, 0, n);
      }
      
      /*byte[] buffer = new byte[smbFile.getContentLength()];
      is.read(buffer);
      os.write(buffer);*/
      
      System.out.println("-------------- end -------------");
    } catch (MalformedURLException e) {
      System.err.println("------------- Create sever message block file fail ------------------");
      e.printStackTrace();
    } catch (SmbException e) {
      System.err.println("------------- Create sever message block file input stream fail ------------------");
      e.printStackTrace();
    } catch (UnknownHostException e) {
      System.err.println("------------- Create sever message block file input stream fail ------------------");
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      System.err.println("------------- Create local file output stream fail ------------------");
      e.printStackTrace();
    } catch (IOException e) {
      System.err.println("------------- Read remote file fail ------------------");
      e.printStackTrace();
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          System.err.println("------------- Close input stream fail ------------------");
        }
      }
      
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          System.err.println("------------- Close output stream fail ------------------");
        }
      }
    }
    
    return null;
  }

  public static File readFromSmb(String smbMachine, String localpath) {
    File localfile = null;
    InputStream bis = null;
    OutputStream bos = null;
    try {
      SmbFile rmifile = new SmbFile(smbMachine);
      String filename = rmifile.getName();
      bis = new BufferedInputStream(new SmbFileInputStream(rmifile));
      localfile = new File(localpath + File.separator + filename);
      bos = new BufferedOutputStream(new FileOutputStream(localfile));
      int length = rmifile.getContentLength();
      byte[] buffer = new byte[length];
      Date date = new Date();
      bis.read(buffer);
      bos.write(buffer);
      Date end = new Date();
      int time = (int) ((end.getTime() - date.getTime()) / 1000);
      if (time > 0)
        System.out.println(time + ", " + length / time / 1024);
    } catch (Exception e) {
      System.out.println(e.getMessage());
    } finally {
      try {
        bos.close();
        bis.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return localfile;
  }
}




你可能感兴趣的:(windows)