ftp写出来了
parent
cf91bb8b65
commit
a21bc9a519
@ -1,28 +1,84 @@
|
|||||||
package com.zhehekeji.web.service.client.image;
|
package com.zhehekeji.web.service.client.image;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardCopyOption;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Service
|
||||||
public class ImageSaver {
|
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 void saveImage(BufferedImage image, String imageName) {
|
public void uploadFileToFtp(String ip,String localFilePath)
|
||||||
File outputFile = new File(targetDirectory, imageName);
|
{
|
||||||
try {
|
try {
|
||||||
ImageIO.write(image, "jpg", outputFile);
|
uploadFileToFtp(ip,"admin","",localFilePath,"/compute.jpg");
|
||||||
System.out.println("Image saved: " + outputFile.getAbsolutePath());
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error saving image: " + e.getMessage());
|
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);
|
||||||
|
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