增加公共方法
This commit is contained in:
@@ -130,6 +130,13 @@
|
||||
<version>3.5.2</version>
|
||||
</dependency>
|
||||
|
||||
<!--传输文件工具包-->
|
||||
<dependency>
|
||||
<groupId>eu.agno3.jcifs</groupId>
|
||||
<artifactId>jcifs-ng</artifactId>
|
||||
<version>2.1.9</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user