当前位置: 移动技术网 > IT编程>开发语言>Java > java利用SMB读取远程文件的方法

java利用SMB读取远程文件的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java利用smb读取远程文件的具体代码,供大家参考,具体内容如下

package com.yss.test.filereadwriter; 
 
import java.io.bufferedinputstream; 
import java.io.bufferedoutputstream; 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import java.net.malformedurlexception; 
 
import jcifs.smb.smbfile; 
import jcifs.smb.smbfileinputstream; 
import jcifs.smb.smbfileoutputstream; 
 
public class remoteaccessdata { 
 
 /** 
  * @param args 
  * @throws ioexception 
  */ 
 public static void main(string[] args) throws ioexception { 
  smbget1("smb://192.168.75.204/test/新建 文本文档.txt"); 
  smbget("smb://192.168.75.204/test/新建 文本文档.txt","e:/"); 
 } 
 
 /** 
  * 方法一: 
  * 
  * @param remoteurl 
  *   远程路径 smb://192.168.75.204/test/新建 文本文档.txt 
  * @throws ioexception 
  */ 
 public static void smbget1(string remoteurl) throws ioexception { 
  smbfile smbfile = new smbfile(remoteurl); 
  int length = smbfile.getcontentlength();// 得到文件的大小 
  byte buffer[] = new byte[length]; 
  smbfileinputstream in = new smbfileinputstream(smbfile); 
  // 建立smb文件输入流 
  while ((in.read(buffer)) != -1) { 
 
   system.out.write(buffer); 
   system.out.println(buffer.length); 
  } 
  in.close(); 
 } 
 
 // 从共享目录下载文件 
 /** 
  * 方法二: 
  * 路径格式:smb://192.168.75.204/test/新建 文本文档.txt 
  *    smb://username:password@192.168.0.77/test 
  * @param remoteurl 
  *   远程路径 
  * @param localdir 
  *   要写入的本地路径 
  */ 
 public static void smbget(string remoteurl, string localdir) { 
  inputstream in = null; 
  outputstream out = null; 
  try { 
   smbfile remotefile = new smbfile(remoteurl); 
   if (remotefile == null) { 
    system.out.println("共享文件不存在"); 
    return; 
   } 
   string filename = remotefile.getname(); 
   file localfile = new file(localdir + file.separator + filename); 
   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(); 
   } 
  } 
 } 
 
 // 向共享目录上传文件 
 public static 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(); 
   } 
  } 
 } 
 
 // 远程url smb://192.168.0.77/test 
 // 如果需要用户名密码就这样: 
 // smb://username:password@192.168.0.77/test 
 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网