java jcifs 创建文件夹_java怎么在samba新建文件夹

展开全部

samba上传下载的例子就能满足62616964757a686964616fe78988e69d8331333363366262你的需求:package com.charles.study;

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 jcifs.smb.SmbFile;

import jcifs.smb.SmbFileInputStream;

import jcifs.smb.SmbFileOutputStream;

/**

* 这个工具类提供了从Samba服务器上下载文件到本地目录,以及上传本地文件到Samba服务器指定目录的方法

* @author charles.wang

*

*/

public class SambaFileAccessUtil {

private SambaFileAccessUtil() {}

/**

* 从samba服务器上下载指定的文件到本地目录

* @param remoteFileURL   Samba服务器远程文件的路径

* @param localDir        本地目录的路径

*/

public static void downloadFileFromSamba(String remoteFileURL, String localDir){

//入参检查

if (  (remoteFileURL==null) || ("".equals(remoteFileURL.trim()))){

System.out.println("Samba服务器远程文件路径不可以为空");

return;

}

//入参检查

if( (localDir==null) || ("".equals(localDir.trim()))){

System.out.println("本地目录路径不可以为空");

return;

}

InputStream in = null;

OutputStream out = null;

try{

//创建一个smbFile对象对应远程服务器上的SmbFile

SmbFile remoteSmbFile = new SmbFile(remoteFileURL);

//获取远程文件的文件名,这个的目的是为了在本地的目录下创建一个同名文件

String remoteSmbFileName = remoteSmbFile.getName();

//本地文件由本地目录,路径分隔符,文件名拼接而成

File localFile = new File(localDir+File.separator+remoteSmbFileName);

//打开文件输入流,指向远程的smb服务器上的文件,特别注意,这里流包装器包装了SmbFileInputStream

in= new BufferedInputStream(new SmbFileInputStream(remoteSmbFile));

//打开文件输出流,指向新创建的本地文件,作为最终复制到的目的地

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();

}

}

}

/**

* 上传本地文件到Samba服务器指定目录

* @param remoteDirURL  Samba服务器远程目录的路径

* @param localFilePath 本地文件路径

*/

public static void uploadFileToSamba(String remoteDirURL,String localFilePath){

//入参检查

if (  (remoteDirURL==null) || ("".equals(remoteDirURL.trim()))){

System.out.println("Samba服务器远程目录路径不可以为空");

return;

}

//入参检查

if( (localFilePath==null) || ("".equals(localFilePath.trim()))){

System.out.println("本地文件路径不可以为空");

return;

}

InputStream in = null;

OutputStream out = null;

try{

//创建一个本地文件对象

File localFile = new File(localFilePath);

//获取本地文件的文件名,这个名字用于在远程的Samba服务器上指定目录创建同名文件

String localFileName = localFile.getName();

//创建远程服务器上Samba文件对象

SmbFile remoteSmbFile = new SmbFile(remoteDirURL+File.separator+localFileName);

//打开一个文件输入流执行本地文件,要从它读取内容

in = new  BufferedInputStream( new  FileInputStream(localFile));

//打开一个远程Samba文件输出流,作为复制到的目的地

out = new  BufferedOutputStream( new  SmbFileOutputStream(remoteSmbFile));

//缓冲内存

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();

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值