From 1ff4977a3e3826ad148ac003dd57a329ae44edc0 Mon Sep 17 00:00:00 2001 From: hongawen <83944980@qq.com> Date: Mon, 16 Jun 2025 14:32:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=AC=E5=85=B1=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- njcn-common/pom.xml | 7 + .../common/utils/file/FileTransferUtil.java | 164 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 njcn-common/src/main/java/com/njcn/common/utils/file/FileTransferUtil.java diff --git a/njcn-common/pom.xml b/njcn-common/pom.xml index 4e03bdc..686b0c8 100644 --- a/njcn-common/pom.xml +++ b/njcn-common/pom.xml @@ -130,6 +130,13 @@ 3.5.2 + + + eu.agno3.jcifs + jcifs-ng + 2.1.9 + + \ No newline at end of file diff --git a/njcn-common/src/main/java/com/njcn/common/utils/file/FileTransferUtil.java b/njcn-common/src/main/java/com/njcn/common/utils/file/FileTransferUtil.java new file mode 100644 index 0000000..4d33c31 --- /dev/null +++ b/njcn-common/src/main/java/com/njcn/common/utils/file/FileTransferUtil.java @@ -0,0 +1,164 @@ +package com.njcn.common.utils.file; + +import jcifs.CIFSContext; +import jcifs.CIFSException; +import jcifs.context.SingletonContext; +import jcifs.smb.*; + +import java.io.*; +import java.net.MalformedURLException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * @author hongawen + * @version 1.0 + * @data 2025/6/16 14:28 + * 用于向Windows服务器传输文件 + */ +public class FileTransferUtil { + + /** + * 向Windows服务器传输文件 + * @param serverIP 服务器IP地址 + * @param username 用户名 + * @param password 密码 + * @param localFilePath 本地文件路径 + * @param remotePath 远程路径(例如:D:/folder/) + * @return 是否传输成功 + */ + public static boolean transferFile(String serverIP, String username, String password, + String localFilePath, String remotePath) { + try { + // 构建远程文件URL + String remoteUrl = String.format("smb://%s/%s", serverIP, remotePath); + + // 创建认证上下文 + CIFSContext context = SingletonContext.getInstance() + .withCredentials(new NtlmPasswordAuthenticator(username, password)); + + // 获取远程目录 + SmbFile remoteDir = new SmbFile(remoteUrl, context); + if (!remoteDir.exists()) { + remoteDir.mkdirs(); + } + + // 获取本地文件 + Path localPath = Paths.get(localFilePath); + if (!Files.exists(localPath)) { + throw new FileNotFoundException("Local file not found: " + localFilePath); + } + + // 构建远程文件路径 + String fileName = localPath.getFileName().toString(); + String remoteFilePath = remoteUrl + fileName; + SmbFile remoteFile = new SmbFile(remoteFilePath, context); + + // 传输文件 + try (InputStream in = Files.newInputStream(localPath); + OutputStream out = remoteFile.getOutputStream()) { + + byte[] buffer = new byte[8192]; + int len; + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + } + } + + return true; + + } catch (MalformedURLException e) { + System.err.println("Invalid URL: " + e.getMessage()); + return false; + } catch (CIFSException e) { + System.err.println("CIFS error: " + e.getMessage()); + return false; + } catch (IOException e) { + System.err.println("IO error: " + e.getMessage()); + return false; + } + } + + /** + * 向Windows服务器传输文件(带进度回调) + * @param serverIP 服务器IP地址 + * @param username 用户名 + * @param password 密码 + * @param localFilePath 本地文件路径 + * @param remotePath 远程路径 + * @param progressCallback 进度回调接口 + * @return 是否传输成功 + */ + public static boolean transferFileWithProgress(String serverIP, String username, String password, + String localFilePath, String remotePath, + ProgressCallback progressCallback) { + try { + // 构建远程文件URL + String remoteUrl = String.format("smb://%s/%s", serverIP, remotePath); + + // 创建认证上下文 + CIFSContext context = SingletonContext.getInstance() + .withCredentials(new NtlmPasswordAuthenticator(username, password)); + + // 获取远程目录 + SmbFile remoteDir = new SmbFile(remoteUrl, context); + if (!remoteDir.exists()) { + remoteDir.mkdirs(); + } + + // 获取本地文件 + Path localPath = Paths.get(localFilePath); + if (!Files.exists(localPath)) { + throw new FileNotFoundException("Local file not found: " + localFilePath); + } + + // 获取文件大小 + long fileSize = Files.size(localPath); + + // 构建远程文件路径 + String fileName = localPath.getFileName().toString(); + String remoteFilePath = remoteUrl + fileName; + SmbFile remoteFile = new SmbFile(remoteFilePath, context); + + // 传输文件 + try (InputStream in = Files.newInputStream(localPath); + OutputStream out = remoteFile.getOutputStream()) { + + byte[] buffer = new byte[8192]; + int len; + long transferred = 0; + + while ((len = in.read(buffer)) != -1) { + out.write(buffer, 0, len); + transferred += len; + + // 计算并回调进度 + if (progressCallback != null) { + int progress = (int) ((transferred * 100) / fileSize); + progressCallback.onProgress(progress); + } + } + } + + return true; + + } catch (MalformedURLException e) { + System.err.println("Invalid URL: " + e.getMessage()); + return false; + } catch (CIFSException e) { + System.err.println("CIFS error: " + e.getMessage()); + return false; + } catch (IOException e) { + System.err.println("IO error: " + e.getMessage()); + return false; + } + } + + /** + * 进度回调接口 + */ + public interface ProgressCallback { + void onProgress(int progress); + } +}