ftp服务器上传下载共享文件

1 windows下搭建ftp服务器

https://blog.csdn.net/qq_34610293/article/details/79210539

搭建好之后浏览器输入 ftp://ip就可以看见弹出账号密码的输入框。(win10家庭版本是没有在计算机管理中显示用户组的,具体添加方法见:https://www.kafan.cn/edu/68801291.html)

2 原生上传下载的demo

<dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.3</version>
        </dependency>

上传controller类

package com.test.domi.controller;


import com.test.domi.common.utils.FtpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/ftp")
public class FTPController {

    @PostMapping("/upload")
    public Boolean pictureUpload(MultipartFile file) throws Exception {
        String newName = UUID.randomUUID().toString();
        String oldName = file.getOriginalFilename();
        newName = newName + oldName.substring(oldName.lastIndexOf("."));
        InputStream inputStream = file.getInputStream();
        return FtpUtil.uploadFile("192.168.6.1",21,"testftp",
                "testftp999","/home/testftp/images","/2015/01/02",newName,inputStream);
    }

    @GetMapping("/download")
    public void pictureDownload(HttpServletResponse response) throws Exception {

        FtpUtil.udownLoadFile("192.168.6.1",21,"testftp",
                "testftp999","/home/testftp/images/2015/01/02","645c4e0a-4c23-4922-994e-84171a61a494.png",response);
    }



}

 工具类

package com.test.domi.common.utils;

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

import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FtpUtil {

    public static boolean uploadFile(String host, int port, String username, String password,
                                     String basePath, String filePath, String filename, InputStream input) {
        FTPClient ftp = new FTPClient();
        Boolean result = false;
        try {
            // 连接FTP服务器
            ftp.connect(host, port);
            // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
            ftp.login(username, password);// 登录
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            //切换到上传目录
            String path = basePath + filePath;
            if (!ftp.changeWorkingDirectory(path)) {
                //如果目录不存在则创建目录
                String[] dirs = path.split("/");
                StringBuffer tempPath = new StringBuffer();
                for (String dir : dirs) {
                    if (null == dir || "".equals(dir)) {
                        continue;
                    }
                    tempPath.append("/").append(dir);
                    if (!ftp.changeWorkingDirectory(tempPath.toString())) {
                        if (!ftp.makeDirectory(tempPath.toString())) {
                            return result;
                        } else {
                            ftp.changeWorkingDirectory(tempPath.toString());
                        }
                    }
                }
            }
            //设置上传文件为二进制类型
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //上传文件默认是10M大小
            //客户端编码等基本设置
            //FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);设置unix下下载大小的限制
            if (!ftp.storeFile(filename, input)) {
                return result;
            }
            input.close();
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }


    public static void udownLoadFile(String host, int port, String username, String password,
                                        String remotePath,String fileName, HttpServletResponse response) {

        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);
            // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
            ftp.login(username, password);// 登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return;
            }
            ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs) {
                String name = ff.getName();
                if (name.equals(fileName)) {
                    response.setContentType("application/form-data");
                    String realFileName = "我的重命名" + name.substring(name.lastIndexOf("."));
                    //"inline; filename=""
                    //+ URLEncoder.encode(title, "UTF-8")
                    //+ ".txt"");
                    response.setHeader("Content-disposition",
                            "attachment; filename=" + new String(realFileName.getBytes("utf-8"), "ISO8859-1"));
                    response.setCharacterEncoding("utf-8");
                    ftp.retrieveFile(ff.getName(), response.getOutputStream());
                }
            }
            ftp.logout();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {

                }
            }

        }


    }
}

3 springboot整合spring Camel 读取ftp文件

<dependency>           
 <groupId>org.apache.camel</groupId>           
 <artifactId>camel-spring-boot-starter</artifactId>  
          <version>2.19.1</version>        
</dependency>       
 <dependency>           
 <groupId>org.apache.camel</groupId>           
 <artifactId>camel-bindy</artifactId>    
        <version>2.19.1</version>       
 </dependency>      
  <dependency>          
  <groupId>org.apache.camel</groupId>    
        <artifactId>camel-ftp-starter</artifactId>  
          <version>2.19.1</version>        
</dependency>

配置文件

待办:

附件FTP服务器,下载的附件不显示设置的名字和后缀,只能重命名后缀才能打开,

对ftp客户端的工具类封装,建立表结构关系,维护fileId与原文件名,存放位置之间的关系,

上传失败时候,在catch中删除文件

原文地址:https://www.cnblogs.com/domi22/p/9744525.html