java spring boot中使用apache common-net下载文件的FTPClient问题

问题描述

显示的日志是这样的: 对于每个请求,它每次都会在日志中添加一个条目。 我不知道它是多次获取相同的资源还是什么?

220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
220-FileZilla Server 0.9.60 beta
220-written by Tim Kosse (tim.kosse@filezilla-project.org)
220 Please visit https://filezilla-project.org/
USER c*******h
USER c*******h
USER c*******h
USER c*******h
331 Password required for c*******h
331 Password required for c*******h
331 Password required for c*******h
331 Password required for c*******h
PASS S*********
PASS S*********
PASS S*********
PASS S*********
230 Logged on
230 Logged on
230 Logged on
230 Logged on
TYPE I
TYPE I
TYPE I
TYPE I
200 Type set to I
200 Type set to I
200 Type set to I
200 Type set to I
PASV
PASV
PASV
PASV
227 Entering Passive Mode (127,1,195,79)
227 Entering Passive Mode (127,79)
RETR /htdocs/frontend/images/mini_18******49$1621268962169.jpeg
RETR /htdocs/frontend/images/mini_18******49$1621268962169.jpeg
RETR /htdocs/frontend/images/mini_18******49$1621268962169.jpeg
RETR /htdocs/frontend/images/mini_18******49$1621268962169.jpeg
150 opening data channel for file download from server of "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
150 opening data channel for file download from server of "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
150 opening data channel for file download from server of "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
150 opening data channel for file download from server of "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
Downloaded file for path: /htdocs/frontend/images/mini_18******49$1621268962169.jpeg
QUIT
QUIT
QUIT
QUIT
226 Successfully transferred "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
226 Successfully transferred "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
226 Successfully transferred "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"
226 Successfully transferred "/htdocs/frontend/images/mini_18******49$1621268962169.jpeg"

我的 ftputil 代码在这里

package com.collreach.userprofile.util;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.multipartfile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

@Component
public class ftputil {

    @Value("${ftp.host}")
    private String host;

    @Value("${ftp.user}")
    private String user;

    @Value("${ftp.pwd}")
    private String pwd;

    @Value("${ftp.host-dir}")
    private String hostDir;

    @Autowired
    FTPClient ftp;
    // FTPClient ftp = null;

    InputStream inputStream = null;

    public String ftpUpload(multipartfile file,String fileName) throws Exception{
        System.out.println("Start");
        FTPConnect(host,user,pwd);
        uploadFile(file,fileName);
        disconnect();
        System.out.println("Done");
        return "Done successfully.";
    }


    public String deletingFile(String fileName) {
        boolean fileDeleted = false;
        try {
            FTPConnect(host,pwd);
            fileDeleted = deleteFile(fileName);
            disconnect();
        }catch(Exception e){
            return "Could not connect.";
        }
        if(fileDeleted)
            return "deleted Successfully.";
        else
            return "No such file exists.";
    }

    public void FTPConnect(String host,String user,String pwd) throws Exception{
        
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
        int reply;
        ftp.connect(host);
        reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            throw new Exception("Exception in connecting to FTP Server");
        }
        ftp.login(user,pwd);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.enterLocalPassiveMode();
    }

    public InputStream downloadFile(String path) throws Exception {
        inputStream = new ByteArrayInputStream(new byte[0]);
        try{
            FTPConnect(host,pwd);
            download(path);
        }
        catch(Exception e){
            System.out.println("Could not connect | download file: " + e);
        }
        finally{
            disconnect();
        }
        return inputStream;
    }

    public void download(String path) throws IOException {
        inputStream =  this.ftp.retrieveFileStream(path);
        System.out.println("Downloaded file for path: " + path);
        inputStream.mark(500);
    }

    public void uploadFile(multipartfile file,String fileName)
            throws Exception {
        try(InputStream input = file.getInputStream()){
            this.ftp.storeFile(fileName,input);
        }
    }

    public boolean deleteFile(String fileName) {
        boolean deleted = false;
        try {
            deleted = this.ftp.deleteFile(hostDir + fileName);
        }catch(Exception e){
            System.out.println("-> :" + e);
        }
        return deleted;
    }

    public void disconnect(){
        if (this.ftp.isConnected()) {
            try {
                this.ftp.logout();
                this.ftp.disconnect();
            } catch (IOException f) {
                // do nothing as file is already saved to server
            }
        }
    }

}

服务层方法,其中ftputil是@Autowired

 @Override
    public InputStream getProfileImageByUsername(String username,Boolean ifMini) throws Exception {
        Optional<UserLogin> user =  userLoginRepository.findByUserName(username);
        Optional<UserPersonalInfo> userInfo;
        if(user.isPresent()){
            userInfo = userPersonalInfoRepository.findById(user.get().getUserPersonalInfo().getUserId());
            String userProfilePhoto;
            if(ifMini)
                userProfilePhoto = userInfo.get().getMiniUserProfilePhoto();
            else
                userProfilePhoto = userInfo.get().getUserProfilePhoto();
            return ftputil.downloadFile(hostDir + userProfilePhoto);
        }
        else {
            return new InputStream() {
                @Override
                public int read() throws IOException {
                    return 0;
                }
            };
        }
    }

我在包含类的 main() 方法中使用这个 bean 以确保单例对象共享 使用@Autowired。

@Bean
    public FTPClient getFTPClient() {
        return new FTPClient();
    }

有时我无法获取图像,并且当多个请求在非常短的时间内出现时,它突然表现出没有提供所有请求的图像,有时甚至根本没有提供任何图像。

有人能指出我在代码中犯了什么错误吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...