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

时间:2018-10-22 08:34:12

标签: java ftp base64 filewriter

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

}

1 个答案:

答案 0 :(得分:1)

尝试将fileType设置为in.something.somethingelse。同样按照javadocs,要完成文件传输,您必须调用FTP.BINARY_FILE_TYPE并检查其返回值以验证是否成功。

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