开发者

ftp is corrupting my images! [duplicate]

This question already has answers here: FTP zip upload is corrupted sometimes (2 answers) Closed 4 years ago.

I have a grails app which should upload a bunch of images to a server via FTP. To do that I`m using commons-net. What is weird is, that if I create a new connection for each file it works normal, but if I have connected once and then start sending the files, the files got corrupted! bellow is my code, which works but I dont want to create a new connection for each file:

    filesList.each{ f->

        String ftpUser = ConfigurationHolder.config.ftp.user
        String ftpPassword = ConfigurationHolder.config.ftp.password
        String ftpHost = ConfigurationHolder.config.ftp.host

        log.debug "ftp> ${ftpUser}@${ftpHost}"

        JakartaFtpWrapper ftp = new JakartaFtpWrapper();
        ftp.connectAndLogin(ftpHost, ftpUser, ftpPassword)
        ftp.setDataTimeout(1000*60*60*5)
        log.debug "Welcome message[${ftp.getReplyString()}]"
        log.debug "Current Directory[${ftp.printWorkingDirectory()}]";

        log.debug "remote dir[${remoteDir}]"
        ftp.makeDirectory(remoteDir)
        ftp.cwd(remoteDir)


        log.debug "uploading file path[${f}]..."
        ftp.binary()
        ftp.enterLocalPassiveMode()


        def input  = new FileInputStream(f.getAbsolutePath());
        OutputStream output = ftp.storeFileStream(f.getName())

        Util.copyStream(input, output);

        output.flush()
        input.close();
        output.close();

        ftp.logout();
        ftp.disconnect();
    }

If I remove the connect from the each, the images got corrupted! am I doing something wrong here?

EDIT**: This one DOES NOT work:

    String ftpUser = ConfigurationHolder.config.malibu.ftp.user
    String ftpPassword = ConfigurationHolder.config.malibu.ftp.password
    String ftpHost = ConfigurationHolder.config.malibu.ftp.host

    log.debug "ftp> ${ftpUser}@${ftpHost}"

    JakartaFtpWrapper ftp = new JakartaFtpWrapper();
    ftp.connectAndLogin(ftpHost, ftpUser, ftpPassword)
    ftp.setDataTimeout(1000*60*60*5)
    log.debug "Welcome message[${ftp.getReplyString()}]"
    log.debug "Current Directory[${ftp.printWorkingDirectory()}]";

    log.debug "remote dir[${remoteDir}]"
    ftp.makeDirectory(remoteDir)
    ftp.cwd(remoteDir)

    filesList.each{ f->
        log.debug "uploading file path[${f}]..."
        ftp.binary()
        ftp.enterLocalPassiveMode()

        def input  = new FileInputStream(f.getAbsolutePath());
        OutputStream output = ftp.storeFileStream(f.getName())

    开发者_运维百科    Util.copyStream(input, output);

        output.flush()
        input.close();
        output.close();
    }

    ftp.logout();
    ftp.disconnect();

EDIT :

link for JakartaFtpWrapper : http://www.nsftools.com/tips/JakartaFtpWrapper.java

EDIT 2 :

I`ve already tried adding the ftp.binary() inside, the loop, or outsite. both doesnt work.

One interesting think about it, is, that always the LAST photo got right in the ftp server, it seems that the first ones are more corrupted then the last ones as well!


Try setting the option in JakartaFtpWrapper to transfer in binary mode instead of ascii.

From related question: Java upload jpg using JakartaFtpWrapper - makes the file unreadable


I have two possible suggestions.

First you could try calling ftp.completePendingCommand() after you copy each stream just to see if it finishes before it tries the next transfer. (This is probably the more likely of my suggestions to actually help)

The other thing I was thinking is to remove the output.flush() command as usually this is called in output.close() though honestly if this turns out to be the case, I'll be very surprised.

Hope this helps!


This is what I do, and it works for me:

OutputStream output;

output = new FileOutputStream(localDirectory);

//get the file from the remote system
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftp.retrieveFile(file.getName(), output);

//close output stream
output.flush();
output.close();

The key thing is to set the FileType with a correct type, maybe it could be FTP.BINARY_FILE_TYPE or FTP.IMAGE_FILE_TYPE

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