JAVA从局域网共享文件夹中下载上传文件

先下载jar包, http://jcifs.samba.org/
  1. 从共享目录下载文件   
  2.  public static void smbGet(String remoteUrl,String localDir) {       
  3.     InputStream in = null;       
  4.     OutputStream out = null;       
  5.     try {       
  6.         SmbFile remoteFile = new SmbFile(remoteUrl);       
  7.         if(remoteFile==null){       
  8.            System.out.println("共享文件不存在");       
  9.            return;       
  10.         }       
  11.         String fileName = remoteFile.getName();       
  12.         File localFile = new File(localDir+File.separator+fileName);       
  13.         in = new BufferedInputStream(new SmbFileInputStream(remoteFile));       
  14.         out = new BufferedOutputStream(new FileOutputStream(localFile));          
  15.         byte[] buffer = new byte[1024];       
  16.         while(in.read(buffer)!=-1){       
  17.            out.write(buffer);       
  18.            buffer = new byte[1024];       
  19.         }       
  20.     } catch (Exception e) {       
  21.         e.printStackTrace();       
  22.     } finally {       
  23.         try {       
  24.            out.close();       
  25.            in.close();       
  26.         } catch (IOException e) {       
  27.            e.printStackTrace();       
  28.         }       
  29.     }       
  30.  }      
  31. public static void smbGet(String remoteUrl,String localDir) {   
  32.  InputStream in = null;   
  33.  OutputStream out = null;   
  34.  try {   
  35.   SmbFile remoteFile = new SmbFile(remoteUrl);   
  36.   if(remoteFile==null){   
  37.    System.out.println("共享文件不存在");   
  38.    return;   
  39.   }   
  40.   String fileName = remoteFile.getName();   
  41.   File localFile = new File(localDir+File.separator+fileName);   
  42.   in = new BufferedInputStream(new SmbFileInputStream(remoteFile));   
  43.   out = new BufferedOutputStream(new FileOutputStream(localFile));   
  44.   byte[] buffer = new byte[1024];   
  45.   while(in.read(buffer)!=-1){   
  46.    out.write(buffer);   
  47.    buffer = new byte[1024];   
  48.   }   
  49.  } catch (Exception e) {   
  50.   e.printStackTrace();   
  51.  } finally {   
  52.   try {   
  53.    out.close();   
  54.    in.close();   
  55.   } catch (IOException e) {   
  56.    e.printStackTrace();   
  57.   }   
  58.  }   
  59. }    
  60.   
  61. 向共享目录上传文件     
  62.  public static void smbPut(String remoteUrl,String localFilePath) {       
  63.     InputStream in = null;       
  64.     OutputStream out = null;       
  65.     try {       
  66.         File localFile = new File(localFilePath);       
  67.               
  68.         String fileName = localFile.getName();       
  69.         SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);       
  70.         in = new BufferedInputStream(new FileInputStream(localFile));          
  71.         out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));       
  72.         byte[] buffer = new byte[1024];       
  73.         while(in.read(buffer)!=-1){       
  74.            out.write(buffer);       
  75.            buffer = new byte[1024];       
  76.         }       
  77.     } catch (Exception e) {       
  78.         e.printStackTrace();       
  79.     } finally {       
  80.         try {       
  81.            out.close();       
  82.            in.close();       
  83.         } catch (IOException e) {       
  84.            e.printStackTrace();       
  85.         }       
  86.     }       
  87.  }   
 远程url     smb://192.168.0.77/test如果需要用户名密码就这样:
smb://username:password@192.168.0.77/test

转载于:https://my.oschina.net/u/267190/blog/104117

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值