diff --git a/njcn-common/pom.xml b/njcn-common/pom.xml index 686b0c8..4e03bdc 100644 --- a/njcn-common/pom.xml +++ b/njcn-common/pom.xml @@ -130,13 +130,6 @@ 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 deleted file mode 100644 index 4d33c31..0000000 --- a/njcn-common/src/main/java/com/njcn/common/utils/file/FileTransferUtil.java +++ /dev/null @@ -1,164 +0,0 @@ -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); - } -}