本文实例为大家分享了java利用SMB读取远程文件的具体代码,供大家参考,具体内容如下
包com.yss.test.FileReadWriter; 进口java.io.BufferedInputStream; 进口java.io.BufferedOutputStream; 进口java.io.File; 进口java.io.FileInputStream; 进口java.io.FileOutputStream; 进口java.io.IOException; 进口java.io.InputStream; 进口java.io.OutputStream; 进口java.net.MalformedURLException; 进口jcifs.smb.SmbFile; 进口jcifs.smb.SmbFileInputStream; 进口jcifs.smb.SmbFileOutputStream; 公开课RemoteAccessData {/* * * @param arg游戏 * @throws IOException */公共静态void main (String [] args)抛出IOException { smbGet1 (“smb://192.168.75.204/测试/新建文本文档. txt”); smbGet (“smb://192.168.75.204/测试/新建文本文档. txt”,“e:/?; }/* * *方法一: * * @param remoteUrl *远程路径smb://192.168.75.204/测试/新建文本文档. txt * @throws IOException */公共静态孔隙smbGet1(字符串remoteUrl)抛出IOException { SmbFile SmbFile=new SmbFile (remoteUrl); int长度=smbFile.getContentLength();//得到文件的大小 字节缓冲区[]=新字节(长度); SmbFileInputStream=新SmbFileInputStream (smbFile);//建立smb文件输入流 而((in.read(缓冲区))!=1){ System.out.write(缓冲); System.out.println (buffer.length); } in.close (); }//从共享目录下载文件/* * *方法二: *路径格式:smb://192.168.75.204/测试/新建文本文档. txt * smb://用户名:password@192.168.0.77/测试 * @param remoteUrl *远程路径 * @param localDir *要写入的本地路径 */公共静态孔隙smbGet (remoteUrl的字符串,字符串localDir) { InputStream=零; OutputStream=零; 尝试{ SmbFile remoteFile=new SmbFile (remoteUrl); 如果(remoteFile==null) { System.out.println(“共享文件不存在”); 返回; } 字符串文件名=remoteFile.getName (); 文件localFile=新文件(localDir +文件。分离器+文件名); 在新BufferedInputStream=(新SmbFileInputStream (remoteFile));=新BufferedOutputStream(新FileOutputStream (localFile)); byte[]新字节缓冲区=[1024]; 而(in.read(缓冲)!=1){ out.write(缓冲); 新字节缓冲区=[1024]; } }捕捉(异常e) { e.printStackTrace (); 最后}{ 尝试{ out.close (); in.close (); }捕捉(IOException e) { e.printStackTrace (); } } }//向共享目录上传文件 公共静态孔隙smbPut (remoteUrl的字符串,字符串localFilePath) { InputStream=零; OutputStream=零; 尝试{ 文件localFile=新文件(localFilePath); 字符串文件名=localFile.getName (); SmbFile remoteFile=new SmbFile (remoteUrl + " +文件名); 在新BufferedInputStream=(新FileInputStream (localFile));=新BufferedOutputStream(新SmbFileOutputStream (remoteFile)); byte[]新字节缓冲区=[1024]; 而(in.read(缓冲)!=1){ out.write(缓冲); 新字节缓冲区=[1024]; } }捕捉(异常e) { e.printStackTrace (); 最后}{ 尝试{ out.close (); in.close (); }捕捉(IOException e) { e.printStackTrace (); } } }//远程url smb://192.168.0.77/测试//如果需要用户名密码就这样://smb://用户名:password@192.168.0.77/测试 }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。