ftp19216825448010datarecordIdentifyRecords2023-09-2112123411_453_card_rgbjpg 如何获取这个服务资源 java如何获取ftp的图片资源并转成http
要获取这个FTP服务资源,你可以使用Java中的FTP客户端库来实现。以下是一个使用Apache Commons Net库的示例代码:
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FTPExample {
public static void main(String[] args) {
String ftpUrl = "ftp://192.168.25.44:8010";
String filePath = "/data/record/IdentifyRecords/2023-09-21/12/123411_453_card_rgb.jpg";
String httpUrl = "";
FTPClient ftpClient = new FTPClient();
try {
// 连接FTP服务器
ftpClient.connect(ftpUrl);
// 登录FTP服务器
ftpClient.login("username", "password");
// 设置被动模式
ftpClient.enterLocalPassiveMode();
// 设置以二进制方式传输文件
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 获取文件输入流
InputStream inputStream = ftpClient.retrieveFileStream(filePath);
// 将文件写入本地
String localFilePath = "/path/to/save/image.jpg";
OutputStream outputStream = new FileOutputStream(localFilePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 关闭流
inputStream.close();
outputStream.close();
// 完成文件下载后,返回HTTP URL
httpUrl = "http://example.com/path/to/save/image.jpg";
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 登出FTP服务器并断开连接
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("HTTP URL: " + httpUrl);
}
}
在上面的代码中,你需要替换ftpUrl为实际的FTP服务器地址,username和password为FTP服务器的登录凭据,filePath为要获取的图片资源路径,localFilePath为要保存图片的本地路径。
在代码执行完成后,你将得到一个HTTP URL,它是将FTP图片资源转换为HTTP的结果。你可以使用这个URL来获取图片资源。
原文地址: https://www.cveoy.top/t/topic/jfFY 著作权归作者所有。请勿转载和采集!