0

I have a problem with my spring boot application. When I upload a file to my ftp server, the file somehow changes its format. I cannot open the pictures anymore.

I upload them via thymeleaf.
In Controller I use upload service`with the method below:

    public void fileUpload(MultipartFile file) {
        try {
            FtpClient ftp = new FtpClient(ftpHost, ftpPort, ftpUser, ftpPassword);
            ftp.open();
            String tmpdir = System.getProperty("java.io.tmpdir");
            File physicalFile = new File(tmpdir+file.getOriginalFilename());
            file.transferTo(physicalFile);
            ftp.putFileToPath(physicalFile, file.getOriginalFilename());
            ftp.close();

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

And here are my FtpClient methods:

public void open() throws IOException {
    ftp = new FTPClient();

    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

    ftp.connect(server, port);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("Exception in connecting to FTP Server");
    }

    ftp.login(user, password);
}

public void close() throws IOException {
    ftp.disconnect();
}

public void putFileToPath(File file, String path) throws IOException {
    ftp.storeFile(path, new FileInputStream(file));
}

Everything works- the file appears on the server, it has even the same weight.
... but I cannot open it.
Any graphic program I try to use to open it says that it is a wrong file format...

enter image description here

Any ideas?

Mateusz Niedbal
  • 326
  • 4
  • 15

1 Answers1

0

in my FtpClient class - I have added one line
in void open() method:

public void open() throws IOException {
    ftp = new FTPClient();

    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    ftp.connect(server, port);
    int reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new IOException("Exception in connecting to FTP Server");
    }

    ftp.login(user, password);

    //      IT WAS THE PROBLEM - I didn't have it:
    ftp.setFileType(FTP.BINARY_FILE_TYPE);

}

thanks for help

Mateusz Niedbal
  • 326
  • 4
  • 15