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