ftp写出来了
parent
cf91bb8b65
commit
98e5df51cf
@ -1,28 +1,94 @@
|
||||
package com.zhehekeji.web.service.client.image;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ImageSaver {
|
||||
|
||||
private final String targetDirectory;
|
||||
|
||||
public ImageSaver(String targetDirectory) {
|
||||
this.targetDirectory = targetDirectory;
|
||||
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
public void copyFileToFolder(String sourceFilePath, String destinationFolderPath, String fileName) {
|
||||
Path source = Paths.get(sourceFilePath);
|
||||
Path destination = Paths.get(destinationFolderPath, fileName);
|
||||
|
||||
try {
|
||||
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
|
||||
System.out.println("文件复制成功:" + source + " -> " + destination);
|
||||
} catch (IOException e) {
|
||||
System.err.println("文件复制过程中发生错误:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ImageSaver imageSaver = new ImageSaver();
|
||||
imageSaver.uploadFileToFtp("192.168.1.52",
|
||||
"d:\\\\data\\media\\14/3-7/20240524/20240524144747-3-7.jpg");
|
||||
}
|
||||
|
||||
public void saveImage(BufferedImage image, String imageName) {
|
||||
File outputFile = new File(targetDirectory, imageName);
|
||||
public void uploadFileToFtp(String ip,String localFilePath)
|
||||
{
|
||||
try {
|
||||
ImageIO.write(image, "jpg", outputFile);
|
||||
System.out.println("Image saved: " + outputFile.getAbsolutePath());
|
||||
Thread.sleep(120);
|
||||
uploadFileToFtp(ip,"administrator","",localFilePath,"/compute.jpg");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error saving image: " + e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public void uploadFileToFtp(String ip,String username, String password,String localFilePath, String remoteFilePath ) throws IOException {
|
||||
FTPClient ftpClient = new FTPClient();
|
||||
try {
|
||||
// 连接到FTP服务器
|
||||
ftpClient.connect(ip);
|
||||
boolean log =ftpClient.login(username, password);
|
||||
|
||||
ftpClient.enterLocalPassiveMode(); // 设置被动模式,适应大多数网络环境
|
||||
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 设置文件传输模式为二进制
|
||||
|
||||
// 读取本地文件
|
||||
File localFile = new File(localFilePath);
|
||||
InputStream inputStream = new FileInputStream(localFile);
|
||||
System.out.println(ftpClient.isConnected());
|
||||
|
||||
// 上传文件
|
||||
boolean uploadSuccess = ftpClient.storeFile(remoteFilePath, inputStream);
|
||||
if (uploadSuccess) {
|
||||
System.out.println("文件上传成功:" + remoteFilePath);
|
||||
} else {
|
||||
System.out.println("文件上传失败:" + remoteFilePath);
|
||||
}
|
||||
|
||||
// 关闭输入流
|
||||
inputStream.close();
|
||||
} catch (IOException ex) {
|
||||
System.err.println("文件上传过程中发生错误:" + ex.getMessage());
|
||||
throw ex;
|
||||
} finally {
|
||||
// 登出并断开连接
|
||||
if (ftpClient.isConnected()) {
|
||||
try {
|
||||
ftpClient.logout();
|
||||
ftpClient.disconnect();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue