gpt4 book ai didi

java - 无法在FTP服务器上写入Base64图像数据

转载 作者:行者123 更新时间:2023-12-02 10:40:52 25 4
gpt4 key购买 nike

我在 FTP 上写入 Base64image 数据时遇到问题。当我将其写入本地驱动器时,照片清晰地显示出来。但是,当我将其写入 FTP 服务器时,它看起来像损坏的图像。当我将其写入本地驱动器时,显示如下 enter image description here我已将图片附在 FTP 上。 enter image description here

这是我的代码。

  private static String testFilesDir = "C:\\Storage";
public String getIncidentPhotoByID(int incident_id, int photoId) {
String base64Image = null;
WebSSLClient client = new WebSSLClient();

Response response =client.createRequest(PropertiesUtil.getOracleCloudRestUrL() + "/mobile/platform/storage/collections/incident_photos_collection/objects/incident_462_03").get();

String jsonResponse = response.readEntity(String.class);

base64Image = jsonResponse;
FTPClient ftp = new FTPClient();
FileInputStream fis = null;
String filename = "incident_462_03";

String[] strings = base64Image.split(",");
String extension;
switch (strings[0]) {//check image's extension
case "data:image/jpeg;base64":
extension = "jpeg";
break;
case "data:image/png;base64":
extension = "png";
break;
default://should write cases for more images types
extension = "jpg";
break;
}
//convert base64 string to binary data

byte[] data1 = Base64.decodeBase64(strings[1]);
/*
String path = testFilesDir+"/"+filename+"."+ extension;
File file = new File(path);
try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file))) {
outputStream.write(data1);
} catch (IOException e) {
e.printStackTrace();
} */

try {

ftp.connect("link.myjpl.com");
ftp.login("user", "password");
String path = "Images/test/"+filename+"."+ extension;
OutputStream out1 = ftp.storeFileStream(path);
out1.write(data1);
ftp.logout();

} catch (IOException e) {
e.printStackTrace();
}

return base64Image;
}

}

最佳答案

尝试将文件类型设置为FTP.BINARY_FILE_TYPE。另外,根据 javadocs,要完成文件传输,您必须调用 CompletePendingCommand 并检查其返回值以验证是否成功。

参见https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#storeFileStream(java.lang.String)

关于java - 无法在FTP服务器上写入Base64图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52925231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com