java使用smb访问网络共享文件

35 篇文章 0 订阅

最近tomcat做了负载均衡,碰到一个难题,就是上传文件的共享问题。 Linux下可以通过NFS来解决这个问题。可以在一部linux server上配置NFS服务器,其他linux server当作NFS客户端。

最后使用的解决方案是使用一个共享地址,该地址可以被多个服务器共同访问。每个服务器通过smb与共享文件进行通信。

实现上传文件到网络共享文件夹和从网络共享文件夹下载文件

实验机器:  Mac OS X 上使用Tomcat 7.0 

          共享文件夹存在与同一个局域网的 win7上

至于Mac OSX 和 win 7 如何设置共享文件夹,可以参考:http://www.jb51.net/os/windows/84034.html  亲测有效


需要使用到的包: jcifs-1.3.15.jar ,我是在 http://download.csdn.net/download/luo201227/7739823 下载

接下来做一个简单的java例子:

首先是main函数,执行两段代码,一段是从网络共享文件夹中上传文件,一段是从网络共享文件夹中下载文件

<span style="font-family:SimSun;font-size:18px;">public static void main(String[] args)
	{
		
		smb test = new Main();
		//本地上传到共享目录
		//smb://用户名:密码@地址/目录
		test.smbPut("smb://chenhong:chenhong@192.168.1.104/shared","/Users/chenhong/Documents/shared/2.java");
		//从共享目录下载到本地
		//smb://用户名:密码@地址/目录/文件
		test.smbGet("smb://chenhong:chenhong@192.168.1.104/shared/1.log","/Users/chenhong/Documents/shared");
		System.out.println("成功");
	}</span>

接下来分别是smbPut方法和smbGet方法



<span style="font-family:SimSun;font-size:18px;">/** 
     * 从本地上传文件到共享目录 
     * @param remoteUrl 共享文件目录 
     * @param localFilePath 本地文件绝对路径 
     */  

	 public void smbPut(String remoteUrl, String localFilePath)  
	    {  
	        InputStream in = null;  
	        OutputStream out = null;  
	        try  
	        {  
	            File localFile = new File(localFilePath);  
	  
	            String fileName = localFile.getName();  
	            SmbFile remoteFile = new SmbFile(remoteUrl+"/"+ fileName);  
	            in = new BufferedInputStream(new FileInputStream(localFile));  
	            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));  
	            byte[] buffer = new byte[1024];  
	            while (in.read(buffer) != -1)  
	            {  
	                out.write(buffer);  
	                buffer = new byte[1024];  
	            }  
	        }  
	        catch (Exception e)  
	        {  
	            e.printStackTrace();  
	        }  
	        finally  
	        {  
	            try  
	            {  
	                out.close();  
	                in.close();  
	            }  
	            catch (IOException e)  
	            {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  </span>

<span style="font-family:SimSun;font-size:18px;">	/** 
	     * 从共享目录拷贝文件到本地 
	     * @param remoteUrl 共享目录上的文件路径 
	     * @param localDir 本地目录 
	     */  
	    public void smbGet(String remoteUrl, String localDir)  
	    {  
	        InputStream in = null;  
	        OutputStream out = null;  
	        try  
	        {  
	            SmbFile remoteFile = new SmbFile(remoteUrl);  
	            //这一句很重要  
	            remoteFile.connect();  
	            if (remoteFile == null)  
	            {  
	                System.out.println("共享文件不存在");  
	                return;  
	            }  
	            String fileName = remoteFile.getName();  
	            File localFile = new File(localDir + File.separator + fileName); 
	            System.out.println(localFile.getAbsolutePath());
	            if(!localFile.exists())
	            {
	            	localFile.createNewFile();
	            }
	          
	            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));  
	            out = new BufferedOutputStream(new FileOutputStream(localFile));  
	            byte[] buffer = new byte[1024];  
	            while (in.read(buffer) != -1)  
	            {  
	                out.write(buffer);  
	                buffer = new byte[1024];  
	            }  
	        }  
	        catch (Exception e)  
	        {  
	            e.printStackTrace();  
	        }  
	        finally  
	        {  
	            try  
	            {  
	                out.close();  
	                in.close();  
	            }  
	            catch (IOException e)  
	            {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	</span>

完整的项目可以下载:java版本  http://download.csdn.net/detail/ch717828/8418329

JSP+Servelt版本的也可以下载:http://download.csdn.net/detail/ch717828/8418331

参考:http://xieruilin.iteye.com/blog/826353

http://zhidao.baidu.com/question/498040518.html   

http://blog.csdn.net/oscar999/article/details/21372227

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值