• java上传图片到FTP服务器


    一:接口API

    
    	/**
    	 * 上传文件
    	 */
    	@RequestMapping("/upload")
    	public R upload(@RequestParam("file") MultipartFile file) throws Exception {
    		if (file.isEmpty()) {
    			throw new EIException("上传文件不能为空");
    		}
    		InputStream inputStream = file.getInputStream();
    		String fileExt = file.getOriginalFilename();
    
    		FTPClient connectionFTP = ftpClientUtils.getConnectionFTP();
    		ftpClientUtils.uploadFile(connectionFTP,fileExt,inputStream);
    		return R.ok().put("file", "ok");
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二:FTP工具类

    package com.utils;
    
    
    import org.apache.commons.net.ftp.*;
    import org.springframework.stereotype.Component;
    
    import java.io.*;
    import java.util.UUID;
    
    
    @Component
    public class FtpClientUtils {
        //  服务器的ip
        private String hostName = "139.2.2.1";
    
        //  服务器的端口
        private int port = 21;
    
        //  用户名
        private String userName = "root";
    
        //  密码
        private String passWord = "root123";
    
        //  存放的位置
        private String path = "www/wwwroot/wenjian";
    
    
        /**
         * 获得FTP连接方式
         */
        public FTPClient getConnectionFTP() {
            //创建FTPClient对象
            FTPClient ftp = new FTPClient();
            try {
                //连接FTP服务器
                ftp.connect(hostName, port);
    
                //下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件
                ftp.setControlEncoding("GBK");
                FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
                conf.setServerLanguageCode("zh");
    
                //登录ftp
                ftp.login(userName, passWord);
    
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    ftp.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ftp;
        }
    
        /**
         * 关闭连接FTP方式
         * @param ftp FTPClient对象
         * @return boolean
         */
        public boolean closeFTP(FTPClient ftp) {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        }
    
        /**
         * 上传文件到FTP的方式
         * @param ftp FTPClient对象
         * @param fileName 文件名
         * @param inputStream 文件流
         * @return boolean
         */
        public String uploadFile(FTPClient ftp, String fileName, InputStream inputStream) {
    
            try {
    //          让客户端告诉服务端开通一个端口用来数据传输(必须要 不然会一直卡死)
                ftp.enterLocalPassiveMode();
    
    //          获取文件存放的目录
                ftp.changeWorkingDirectory(path);
    
    //          获取文件目录所有的文件
                FTPFile[] fs = ftp.listFiles();
    
    //          判断是否有重复的文件名、有的话就重新生成一个
                fileName = FtpClientUtils.isFileExist(fileName, fs);
    
    //          如果缺省该句 传输txt正常 但图片和其他格式的文件传输出现乱码
                ftp.setFileType(FTP.BINARY_FILE_TYPE);
    
    //          开始上传
                boolean boo  = ftp.storeFile(fileName, inputStream);
                if (boo){
                    return fileName;
                }
    
                //关闭输入流
                inputStream.close();
    
                //退出ftp
                ftp.logout();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 删除文件FTP方式
         * @param ftp FTPClient对象
         * @param fileName FTP服务器上要删除的文件名
         * @return
         */
        public boolean deleteFile(FTPClient ftp, String fileName) {
            boolean success = false;
            try {
    //          让客户端告诉服务端开通一个端口用来数据传输(必须要 不然会一直卡死)
                ftp.enterLocalPassiveMode();
    
    //          获取文件存放的目录
                ftp.changeWorkingDirectory(path);
    
    //          开始删除
                success = ftp.deleteFile(fileName);
    
    //          退出登录
                ftp.logout();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return success;
        }
    
        /**
         * 从ftp服务器上下载文件
         * @param ftp FTPClient对象
         * @param path FTP服务器上传地址
         * @param fileName 本地文件路径
         * @param localPath 本里存储路径
         * @return boolean
         */
        public boolean downFile(FTPClient ftp, String path, String fileName, String localPath) {
            boolean success = false;
            try {
    //          让客户端告诉服务端开通一个端口用来数据传输(必须要 不然会一直卡死)
                ftp.enterLocalPassiveMode();
    
    //          获取文件存放的目录
                ftp.changeWorkingDirectory(path);
    
    //          获取指定目录下的所有文件
                FTPFile[] fs = ftp.listFiles();
    
                for (FTPFile ff : fs) {
                    if (ff.getName().equals(fileName)) {
                        File localFile = new File(localPath + "\\" + ff.getName());
                        OutputStream outputStream = new FileOutputStream(localFile);
                        //将文件保存到输出流outputStream中
                        ftp.retrieveFile(new String(ff.getName().getBytes("GBK"), "ISO-8859-1"), outputStream);
                        outputStream.flush();
                        outputStream.close();
                    }
                }
    //          退出登录
                ftp.logout();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return success;
        }
    
        /**
         * 根据文件名获取文件流
         * @param ftp
         * @param fileName
         * @return
         */
        public InputStream downFile(FTPClient ftp,String fileName) {
    //      让客户端告诉服务端开通一个端口用来数据传输(必须要 不然会一直卡死)
            ftp.enterLocalPassiveMode();
            try {
    //          获取文件存放的目录
                ftp.changeWorkingDirectory(path);
    
    //          获取指定目录下的所有文件
                FTPFile[] fs = ftp.listFiles();
    
                for (FTPFile ff : fs) {
                    if (ff.getName().equals(fileName)) {
                        return ftp.retrieveFileStream(ff.getName());
                    }
                }
    //          退出登录
                ftp.logout();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 判断是否有重名文件
         * @param fileName
         * @param fs
         * @return
         */
        public static String isFileExist(String fileName, FTPFile[] fs) {
            for (FTPFile ftpFile : fs) {
                if (ftpFile.getName().equals(fileName)) {
                    return UUID.randomUUID().toString()+ fileName.substring(fileName.indexOf("."));
                }
            }
            return fileName;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
  • 相关阅读:
    怎么在树莓派上搭建web网站,并发布到外网可访问?
    Linux网络编程系列之UDP组播
    Python钢筋混凝土结构计算.pdf-T001-混凝土强度设计值
    SQL 如何提取多级分类目录
    DAB DETR 论文精度,并解析其模型结构
    Cholesterol-PEG-Azide CLS-PEG-N3 胆固醇-聚乙二醇-叠氮 MW:3400
    透视俄乌网络战之二:Conti勒索软件集团(下)
    阿里云服务器+Frp+Proxifier工具进行内网穿透
    前端学习八股文(2)29-52
    一本书,带你走出Spring新手村
  • 原文地址:https://blog.csdn.net/m0_47944994/article/details/133378996