欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ftp文件上传下载

程序员文章站 2022-03-11 20:07:41
...

springboot实现ftp文件上传下载

1.添加ftp依赖

        <!-- ftp文件上传 -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>

2.ftp工具类

package com.demo.utlis;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import java.io.*;
import java.net.SocketException;

/**
 * ftp工具类
 */
public class FtpUtil {

    private String ftpIp = "";
    private String ftpUserName = "";
    private String ftpPassword = "";
    private Integer ftpPort = 21;
    private Integer httpPort = 80;
    public FTPClient ftpClient = null;


   /**
     * ftp下载文件
     * @param ftpPath  ftp服务器路径
     * @param localPath  保存文件的路径
     * @param fileName  文件名称
     * @throws Exception
     */
    public void downFile(String ftpPath,String localPath,String fileName) throws Exception {
        try {
            ftpClient = this.getFTPClient(ftpIp,ftpUserName,ftpPassword,ftpPort);
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.changeWorkingDirectory(ftpPath);
            File localFile = new File(localPath + File.separatorChar + fileName);
            OutputStream os = new FileOutputStream(localFile);
            ftpClient.retrieveFile(fileName, os);
            os.close();
            ftpClient.logout();

        } catch (FileNotFoundException e) {
            System.out.println("没有找到" + ftpPath + "文件");
            e.printStackTrace();
        } catch (SocketException e) {
            System.out.println("连接FTP失败.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取错误。");
            e.printStackTrace();
        }
    }




