您的位置:首页 > 编程语言 > Java开发

创建共享文件和文件夹

2017-03-07 11:30 169 查看
1.手动新建一个共享文件夹produce_pdf_to_share

2.produce_pdf_to_share文件夹右键属性,设置为共享。在高级共享中,点击权限,添加用户,这个用户的用户名密码就是就是代码下方smb中要填的。

3.导入jcifs-1.2.6.jar包,示例代码如下:

public class SmbIOUtil {
//将一个本地文件写入共享文件夹
public static void smbPut(String remoteUrl,String localFilePath){
String separator="/";
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);

String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl+separator+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();
}
}
}

public static void main(String[] args) throws Exception{
// smb串必须是"/"分割符,"\"会报错
smbPut("smb://用户名:密码@ip地址/produce_pdf_to_share", "D:\\produce_pdf\\20170227164904.pdf");
}  }


4.在共享文件夹中创建文件夹(最初的共享文件夹{produce_pdf_to_share}因为需要设置用户等,需要手动创建,之后在produce_pdf_to_share下创建的文件夹需要是共享文件夹,需要SmbFile 类创建,File类创建会报错)

SmbFile shareDir=new SmbFile("smb://用户名:密码@ip地址/produce_pdf_to_share/文件夹");
if(!shareDir.exists()){
shareDir.mkdirs();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 文件共享