java:在共享目录下新建文件夹及

我们需要一个jar包
jcifs-1.3.19.jar

 /**
  * 
  * @param remoteUrl 远程路径
  * @param newFilePath  文件路径
  * @param content  写入内容
  */
 public void smbPut(String remoteUrl,String newFilePath,String content){
  try {
   OutputStream out = null;
   if(newFilePath.lastIndexOf("/")==-1){
    System.out.println("newFilePath的格式不对");
    return;
   }
   //文件夹路径  datafolder
   String folder = newFilePath.substring(0, newFilePath.lastIndexOf("/"));
   
   //新建远程的文件夹
   SmbFile remoteFile = new SmbFile(remoteUrl + "/" + folder);
   //判断文件夹是否存在,否则创建
   if(!remoteFile.exists()){
    remoteFile.mkdir();
   }
   
   //新建远程文件
   remoteFile = new SmbFile(remoteUrl + "/" + newFilePath);
   
   out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
   
   //使用输出流写入内容
   byte[] buffer = content.getBytes("utf-8");
   for (byte b : buffer) {
    out.write(b);
    buffer = new byte[1024];
   }
   
   //关闭资源
   out.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 
  
 }
 
 @Test
 public void testSmb(){
  //remoteUrl:  sub:域名;用户名:密码@目标ip/文件夹
  //newFilePath: aa/bb/cc/data.txt 或者/data.txt
  smbPut("smb://username:pwd@202.117.114.54/datafolder", "aa/bb/cc/data.txt", "内容");
 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值