Java FTP下载的图像未正确显示

Java FTP下载的图像未正确显示,java,image,spring-mvc,tomcat,ftp,Java,Image,Spring Mvc,Tomcat,Ftp,我将用户上传的图像保存在FTP中。 FTP服务正在服务器A上运行。实际的问题是,当我希望看到在本地主机上运行的web应用程序上传的映像时,一切正常,但当我将本地应用程序部署到同一服务器A上运行的Tomcat时,映像显示不正确 在本地Tomcat中运行web应用程序时的图片: 在远程Tomcat中运行web应用程序时出现了相同的情况: 您可以看到第二个图像没有正确显示。还要提到的是,FTP是同一个 我使用Spring和ApacheFTPClient库实现图像上传/下载功能 控制器源代码: @Req

我将用户上传的图像保存在FTP中。 FTP服务正在服务器A上运行。实际的问题是,当我希望看到在本地主机上运行的web应用程序上传的映像时,一切正常,但当我将本地应用程序部署到同一服务器A上运行的Tomcat时,映像显示不正确

在本地Tomcat中运行web应用程序时的图片:

在远程Tomcat中运行web应用程序时出现了相同的情况:

您可以看到第二个图像没有正确显示。还要提到的是,FTP是同一个

我使用Spring和ApacheFTPClient库实现图像上传/下载功能

控制器源代码:

@RequestMapping(value = "/{id:\\d+}/image", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
protected byte[] getUserImage(BaseForm form,
                              @PathVariable("id") int userId) {

    try {
        User user = checkToken(form.getToken());

        log.info("/users/{id}/image [GET]. User: " + user + ", form: " + form + ", User id: " + userId);

        FileWrapper image = service.getUserImage(userId);

        if(image != null) {
            return ftpService.downloadFtpFile(image.getName());
        }
    }
    catch(Exception e) {
        log.error(e.getMessage(), e);
    }

    return null;
}
public byte[] downloadFtpFile(String filePath) throws IOException {

    FTPClient client = new FTPClient();

    try {
        client.connect(host, port);
        if(!client.login(username, password)) {
            throw new AdminException("Invalid ftp username/password");
        }

        client.enterLocalPassiveMode();

        try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            client.retrieveFile(filePath, outputStream);
            return outputStream.toByteArray();
        }
    }
    catch(Exception e) {
        log.error(e.getMessage(), e);
    }
    finally {
        if(client.isConnected()) {
            client.logout();
            client.disconnect();
        }
    }

    return null;
}
FtpService源代码:

@RequestMapping(value = "/{id:\\d+}/image", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
protected byte[] getUserImage(BaseForm form,
                              @PathVariable("id") int userId) {

    try {
        User user = checkToken(form.getToken());

        log.info("/users/{id}/image [GET]. User: " + user + ", form: " + form + ", User id: " + userId);

        FileWrapper image = service.getUserImage(userId);

        if(image != null) {
            return ftpService.downloadFtpFile(image.getName());
        }
    }
    catch(Exception e) {
        log.error(e.getMessage(), e);
    }

    return null;
}
public byte[] downloadFtpFile(String filePath) throws IOException {

    FTPClient client = new FTPClient();

    try {
        client.connect(host, port);
        if(!client.login(username, password)) {
            throw new AdminException("Invalid ftp username/password");
        }

        client.enterLocalPassiveMode();

        try(ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            client.retrieveFile(filePath, outputStream);
            return outputStream.toByteArray();
        }
    }
    catch(Exception e) {
        log.error(e.getMessage(), e);
    }
    finally {
        if(client.isConnected()) {
            client.logout();
            client.disconnect();
        }
    }

    return null;
}

提前谢谢

如果您没有将FTP传输设置为二进制而不是ASCII,它将转换行尾或它认为会损坏图片的行尾。

您是否检查了文件中的具体差异?同一FTP中的文件是相同的。在一种情况下,我从运行在本地机器上的web应用程序下载文件,它可以正常工作,但在第二种情况下,我从运行在远程机器上的web应用程序下载相同的文件,FTP服务位于何处?此文件显示不正确。请比较在本地和远程Tomcat上获得的文件。显然第二个是布罗肯,问题是有什么区别。可能文件被截断或某些字节被破坏。您是否尝试将FTP设置为二进制模式,而不是ASCII?是的。如果设置为ASCII,它将转换行尾或它认为是行尾的内容。