Java FTP上传的图像损坏

时间:2018-12-04 08:11:14

标签: java ftp java-io fileinputstream apache-commons-net

我正在尝试将一些文件上传到本地FTP服务器。可观察列表包含所有文件,并通过循环数组进行上传 我正在使用commons-net-3.6.jar库。

目录和所有内容均已创建,但上传的图像已损坏。
颜色发生了巨大变化(看起来像是带有色彩的旧静态电视图像)
我在做什么错了?

注意!
我注意到的是,文件的大小以KB为单位相同,但字节大小略有不同。

    ObservableList<File> uploadFiles = FXCollections.observableArrayList();

    FTPClient client = new FTPClient();
    InputStream fis = null;

    FTPConnection con = new FTPConnection();
    con.readData();  //gets username and password

    uploadFiles  = Something.getFiles(); //Gets Files

    try {
        client.connect(con.getServerIp());
        client.login(con.getUsername(), con.getPassword());

        String pathname = getPathname();

        client.makeDirectory(pathname);


        for (int i = 0; i < uploadFiles.size(); i++) {


            fis = new FileInputStream(uploadFiles.get(i)); 
            String filename = uploadFiles.get(i).getName();

            String uploadpath = pathname+"/"+filename;
            System.out.println("Uploading File : " + uploadpath);
            client.storeFile(uploadpath, fis);



        }



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




    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {

            e.printStackTrace();

        }
    }

output image sample

1 个答案:

答案 0 :(得分:0)

将文件类型设置为Binary会成功!

client.setFileType(FTP.BINARY_FILE_TYPE);
相关问题