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

unable to write Base64 image data on FTP server

我在 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 并检查其 return 值以验证成功。

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