标题
    
    
        [smb/java] java访问共享目录  ..smb://xxx:xxx@10.108.23.112/myDo
    
 
    
 	 
       
    
    clq
    
    
    
		
        浏览(0) + 
        2009-05-08 16:51:19 发表
        
编辑
         
        
        
        
        关键字: 
        
 
        
        
        
        
                
        [smb/java] java访问共享目录  ..smb://xxx:xxx@10.108.23.112/myDo java访问共享目录
新一篇: jsf结合myfaces实现文件上传
由于工作需要读取局域网中一台机器的 共享目录中的文件,需要jcifs-1.1.11.jar的支持,使用SMB协议,以下是实现了远程读取并复制到本地,然后删除本地文件的功能:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
public class TestReadSmb ...{
    public static void main(String[] args) ...{
            String smbMachine="smb://10.108.23.200/temp/说明文件.txt";
            String localPath="D:/temp";
            File file=readFromSmb(smbMachine,localPath);
            removeFile(file);
    }
    /** *//**
     * 从smbMachine读取文件并存储到localpath指定的路径
     * 
     * @param smbMachine
     *            共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码
     * @param localpath
     *            本地路径
     * @return
     */
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+"kb/秒");            
        } catch (Exception e) ...{
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
            
        }finally...{
            try ...{
                bos.close();
                bis.close();
            } catch (IOException e) ...{
//                // TODO Auto-generated catch block
                e.printStackTrace();
            }            
        }
        return localfile;
    }
    public static boolean removeFile(File file) ...{
        return file.delete();
    }
}
        
        
        
        		
		        
                
                
     
    
 
	
	
     
    
       
    
    clq
    
    
    
    
    
    		    
    
          
              
    	另外作为服务启动的 tomcat 下的 jsp 的不能访问共享目录的,经我的测试不仅仅是 system 用户的问题.据说是服务不能用交互式登录的问题.以后再找解决方法了.
目前的做法是不注册为服务,用命令行手工启动.比较牛的是 tomcat 6 中没有命令行脚本!解压版本才有,我是用的 NetBeans IDE 6.5.1 自带的 tomcat 6 的 bin 目录copy 的.
    
    
     
 
	 
    
       
    
    clq
    
    
    
    
    
    
    
          
              
    	netbeans-6.5.1-ml-javase-windows.exe  大小 39m  居然没有 jsp 的开发环境.
http://zh-cn.netbeans.org/download/6.5/ml/
它是有多个版本的,选择 java 的那个,212m 大小.
或者是这个完整版本
http://zh-cn.netbeans.org/download/6.5/ml/bundles/netbeans-6.5-ml-windows.exe
    
    
     
 
	 
	
    NEWBT官方QQ群1: 276678893
    可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
    但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
    验证问题说明申请入群原因即可.