其他分享
首页 > 其他分享> > 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录

将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录

作者:互联网

将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器
需要配置修改的点:
1. 服务器ip端口(服务器ip 端口22/21)。
2. 服务器账号密码(服务器登录用户名密码)。
3. 上传路径(写入的路径会去检查是否存在,若存在则在路径下继续深入,不存在则创建)。
4. 下面的注释很全面很清楚,如遇问题,可留言。
@Slf4j
@Component
public class FtpUtils {

    /**
     * @param 文件
     * @return
     */
    public String ftpUpload(File file){
        FTPClient ftp = new FTPClient();
        String uploadPath ="";//文件上传的路径
        String state = "";//文件上传状态
        try {
            //配置表中取出ftp服务器所在的ip和服务的端口
            String ip = "192.168.11.111";//ftp服务器的ip
            int port = 21;//ftp服务器的端口
            /**
             * 这里修改存放路径
             */
            uploadPath ="aaa/bbb/ccc/";//文件存放的路径名
            ftp.connect(ip, port);//连接ftp服务器
            ftp.login("userName", "passWord");//登录
//            ftp.connect("432.8.129.66",21);
//            ftp.login("ubuntu","root");
            //检验是否连接成功
            int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                log.info("连接失败");
                return "99999";
            }
            uploadPath = uploadPath;
            //更改文件保存的目录
//            ftp.changeWorkingDirectory("/");
            //判断文件夹  创建文件夹
            this.createDir(uploadPath,ftp);
           // ftp.makeDirectory(uploadPath);

            log.info("文件上传的路径:ftp服务器根目录"+uploadPath+file.getName());
            FileInputStream is = new FileInputStream(file);
            //以第一个参数作为文件名,
            ftp.enterLocalPassiveMode();
            ftp.setControlEncoding("UTF-8");
            if(ftp.storeFile(new String(file.getName().getBytes("UTF-8")), is)){
                log.info("上传成功");
                state = "0000";
            }else{
                log.info("上传失败");
                state = "9999";
            }


            ftp.logout();
            is.close();
        } catch (SocketException e) {
            // connec报的
            e.printStackTrace();

            log.info("服务器连接异常!!");

        }catch (IOException e) {
            // connec报的连接异常
            e.printStackTrace();
            log.info("文件服务器连接异常!!");
        }finally{
            //关闭ftp连接
            if(ftp.isConnected()){
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //返回上传状态
        return state;
    }



    public void createDir(String remote,FTPClient ftpClient)throws UnsupportedEncodingException,IOException{
        //将传进来的remote前面的目录路径拿出来
        String directory = remote.substring(0, remote.lastIndexOf("/") + 1);

        if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"), "iso-8859-1"))) {
            // 如果远程目录不存在,则递归创建远程服务器目录
            int start = 0;
            int end = 0;
            //地址开头为/ start=1
            if (directory.startsWith("/")) {
                start = 1;
             } else {
                start = 0;
            }
            end = directory.indexOf("/", start);
            while (true) {
                String subDirectory = new String(remote.substring(start, end).getBytes("GBK"),"iso-8859-1");
                if (!ftpClient.changeWorkingDirectory(subDirectory)) {
                    if (ftpClient.makeDirectory(subDirectory)) {
                        ftpClient.changeWorkingDirectory(subDirectory);
                    } else {
                        System.out.println("创建目录失败");
                    }
                }
                start = end + 1;
                end = directory.indexOf("/", start);

                // 检查所有目录是否创建完毕
                if (end <= start) {
                    break;
                }
             }

         }
    }

}

标签:ftp,uploadPath,String,文件,start,服务器,上传
来源: https://blog.csdn.net/w_monster/article/details/111407473