1 引用jar 包
<dependency> <groupId>org.samba.jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.3.14-kohsuke-1</version> </dependency>
2 从本地上传文件到服务器共享文件夹
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileOutputStream; /** * * <b>类名称:</b>CopyFileToLan<br/> * <b>类描述:</b> 本地文件写入局域网共享文件夹 <br/> * <b>版本:</b>V1.0<br/> */ public class CopyFileToLan { public static void main(String[] args){ InputStream in = null; OutputStream out = null; try { //测试文件 File localFile = new File("D:\\文档\\test.txt"); String host = "192.168.1.151";//远程服务器的地址 String username = "admin";//用户名 String password = "admin";//密码 String path = "/share/";//远程服务器共享文件夹名称 String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/"); SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName()); remoteFile.connect(); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[4096]; int len = 0; while ((len = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); } catch (Exception e) { String msg = "发生错误:" + e.getLocalizedMessage(); System.out.println(msg); } finally { try { if(out != null) { out.close(); } if(in != null) { in.close(); } } catch (Exception e) {} } } }
3 从服务器共享文件夹下载文件到本地
import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import java.io.*; /** * <b>类名称:</b>CopyLanFileToLocal<br/> * <b>类描述:</b> 读取局域网共享文件夹文件,到本地文件夹 <br/> */ public class CopyLanFileToLocal { public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { //目标文件名 String fileName = "1.jpg"; //本地文件 String localPath = "d:/"; String host = "192.168.1.151";//远程服务器的地址 String username = "admin";//用户名 String password = "admin";//密码 String path = "/share/";//远程服务器共享文件夹名称 String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/"); //创建远程文件对象 SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName); remoteFile.connect(); //创建文件流 in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new BufferedOutputStream(new FileOutputStream(new File(localPath + fileName))); //读取文件内容 byte[] buffer = new byte[4096]; int len = 0; while ((len = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); } catch (Exception e) { String msg = "下载远程文件出错:" + e.getLocalizedMessage(); System.out.println(msg); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (Exception e) { } } } }
SmbFile 和 FIle 对文件的操作基本一致, 在文件复制的时候,想过用 commons-io 的FileUtils.copyFile(final File srcFile, final File destFile);和 java 1.7 的 Files.copy(Path source, Path target, CopyOption... options);但是由于SmbFile 和 File 不属于一个类型,导致失败,最后只能选择使用流的方式进行操作。
原文地址:https://www.cnblogs.com/zwb1234/p/9257584.html
时间: 2024-11-23 14:35:51