ftp代码编写时遇到的坑

目录

1.上传服务器上的文件中文名乱码

2.读取ftp路径下的文件

3.配置ftp下载时的编码转换

4.ftp的maven依赖

5.ftp工具类


1.上传服务器上的文件中文名乱码

解决:需要设置linux服务器的编码问题 

首先判断服务器是否支持中文,使用echo $LANG 命令测试

如果不是中文,需要修改如下:(临时)

1、locale

2.临时更换系统语言

LANG="zh_CN.UTF-8"

3.修改系统默认语言

2.读取ftp路径下的文件

首先需要下载到指定的目录中,然后才能解析读取

3.配置ftp下载时的编码转换

4.ftp的maven依赖

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

5.ftp工具类

详细代码:看github

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.SocketException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
@Slf4j
public class FTPUtil {

    /**
     * 打开FTP服务链接
     * @param ftpHost
     * @param ftpPort
     * @param ftpUserName
     * @param ftpPassword
     */
    public static FTPClient getFTPClient(String ftpHost, Integer ftpPort, String ftpUserName, String ftpPassword){
        FTPClient ftpClient = null;
        try {
            ftpClient = new FTPClient();
            ftpClient.setConnectTimeout(60000);
            if(ftpPort != null){
                ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
            }else {
                ftpClient.connect(ftpHost);// 连接FTP服务器
            }
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                if (ftpClient.login(ftpUserName, ftpPassword)) {// 登陆FTP服务器
                    if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
                            "OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
                        ftpClient.setControlEncoding("UTF-8");
                    }else {
                        ftpClient.setControlEncoding("GBK");
                    }
                    ftpClient.enterLocalPassiveMode();// 设置被动模式
                    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// 设置传输的模式,以二进制流的方式读取
                    ftpClient.enterLocalPassiveMode();
                    System.out.println("FTP服务连接成功!");
                }else {
                    System.out.println("FTP服务用户名或密码错误!");
                    disConnection(ftpClient);
                }
            }else {
                System.out.println("连接到FTP服务失败!");
                disConnection(ftpClient);
            }
        } catch (SocketException e) {
            e.printStackTrace();
            disConnection(ftpClient);
            System.out.println("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            disConnection(ftpClient);
            System.out.println("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }

    /**
     * 关闭FTP服务链接
     * @throws IOException
     */
    public static void disConnection(FTPClient ftpClient){
        try {
            if(ftpClient.isConnected()){
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件夹下的所有文件信息
     * @param path 文件路径
     */
    public static FTPFile[] getFTPDirectoryFiles(FTPClient ftpClient,String path){
        FTPFile[] files = null;
        try {
            ftpClient.changeWorkingDirectory(path);
            files = ftpClient.listFiles();
            log.info(files.toString());
        }catch (Exception e){
            e.printStackTrace();
            //关闭连接
            disConnection(ftpClient);
            System.out.println("FTP读取数据异常!");
        }
        return files;
    }


    /**
     * 获取文件夹下的所有文件信息
     * @param path 文件路径
     */
    public static InputStream getFTPFile(FTPClient ftpClient,String path,String fileName){
        InputStream in = null;
        try {
            ftpClient.changeWorkingDirectory(path);
            FTPFile[] files = ftpClient.listFiles();
            if(files.length > 0){
                in  = ftpClient.retrieveFileStream(fileName);
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("FTP读取数据异常!");
        }finally {
            //关闭连接
            disConnection(ftpClient);
        }
        return in;
    }

    /**
     * 下载ftp服务器文件方法
     * @param ftpClient FTPClient对象
     * @param newFileName 新文件名
     * @param fileName 原文件(路径+文件名)
     * @param downUrl  下载路径
     * @return
     * @throws IOException
     */
    public static boolean downFile(FTPClient ftpClient, String newFileName, String fileName, String downUrl) throws IOException {
        boolean isTrue = false;
        OutputStream os=null;
        File localFile = new File(downUrl + "/" + newFileName);
        if (!localFile.getParentFile().exists()){//文件夹目录不存在创建目录
            localFile.getParentFile().mkdirs();
            localFile.createNewFile();
        }
        os = new FileOutputStream(localFile);
        isTrue =ftpClient.retrieveFile(new String(fileName.getBytes("utf-8"),"iso-8859-1"), os);
        os.close();
        return isTrue;
    }

    public static void main(String args[]){
        InputStream in = null;
        BufferedReader br = null;
        FTPClient ftpClient1 = null;
        try{
            String path = "/var/ftp/aaa/";//设置ftp文件所在的目录
            //读取单个文件
            /*FTPClient ftpClient = getFTPClient("159.226.16.187",21,"zxd","1a2b3c");
            String fileName = "person.txt";
            in = getFTPFile(ftpClient,path,fileName);
            if(in != null){
                br = new BufferedReader(new InputStreamReader(in,"GBK"));
                String data = null;
                while ((data = br.readLine()) != null) {
                    String[] empData = data.split(";");
                    System.out.println(empData[0]+" "+empData[1]);
                }
            }*/

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMmDDHHmmss");
            String dateStr = sdf.format(new Date());

            //读取文件夹下的所有文件
            ftpClient1 = getFTPClient("159.226.16.187",21,"zxd","1a2b3c");
            FTPFile[] files = getFTPDirectoryFiles(ftpClient1,path);

            //List<String> list = new ArrayList<String>();
            if(files != null && files.length > 0){
                for (int i = 0; i < files.length; i++) {
                    if(files[i].isFile()){

                        //in = ftpClient1.retrieveFileStream(remotePath);
                       // br = new BufferedReader(new InputStreamReader(in,"GBK"));
                        //只读取文件后缀名为.docx的文件
                        if(files[i].getName().indexOf(".docx")>=0 &&files[i].getName().length() == (files[i].getName().lastIndexOf(".docx") + ".docx".length())){
                            /*System.out.println("获取的文件名:>>>>>>>>>>>>>>>>>>>>"+files[i].getName());
                            String NewfileName="ddd"+dateStr+".docx";
                            boolean flag =downFile(ftpClient1,NewfileName,path+files[i].getName(),"D:\\file\\");
                            System.out.println(flag);//flag=true说明下载成功
                            WebLoophole bean = new WebLoophole();*/
                            try {
                                /*String remotePath="D:\\file\\"+NewfileName;
                                log.info("文件路径输出>>>>>>>>>>>>>>>>>>>>>>>>>>=========>"+remotePath);
                                List<String> list = POIUtil.readWord("D:\\file\\");
                                if( list !=null){
                                    bean.setWebName(list.get(5));
                                    DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
                                    String replace = list.get(13).replace("年", "-").replace("月", "-").replace("日", "-");
                                    bean.setScanTime(sdf1.parse(replace));
                                    bean.setLoopholeTotal(Integer.parseInt(list.get(35).split(" ")[1]));
                                    bean.setDomain(list.get(22).split("\t")[1]);
                                    bean.setWebUrl(list.get(21).split("\t")[1]);
                                    String [] numArray = list.get(33).split("\t");
                                    bean.setCritical(Integer.parseInt(numArray[0]));
                                    bean.setHigh(Integer.parseInt(numArray[1]));
                                    bean.setMiddle(Integer.parseInt(numArray[2]));
                                    bean.setLow(Integer.parseInt(numArray[3]));
                                    log.info("数据输出=========>"+bean);
                                    //searchDataMapper.insertWebLoopholeLog(bean);
                                }*/

                            } catch (Exception e) {
                                log.error("{}",e);
                            }finally {
                                /*Process process =null;
                                //读取完文件内容,执行脚本命令清空
                                String command1 ="rm "+" "+filePath;
                                try {
                                    Runtime.getRuntime().exec(command1 ).waitFor();
                                } catch (IOException e1) {
                                    log.error("{}",e1);
                                }catch (InterruptedException e) {
                                    log.error("{}",e);
                                }*/
                            }

                        }else{
                            System.out.println("获取的文件名:>>>>>>>>>>>>>>>>>>>>"+files[i].getName());
                            String filePath="D:\\file1\\";
                            boolean flag =downFile(ftpClient1,files[i].getName(),path+files[i].getName(),filePath);
                            System.out.println(flag);//flag=true说明下载成功*/
                            File[] dirFiles = new File(filePath).listFiles();
                            String fileName = null;
                            List<Map<String,Object>> list = null;
                            InputStream is = null;
                            for(File temp : dirFiles){
                                //获取文件路径,包含文件名
                                filePath = temp.getAbsolutePath();
                                //获取文件名
                                fileName = temp.getName();
                                System.out.println(temp.isFile() + " " + temp.getAbsolutePath());
                                // 创建输入流,读取Excel
                                try {
                                    File file1 = new File(filePath);
                                    System.out.println(file1.getAbsolutePath());
                                    is = new FileInputStream(file1.getAbsolutePath());
                                    list = new ArrayList<Map<String,Object>>();
                                    // jxl提供的Workbook类
                                    HSSFWorkbook workbook =  new HSSFWorkbook(is);
                                    Map<String,Object> map = null;
                                    for (int n = 0; n < workbook.getNumberOfSheets(); n++) {
                                        if(workbook.getNumberOfSheets() == 0){
                                            System.out.println("输出为空>>>>>>>>>>>>>>>");
                                            //return list;
                                        }
                                        HSSFSheet sheet = workbook.getSheetAt(i);
                                        System.out.println(sheet.getLastRowNum());
                                        for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                                            if(j == 1){
                                                continue;
                                            }
                                            map = new HashMap<String,Object>();
                                            String ip = sheet.getRow(j).getCell(0)==null?"":sheet.getRow(j).getCell(0).toString().trim();
                                            String machinName = sheet.getRow(j).getCell(1)==null?"":sheet.getRow(j).getCell(1).toString().trim();
                                            String name = sheet.getRow(j).getCell(2)==null?"":sheet.getRow(j).getCell(2).toString().trim();
                                            String riskLevel = sheet.getRow(j).getCell(3)==null?"":sheet.getRow(j).getCell(3).toString().trim();
                                            String stagery = sheet.getRow(j).getCell(4)==null?"":sheet.getRow(j).getCell(4).toString().trim();
                                            String status = sheet.getRow(j).getCell(5)==null?"":sheet.getRow(j).getCell(5).toString().trim();
                                            String descript = sheet.getRow(j).getCell(6)==null?"":sheet.getRow(j).getCell(6).toString().trim();
                                            Date createTime = sheet.getRow(j).getCell(7).getDateCellValue();
                                            map.put("ip", ip);
                                            map.put("machinName", machinName);
                                            map.put("name", name);
                                            map.put("riskLevel", riskLevel);
                                            map.put("stagery", stagery);
                                            map.put("status", status);
                                            map.put("descript", descript);
                                            //Date date = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss").parse(createTime);
                                            map.put("createTime", createTime);
                                            list.add(map);
                                            //searchDataMapper.insertHostLog(list);
                                        }
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                            // return list;
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                //关闭连接
                disConnection(ftpClient1);
                //关闭流
                if (br != null)
                    br.close();
                if (in != null)
                    in.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值