【问题标题】:unable to write Base64 image data on FTP server无法在 FTP 服务器上写入 Base64 图像数据
【发布时间】:2018-10-22 08:34:12
【问题描述】:

我遇到了在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;
}

}

【问题讨论】:

  • 试试out1.flush();
  • 我在 out1.write(data1); 之后添加了.图像显示相同。
  • @MartinPrikryl 是的,我附上了原始图片和损坏的图片。代码也在那里。
  • @MartinPrikryl 我已经把将图像写入本地的注释代码。并且下面的代码注释代码也写入FTP。
  • 尝试将 fileType 设置为 FTP.BINARY_FILE_TYPE 同样按照 javadocs 要完成文件传输,您必须调用 completePendingCommand 并检查其返回值以验证成功。

标签: java ftp base64 filewriter


【解决方案1】:

尝试将文件类型设置为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)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多