一、访问网络上的SMB共享目录
1.导入JAR包
<!--访问网络上的SMB共享目录-->
<dependency>
<groupId>com.hierynomus</groupId>
<artifactId>smbj</artifactId>
<version>0.13.0</version>
</dependency>
2.使用
String username = "Administrator";
String password = "Password";
String server = "20.135.22.2";
String shareName = "TDMRecv";
String folderPath = "folder";
SMBClient client = new SMBClient();
try {
Connection connection = client.connect(server);
AuthenticationContext ac = new AuthenticationContext(username, password.toCharArray(), null);
Session session = connection.authenticate(ac);
DiskShare shareFolder = (DiskShare) session.connectShare(shareName);
List<FileInfo> files = shareFolder.list(folderPath);
for (FileInfo file : files) {
System.out.println("文件名: " + file.getName());
System.out.println("大小: " + file.getSize() + " 字节");
System.out.println("是否为目录: " + file.isDirectory());
System.out.println("最后修改时间: " + new Date(file.getLastModified()));
System.out.println("------------------------");
}
File localFolder = new File(String.valueOf(targetFolder));
uploadFolder(shareFolder, localFolder ,localFolder);
FileUtils.deleteDirectory(new File(localFolder.getAbsolutePath()));
} catch (IOException e) {
log.error("无法连接到共享或创建文件夹或删除文件失败:" + e.getMessage());
e.printStackTrace();
}
private static void uploadFolder(DiskShare shareFolder, File localFolder, File localFolder2) throws IOException {
if (!localFolder.isDirectory()) {
log.error("本地文件夹不存在:"+localFolder);
}
String localFolderName = "MESRecv\\"+localFolder.getName();
if (!shareFolder.folderExists(localFolderName)&&localFolder2.getName().equals(localFolder.getName())) {
shareFolder.mkdir(localFolderName);
log.info("文件夹创建成功:" + localFolderName);
} else {
log.info("文件夹已存在:" + localFolderName);
}
File[] files = localFolder.listFiles();
if (files != null) {
for (File file : files) {
String remoteFolderPath = "MESRecv\\"+localFolder.getName()+"\\"+file.getName();
if (file.isDirectory()) {
if (!shareFolder.folderExists(remoteFolderPath)) {
shareFolder.mkdir(remoteFolderPath);
uploadFolder(shareFolder, file.getAbsoluteFile(), localFolder2);
log.info("文件夹创建成功:"+remoteFolderPath);
} else {
log.info("文件夹已存在:"+localFolderName);
}
} else {
FileInputStream fis = null;
SmbFileOutputStream fos = null;
try {
File localFile = new File(file.getAbsolutePath());
fis = new FileInputStream(localFile);
String absolutePath = file.getAbsolutePath();
String temporaryFolder = absolutePath.substring(absolutePath.indexOf("temporaryFolder") + "temporaryFolder".length() + 1);
String replace = temporaryFolder.replace("\\", "/");
String remoteFolderPath1 = "MESRecv/" + replace;
String path = "smb://" + shareFolder.getSmbPath().getHostname() + "/" + shareFolder.getSmbPath().getShareName() + "/" + remoteFolderPath1;
SmbFile smbFile = new SmbFile(path);
fos = new SmbFileOutputStream(smbFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
log.info("文件上传成功:" + file.getAbsolutePath());
} finally {
IOUtils.closeQuietly(fis);
IOUtils.closeQuietly(fos);
}
}
}
}
}
接口传输
1.示例
String apiUrl = "http://20.135.168.23:8080/test/receive";
String requestData = "{\"key\":\"value\"}";
log.info("json数据:"+requestData);
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
byte[] input = requestData.getBytes();
outputStream.write(input, 0, input.length);
outputStream.flush();
outputStream.close();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
log.info("响应内容: " + response.toString());
} else {
log.error("请求失败: HTTP error code: " + responseCode);
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
ftp传输
1.获取文件
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ip, port);
ftpClient.login("administrator", "admin@123");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("utf-8");
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("FTP连接失败");
}
System.out.println("FTP连接成功");
FTPFile[] twoFtpFiles = ftpClient.listFiles("/" + DIRNAME);
for (FTPFile file : twoFtpFiles) {
}
2.传输文件
FTPClient ftpClient = new FTPClient();
ftpClient.connect("20.135.22.2", 21);
ftpClient.login("Administrator", "Password123");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("utf-8");
ftpClient.changeWorkingDirectory("/MESSJB");
InputStream inputStream = file.getInputStream();
ftpClient.storeFile(task.get("commCode").toString()+ ".docx", inputStream);
inputStream.close();
ftpClient.logout();
ftpClient.disconnect();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)