使用FTPClient将图片后上传至ftp服务器

1、上传本地图片至远程ftp服务器

	@Test
    public void uploadFTP() throws IOException {


        String fj = "";//附件信息字段
        FTPClient ftp = new FTPClient();
        // FTP服务器ip地址及端口
        String baseFtpPath = "ftp://102.0.171.174:21";
        // 本地图片绝对地址
        String localFilePath = "G:\\work\\1.png";
		// 图片保存地址 及 保存的图片名
        String ftpPath = "/usr/faceScan/aa/" + "text.png";
        //连接ftp
        if (!ftp.isConnected()) {
            int reply;
            ftp.connect("102.0.171.174", 21); // 连接FTP服务器
            ftp.login("root", "123456"); // 登录FTP
            reply = ftp.getReplyCode(); // 正常返回230登陆成功
            System.out.println(reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
            }
        }

        // 读取本地文件
        File localFile = new File(localFilePath);
        // 获取保存的各级目录名
        String ftpDir = ftpPath.substring(0, ftpPath.lastIndexOf("/"));
        String[] dirs = ftpDir.split("/");
        // 创建目录并修改为ftp的上传目录
        for (String dir : dirs) {
            ftp.makeDirectory(dir);
            ftp.changeWorkingDirectory(dir);
        }
        // 使用流读取图片
        InputStream input = new FileInputStream(localFile);// localFilePath为要上传的本地文件路径
		// 打印输入流读取的内容
        while (true){
            int read = input.read();
            if(read < 0){
                break;
            }
            System.out.println((char)read + "   =========");
        }
        ftp.enterLocalPassiveMode(); // 设置被动模式
        ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); // 设置流上传方式
        ftp.setFileType(FTP.BINARY_FILE_TYPE); // 设置二进制上传
        // 上传 fileName为上传后的文件名
        boolean t =  ftp.storeFile(location.substring(location.lastIndexOf("/") + 1), input); 
        System.out.println(t);
        input.close(); // 关闭文件流
        fj = fj + (baseFtpPath + ftpPath) + ",";
        for (int i = 0; i < dirs.length; i++) {
            ftp.changeToParentDirectory();
        }
        if (ftp.isConnected()) {
            ftp.logout(); // 退出FTP
        }
    }

2、从微信素材库获取图片后上传至远程ftp服务器

 ......
try {
       // 由redis缓存获取accessToken
       Object accessToken = redisTemplate.opsForValue().get("WxConfigAccesstoken");
       byte[] ret = null;
       // 拼接微信素材库图片的获取地址
       String url = "https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=" + accessToken;
       JSONObject jsonVerify = new JSONObject();
       // 微信服务器的图片保存标志
       jsonVerify.put("verify_result", btUser.getCode());
       //doPost  访问微信服务器,获取图片
       ret = HttpConnectionPoolUtil.weixinPostReturnByte(url, jsonVerify.toJSONString(), null);
       if (ret.length < 10000) {
           return BaseRespone.ok(Code.JWT_INSERT_ERROR_CODE, Code.JWT_INSERT_ERROR_MSG);
       }
//
//                    //小于300k不压缩
//                    //压缩区间
//                    byte[] formatImageByte = ImageUtils.compressImage(ret);
//                    //如果异常的话
//                    if (null == formatImageByte) {
//                        formatImageByte = (new BASE64Encoder().encode(ret)).replace("\n", "").getBytes();
//                    }
//                    btUser.setCode(String.valueOf(formatImageByte));
//
       btUser.setCode(String.valueOf(ret));
       String fj = "";//附件信息字段
       FTPClient ftp = new FTPClient();
       // ftp地址
       String baseFtpPath = "ftp://102.0.171.174:21";
       // ftp 文件保存目录
       String ftpPath = "/usr/faceScan/" + UUID.randomUUID().toString() + ".png";
       //连接ftp
       if (!ftp.isConnected()) {
           int reply;
           ftp.connect(ftpIp, ftpPort); // 连接FTP服务器
           ftp.login(username, password); // 登录FTP
           reply = ftp.getReplyCode(); // 正常返回230登陆成功
           if (!FTPReply.isPositiveCompletion(reply)) {
               ftp.disconnect();
           }
       }
       // 获取各级目录
       String ftpDir = ftpPath.substring(0, ftpPath.lastIndexOf("/"));
       String[] dirs = ftpDir.split("/");
       // 创建各级目录,并将ftp的上传目录更改为这个目录
       for (String dir : dirs) {
           ftp.makeDirectory(dir);
           ftp.changeWorkingDirectory(ftpDir);
       }
       // 将从微信服务器获取的图片字节数组装入流中
       InputStream input = new ByteArrayInputStream(ret);
       ftp.enterLocalPassiveMode(); // 设置被动模式
       ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); // 设置流上传方式
       ftp.setFileType(FTP.BINARY_FILE_TYPE); // 设置二进制上传
	   // 上传 fileName为上传后的文件名
       boolean t =  ftp.storeFile(location.substring(location.lastIndexOf("/") + 1), input); 

       input.close(); // 关闭文件流
       fj = fj + (baseFtpPath + ftpPath) + ",";
       for (int i = 0; i < dirs.length; i++) {
           ftp.changeToParentDirectory();
       }
       if (ftp.isConnected()) {
           ftp.logout(); // 退出FTP
       }
   }catch (Exception e){
       e.printStackTrace();
   }
posted @ 2019-06-17 18:57  IT-小浣熊  阅读(203)  评论(0)    收藏  举报