FtpUtil.java 19.9 KB
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
//package org.nafmii.common.utils;
//
//import com.alibaba.excel.util.DateUtils;
//import com.google.common.collect.Maps;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.apache.commons.io.IOUtils;
//import org.apache.commons.net.ftp.FTPClient;
//import org.apache.commons.net.ftp.FTPFile;
//import org.apache.commons.net.ftp.FTPReply;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.multipart.MultipartFile;
//import org.springframework.web.multipart.MultipartHttpServletRequest;
//import org.springframework.web.multipart.commons.CommonsMultipartResolver;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.*;
//import java.net.MalformedURLException;
//import java.nio.charset.StandardCharsets;
//import java.util.*;
//
///**
// * @author:zhoujh
// * @Function:TODO
// * @date 2020/5/21 15:13
// * @ClassName:
// * @version:
// * @remark:
// */
//@Slf4j
//@Configuration
//public class FtpUtil {
//    /**访问地址*/
//    @Value("${ftp.att.upload.path}")
//    private static String path;
//
//    /**存放地址*/
//    @Value("${ftp.att.upload.docBase}")
//    private static String docBase;
//
//    //ftp服务器地址
//    @Value("${ftp.att.hostname}")
//    private String hostname;
//    //ftp服务器端口号默认为21
//    @Value("${ftp.att.port}")
//    private Integer port;
//    //ftp登录账号
//    @Value("${ftp.att.username}")
//    private String username;
//    //ftp登录密码
//    @Value("${ftp.att.password}")
//    private String password;
//
//    public FTPClient ftpClient = null;
//
//    public Map<String, Object> upload(HttpServletRequest request){
//        final Map<String, Object> result = new HashMap<>();
//        //初始化多部份解析器
//        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//        //检查了request是否为null,请求是否为post,form中是否有enctype="multipart/form-data"
//        if(multipartResolver.isMultipart(request)){
//            //转换为多部份request
//            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
//            //获取multiRequest中所有文件名
//            Iterator it = multiRequest.getFileNames();
//            while (it.hasNext()){
//                MultipartFile file = multiRequest.getFile(it.next().toString());
//                if(file != null){
//                    //获取文件名
//                    String fileName = file.getOriginalFilename();
//                    // 文件后缀
//                    String suffixes = fileName.substring(fileName.lastIndexOf("."));
//                    // 重命名文件
//                    String nowName = getNewName(suffixes);
//                    final ByteArrayOutputStream fileStream = new ByteArrayOutputStream();
//                    // 存放位置
//                    String savePath = getFilePath(docBase, nowName);
//                    try {
//                        // 复制文件内容
//                        IOUtils.copy(file.getInputStream(), fileStream);
//                        //向session中存入文件内容的引用,为保存文件做准备
//                        request.getSession().setAttribute(nowName,fileStream);
//                        // 复制文件
//                        FileUtils.copyInputStreamToFile(new ByteArrayInputStream(fileStream.toByteArray()), new File(savePath));
//                    } catch (Exception e) {
//                        return null;
//                    }
//                    result.put("nowName",nowName);
//                    result.put("savePath",savePath);
//                    result.put("fileSize",file.getSize());
//                    result.put("url",getFilePath(path,nowName));
//                } else {
//                    return result;
//                }
//            }
//        }
//        return result;
//    }
//
//    public static Map<String, String> upload(String imgBase64){
//        log.info("==============开始上传图片=============");
//        Map<String, String> result = new HashMap<>();
//        if(imgBase64 != null){
//            // 文件后缀
//            String suffixes = ".jpg";
//            // 重命名
//            String nowName = getNewName(suffixes);
//            // 存放位置
//            String savePath = getFilePath(docBase, nowName);
//            try {
////                IOUtils.copy(file.getInputStream(), fileStream);
//                mkdir(savePath.substring(0,savePath.lastIndexOf("/")));
//                FtpUtil.base64MultipartFile(imgBase64,savePath);
////                file.transferTo(new File(savePath));
//                log.info("==============上传成功==============");
//            } catch (Exception e) {
//                log.error("==============出现异常,上传失败==============");
//                return null;
//            }
//            result.put("nowName",nowName);
//            result.put("path",getFilePath(nowName));
//            result.put("savePath",savePath);
////            result.put("url",getFilePath(path,nowName));
//        }
//        return result;
//    }
//
//    public static void main(String[] args) {
//        FtpUtil ft = new FtpUtil();
//        ft.initFtpClient();
//    }
//    /**
//     * 初始化ftp服务器
//     */
//    public void initFtpClient() {
//        hostname="192.168.202.38";
//        port = 21;
//        username = "ypenglv";
//        password = "peng1003";
//        ftpClient = new FTPClient();
//        ftpClient.setControlEncoding("utf-8");
//        try {
//            log.info("...ftp服务器:connecting"+hostname+":"+port+"...."+ DateUtils.format(new Date()));
//            ftpClient.connect(hostname, port); //连接ftp服务器
//            if(!ftpClient.isConnected()){
//                ftpClient.connect(hostname, port);
//            }
//            ftpClient.login(username, password); //登录ftp服务器
//            int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
//            if(!FTPReply.isPositiveCompletion(replyCode)){
//                log.info("connect failed...ftp服务器:"+hostname+":"+port+"...."+DateUtils.format(new Date()));
//            }
//            log.info("connect successfu...ftp服务器:"+hostname+":"+port+"...."+DateUtils.format(new Date()));
//        }catch (MalformedURLException e) {
//            e.printStackTrace();
//        }catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 上传文件
//     * @param pathname ftp服务保存地址
//     * @param fileName 上传到ftp的文件名
//     *  @param originfilename 待上传文件的名称(绝对地址) *
//     * @return
//     */
//    public boolean uploadFile( String pathname, String fileName,String originfilename){
//        boolean flag = false;
//        InputStream inputStream = null;
//        try{
//            log.info("开始上传文件");
//            inputStream = new FileInputStream(new File(originfilename));
//            initFtpClient();
//            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//            CreateDirecroty(pathname);
//            ftpClient.makeDirectory(pathname);
//            ftpClient.changeWorkingDirectory(pathname);
//            ftpClient.storeFile(fileName, inputStream);
//            inputStream.close();
//            ftpClient.logout();
//            flag = true;
//            log.info("上传文件成功");
//        }catch (Exception e) {
//            log.info("上传文件失败");
//            e.printStackTrace();
//        }finally{
//            if(ftpClient.isConnected()){
//                try{
//                    ftpClient.disconnect();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//            }
//            if(null != inputStream){
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return true;
//    }
//    /**
//     * 上传文件
//     * @param path ftp服务保存地址
//     * @param fileName 上传到ftp的文件名重命名的文件名
//     * @param inputStream 输入文件流
//     * @return
//     */
//    public Map<Boolean,String> uploadArrFile(String path, LinkedList<String> fileName, LinkedList<InputStream> inputStream){
//        boolean flag = false;
//        Map<Boolean,String> map = Maps.newHashMap();
//        String realPath = "";
//        try{
//            log.info("======开始上传文件======");
//            log.info("======上传文件地址======"+path+"====系统存储文件名=="+fileName);
//            initFtpClient();
//            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//            realPath = path + "/" + fileName;
//            CreateDirecroty(path);
//            ftpClient.makeDirectory(path);
//            ftpClient.changeWorkingDirectory(path);
//            for(int i=0;i<fileName.size();i++){
//                ftpClient.storeFile(fileName.get(i), inputStream.get(i));
//                inputStream.get(i).close();
//            }
//            ftpClient.logout();
//            flag = true;
//            log.info("======上传文件成功======");
//        }catch (Exception e) {
//            log.error("======上传文件失败======");
//            e.printStackTrace();
//        }finally{
//            if(ftpClient.isConnected()){
//                try{
//                    ftpClient.disconnect();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//            }
//            for(int i=0;i<inputStream.size();i++){
//                if(null != inputStream.get(i)){
//                    try {
//                        inputStream.get(i).close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }
//        map.put(flag,realPath);
//        return map;
//    }
//    /**
//     * 上传文件
//     * @param path ftp服务保存地址
//     * @param fileName 上传到ftp的文件名重命名的文件名
//     * @param inputStream 输入文件流
//     * @return
//     */
//    public Map<Boolean,String> uploadFile(String path, String fileName, InputStream inputStream){
//        boolean flag = false;
//        Map<Boolean,String> map = Maps.newHashMap();
//        String realPath = "";
//        try{
//            log.info("======开始上传文件======");
//            log.info("======上传文件地址======"+path+"====系统存储文件名=="+fileName);
//            initFtpClient();
//            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//            realPath = path + "/" + fileName;
//            CreateDirecroty(path);
//            ftpClient.makeDirectory(path);
//            ftpClient.changeWorkingDirectory(path);
//            ftpClient.storeFile(fileName, inputStream);
//            inputStream.close();
//            ftpClient.logout();
//            flag = true;
//            log.info("======上传文件成功======");
//        }catch (Exception e) {
//            log.error("======上传文件失败======");
//            e.printStackTrace();
//        }finally{
//            if(ftpClient.isConnected()){
//                try{
//                    ftpClient.disconnect();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//            }
//            if(null != inputStream){
//                try {
//                    inputStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        map.put(flag,realPath);
//        return map;
//    }
//    //改变目录路径
//    public boolean changeWorkingDirectory(String directory) {
//        boolean flag = true;
//        try {
//            flag = ftpClient.changeWorkingDirectory(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(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"), StandardCharsets.ISO_8859_1);
//                path = path + "/" + subDirectory;
//                if (!existFile(path)) {
//                    if (makeDirectory(subDirectory)) {
//                        changeWorkingDirectory(subDirectory);
//                    } else {
//                        log.info("创建目录[" + 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);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return flag;
//    }
//
//    /** * 下载文件 *
//     * @param pathname FTP服务器文件目录 *
//     * @param filename 文件名称 *
//     * @param localpath 下载后的文件路径 *
//     * @return */
//    public boolean downloadFile(String pathname, String filename, String localpath){
//        boolean flag = false;
//        OutputStream os=null;
//        try {
//            log.info("======开始下载文件======");
//            initFtpClient();
//            //切换FTP目录
//            ftpClient.changeWorkingDirectory(pathname);
//            FTPFile[] ftpFiles = ftpClient.listFiles();
//            for(FTPFile file : ftpFiles){
//                if(filename.equalsIgnoreCase(file.getName())){
//                    File localFile = new File(localpath + "/" + file.getName());
//                    os = new FileOutputStream(localFile);
//                    ftpClient.retrieveFile(file.getName(), os);
//                    os.close();
//                }
//            }
//            ftpClient.logout();
//            flag = true;
//            log.info("======下载文件成功======");
//        } catch (Exception e) {
//            log.error("======下载文件失败======");
//            e.printStackTrace();
//        } finally{
//            if(ftpClient.isConnected()){
//                try{
//                    ftpClient.disconnect();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//            }
//            if(null != os){
//                try {
//                    os.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return flag;
//    }
//
//    /** * 删除文件 *
//     * @param pathname FTP服务器保存目录 *
//     * @param filename 要删除的文件名称 *
//     * @return */
//    public boolean deleteFile(String pathname, String filename){
//        boolean flag = false;
//        try {
//            log.info("======开始删除文件======");
//            initFtpClient();
//            //切换FTP目录
//            ftpClient.changeWorkingDirectory(pathname);
//            ftpClient.dele(filename);
//            ftpClient.logout();
//            flag = true;
//            log.info("======删除文件成功======");
//        } catch (Exception e) {
//            log.error("======删除文件失败======");
//            e.printStackTrace();
//        } finally {
//            if(ftpClient.isConnected()){
//                try{
//                    ftpClient.disconnect();
//                }catch(IOException e){
//                    e.printStackTrace();
//                }
//            }
//        }
//        return flag;
//    }
//
//    public static void mkdir(String path) {
//        File dir = new File(path);
//        if (dir.exists()) {
//            if (!dir.isFile()) {
//                return;
//            }
//            dir.delete();
//        }
//        dir.mkdirs();
//    }
//
//    /**
//     * 获取文件绝对路径
//     * @param request
//     * @param host
//     * @return
//     */
//    public static String getFileUrl(HttpServletRequest request, String host) {
//        String path = request.getScheme() + "://" + host + request.getContextPath() + "/";
//        int port = request.getServerPort();
//        if (80 != port) {
//            path = request.getScheme() + "://" + host + ":" + request.getServerPort() + request.getContextPath() + "/";
//        }
//        return path;
//    }
//
//    /**
//     * 文件路径
//     * @param pathname
//     * @return
//     */
//    public static String getFilePath(String pathname) {
//        StringBuilder sb = new StringBuilder().append(DateUtils.format(new Date())).append("/").append(pathname);
//        return sb.toString();
//    }
//
//    /**
//     * 文件路径
//     * @param filepath
//     * @param filename
//     * @return
//     */
//    public static String getFilePath(String filepath, String filename) {
//        StringBuilder sb = new StringBuilder(filepath).append(DateUtils.format(new Date())).append("/").append(filename);
//        return sb.toString();
//    }
//
//    /**
//     * 文件重命名
//     * @param suffixes
//     * @return
//     */
//    public static String getNewName(String suffixes){
//        return UUID.randomUUID().toString().replaceAll("\\-", "") + suffixes;
//    }
//
//    /**
//     * 删除文件
//     * @param path
//     */
//    public static void delFiles(String path) {
//        File file = new File(path);
//        if (file.exists()) {
//            file.delete();
//        }
//    }
//    public static boolean base64MultipartFile(String imgStr, String imagePath){
//        if (imgStr == null){
//            return false;
//        }
//        try {
//            String[] baseStr = imgStr.split(",");
//            Base64.Decoder decoder= Base64.getMimeDecoder();
//            byte[] b =  new byte[0];
//            b = decoder.decode(baseStr[1]);
//            for(int i = 0; i < b.length; ++i) {
//                if (b[i] < 0) {
//                    b[i] += 256;
//                }
//            }
//            OutputStream out = new FileOutputStream(imagePath);
//            out.write(b);
//            out.flush();
//            out.close();
//            return true;
//        }catch (Exception e){
//            e.printStackTrace();
//            return false;
//        }
//    }
//}