    /**
     * Description: 向FTP服务器上传文件
     * FTP服务器中文件所在路径 格式: ftptest/aa
     * @param fileName ftp文件名称
     * @param input 文件流
     * @return 成功返回true,否则返回false
     */
    public  boolean uploadFile(String ftpPath,String fileName, InputStream input) {
        boolean success = false;
        try {
            int reply;
            ftpClient = getFTPClient(ftpIp, ftpUserName, ftpPassword, ftpPort);
            reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                return success;
            }
            ftpClient.setControlEncoding("UTF-8"); // 中文支持
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();

            if(ftpPath!=null && !"".equals(ftpPath)) {
                CreateDirecroty(ftpPath); //在ftp服务器中创建文件夹
            }
            ftpClient.changeWorkingDirectory(ftpPath);
            ftpClient.storeFile(fileName, input);
            input.close();
            ftpClient.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

    /**
     * 获取FTPClient对象
     * @param ftpIp     FTP主机服务器
     * @param ftpPassword FTP 登录密码
     * @param ftpUserName FTP登录用户名
     * @param ftpPort     FTP端口 默认为21
     * @return
     */
    public FTPClient getFTPClient(String ftpIp, String ftpUserName,String ftpPassword, int ftpPort) {
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient = new FTPClient();
            ftpClient.connect(ftpIp,ftpPort);// 连接FTP服务器
            ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            } else {
                System.out.println("FTP连接成功。");
            }
        } catch (SocketException e) {
            e.printStackTrace();
            System.out.println("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }


    //改变目录路径
    public boolean changeWorkingDirectory(String directory) {
        boolean flag = true;
        try {
            flag = ftpClient.changeWorkingDirectory(directory);
            if (flag) {
                System.out.println("进入文件夹" + directory + " 成功!");

            } else {
                System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return flag;
    }

    //创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
    public boolean CreateDirecroty(String remote) throws IOException {
        boolean success = true;
        String directory = remote + "/";
        // 如果远程目录不存在,则递归创建远程服务器目录
        if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
            int start = 0;
            int end = 0;
            if (directory.startsWith("/")) {
                start = 1;
            } else {
                start = 0;
            }
            end = directory.indexOf("/", start);
            String path = "";
            String paths = "";
            while (true) {
                String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
                path = path + "/" + subDirectory;
                if (!existFile(path)) {
                    if (makeDirectory(subDirectory)) {
                        changeWorkingDirectory(subDirectory);
                    } else {
                        System.out.println("创建目录[" + subDirectory + "]失败");
                        changeWorkingDirectory(subDirectory);
                    }
                } else {
                    changeWorkingDirectory(subDirectory);
                }

                paths = paths + "/" + subDirectory;
                start = end + 1;
                end = directory.indexOf("/", start);
                // 检查所有目录是否创建完毕
                if (end <= start) {
                    break;
                }
            }
        }
        return success;
    }

    //判断ftp服务器文件是否存在
    public boolean existFile(String path) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);
        if (ftpFileArr.length > 0) {
            flag = true;
        }
        return flag;
    }
    //创建目录
    public boolean makeDirectory(String dir) {
        boolean flag = true;
        try {
            flag = ftpClient.makeDirectory(dir);
            if (flag) {
                System.out.println("创建文件夹" + dir + " 成功!");

            } else {
                System.out.println("创建文件夹" + dir + " 失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }



    public String getFtpIp() {return ftpIp;}
    public void setFtpIp(String ftpIp) {this.ftpIp = ftpIp;}
    public String getFtpUserName() {return ftpUserName;}
    public void setFtpUserName(String ftpUserName) {this.ftpUserName = ftpUserName;}
    public String getFtpPassword() {return ftpPassword;}
    public void setFtpPassword(String ftpPassword) {this.ftpPassword = ftpPassword;}
    public Integer getFtpPort() {return ftpPort;}
    public void setFtpPort(Integer ftpPort) {this.ftpPort = ftpPort;}
    public Integer getHttpPort() {return httpPort;}
    public void setHttpPort(Integer httpPort) {this.httpPort = httpPort;}
    public FTPClient getFtpClient() {return ftpClient;}
    public void setFtpClient(FTPClient ftpClient) {this.ftpClient = ftpClient;}

    public FtpUtil(String ftpIp, String ftpUserName, String ftpPassword, Integer ftpPort, Integer httpPort) {
        this.ftpIp = ftpIp;
        this.ftpUserName = ftpUserName;
        this.ftpPassword = ftpPassword;
        this.ftpPort = ftpPort;
        this.httpPort = httpPort;
    }

    public FtpUtil() {
    }
}

3.controller代码

package com.demo.controller;

import com.demo.utlis.FtpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value="ftp",tags="ftp文件上传")
@RequestMapping("ftp")
public class FtpController {

    //ftp服务器信息
    private String ftpIp = "182.16.1.134";  
    private String ftpUserName = "ftp_user";
    private String ftpPassword = "123";
    private Integer ftpPort = 21;
    private Integer httpPort = 80;


        @RequestMapping(value = "uploadfile",method= RequestMethod.POST)
        @ApiOperation(value="ftp文件上传")
        public Map<String,Object> uploadFile(MultipartFile file,String ftpPath) throws Exception {
            Map<String,Object> map = new HashMap<String,Object>();
            InputStream inputStream=file.getInputStream();//获取上传的文件流
            String filename=file.getOriginalFilename();//获取上传的文件名

            FtpUtil ftp = new FtpUtil();
            ftp.setFtpIp(ftpIp);
            ftp.setFtpUserName(ftpUserName);
            ftp.setFtpPassword(ftpPassword);
            ftp.setFtpPort(ftpPort);
            boolean bool = ftp.uploadFile(ftpPath,filename,inputStream);
            map.put("success",bool);
            return map;
        }

        @RequestMapping(value="/downUploadFile",method= RequestMethod.GET)
        @ApiOperation(httpMethod="GET",value="ftp下载文件",notes="ftp下载文件")
        public void downuploadFile(String ftpPath,String localPath,String fileName) throws Exception {
            FtpUtil ftp = new FtpUtil();
            ftp.setFtpIp(ftpIp);
            ftp.setFtpUserName(ftpUserName);
            ftp.setFtpPassword(ftpPassword);
            ftp.setFtpPort(ftpPort);
            ftp.downFile(ftpPath,localPath,fileName);
        }

        @RequestMapping(value="/downUrl",method= RequestMethod.GET)
        @ApiOperation(httpMethod="GET",value="获取下载url",notes="获取下载url")
        public String downuploadFile(String ftpPath, String fileName) throws Exception {

            return "http://"+ftpIp+":"+httpPort+"/"+ftpPath+"/"+fileName;
        }


}

 Ftp_Server 搭建ftp服务器时的跟目录,参数ftpPath为空时默认改路径

ftp文件上传下载

 

相关标签: 文件上传下载