zdm преди 5 години
родител
ревизия
0b128b70ee
променени са 3 файла, в които са добавени 141 реда и са изтрити 27 реда
  1. 2 1
      pom.xml
  2. 18 18
      src/main/java/com/yihu/wlyy/controller/FileDownloadController.java
  3. 121 8
      src/main/java/com/yihu/wlyy/util/FtpFilesUtil.java

+ 2 - 1
pom.xml

@ -87,11 +87,12 @@
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.1</version>
        <version>4.3.6</version>
    </dependency>
    <dependency>
        <groupId>org.apache.axis</groupId>

+ 18 - 18
src/main/java/com/yihu/wlyy/controller/FileDownloadController.java

@ -34,33 +34,33 @@ public class FileDownloadController {
    private  FtpFilesUtil ftpFilesUtil;
    @RequestMapping(value = "fileDownload", method = RequestMethod.GET)
    public Envelop fileDownload(){
        Envelop envelop =new Envelop();
        try {
            JSONArray jsonArray = ftpFilesUtil.getFileListByPath(URL,PORT,USER,PWD, "/data/rec2/2019/0507/08/");
            System.out.println("==================");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return envelop;
    }
//    @RequestMapping(value = "fileDownload", method = RequestMethod.GET)
//    public Envelop fileDownload(){
//        Envelop envelop =new Envelop();
//        try {
//            JSONArray jsonArray = ftpFilesUtil.getFileListByPath(URL,PORT,USER,PWD, "/data/rec2/2019/0507/08/");
//            System.out.println("==================");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return envelop;
//    }
//
    @RequestMapping(value = "fileDownloadLocal", method = RequestMethod.GET)
    public Envelop fileDownloadLocal(){
        Envelop envelop =new Envelop();
    public Envelop fileDownloadLocal() {
        Envelop envelop = new Envelop();
        try {
            ftpFilesUtil.getFileListByPathTest1(URL, PORT, USER, PWD, "/data/rec2/2019/0507/08/");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return envelop;
    }
    @Scheduled(cron="0 0 23 * * ?")
    public void fileDownloadJob(){
    @Scheduled(cron = "0 0 23 * * ?")
    public void fileDownloadJob() {
        try {
            ftpFilesUtil.uploadFtpFileJobByDate(URL,PORT,USER,PWD);
            ftpFilesUtil.uploadFtpFileJobByDate(URL, PORT, USER, PWD);
        } catch (Exception e) {
            e.printStackTrace();
        }

+ 121 - 8
src/main/java/com/yihu/wlyy/util/FtpFilesUtil.java

@ -2,10 +2,23 @@ package com.yihu.wlyy.util;
import com.yihu.wlyy.util.httpUtil.HttpResponse;
import com.yihu.wlyy.util.httpUtil.HttpUtils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.commons.net.ftp.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -305,15 +318,8 @@ public class FtpFilesUtil {
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isFile()) {
                        byte[] byt = getFileStreamFromFTP(this.ftpClient, pathName + files[i].getName());
                        Map<String, Object> params = new HashMap<>();
                        String str = Base64Utils.encode(byt);
                        params.put("name", files[i].getName());
                        params.put("bytes", str);
                        inputStreamUpload(this.ftpClient, pathName , files[i].getName());
                        Log.info("DATE:" + DateUtil.getStringDate() + ";FILES_NAME=" + files[i].getName());
                        HttpResponse res = HttpUtils.doPost(wlyyUrl + "upload/ftpVoiceUpload", params);
                        JSONObject rs = new JSONObject(res.getContent());
                        Log.info("res=" + rs.get("data").toString() + "");
                    }else{
                        String time=files[i].getName();
                        Log.info("filesPathName===================="+pathName+time+"/");
@ -390,6 +396,113 @@ public class FtpFilesUtil {
        return null;
    }
    /**
     * 递归遍历出目录下面所有文件
     * @param pathName 需要遍历的目录,必须以"/"开始和结束
     * @throws IOException
     */
    public JSONArray getFileListByPathTest1(String url,int port,String user,String pwd,String pathName) throws Exception {
        JSONArray jsonArray = new JSONArray();
        //连接ftp服务器
        connectServer(url,port,user,pwd);
        if (pathName.startsWith("/") && pathName.endsWith("/")) {
            //设置访问被动模式
            ftpClient.enterLocalPassiveMode();
            ftpClient.setRemoteVerificationEnabled(false);
            //更换目录到当前目录
            ftpClient.changeWorkingDirectory(pathName);
            //检索ftp目录下所有的文件,利用时间字符串进行过滤
            FTPFile[] files = ftpClient.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isFile()) {
                        int size = (int) files[i].getSize();
                        Log.info("fileSize=========" + size);
                        inputStreamUpload( ftpClient, pathName,files[i].getName());
//                        //获取ftp的文件流
//                        BufferedInputStream inputStream = new BufferedInputStream( ftpClient.retrieveFileStream(pathName + files[i].getName()));
//                        BufferedOutputStream fos = null;
//                        try {
//                            int j;
//                            byte[] bytes = new byte[size];
//                            //指定输出地址
//                            fos =  new BufferedOutputStream(new FileOutputStream("D:\\testFile\\"+files[i].getName()));
//                            // 输出文件
//                            while ((j = inputStream.read(bytes)) != -1) {
//                                Log.info("time=========="+j);
//                                fos.write(bytes, 0, j);
//                            }
//                            fos.flush();
//                            Log.info("FTP文件下载成功!");
//                        } catch (Exception e) {
//                            Log.error("FTP文件下载失败!", e);
//                        } finally {
//                            try {
//                                if (fos != null) {
//                                    fos.close();
//                                }
//                                if (inputStream != null) {
//                                    inputStream.close();
//                                    ftpClient.completePendingCommand();
//                                }
//                            } catch (IOException e) {
//                                e.printStackTrace();
//                            }
//                        }
                    }
                }
            }
        }
        //关闭连接
        closeConnect();
        return jsonArray;
    }
    //====================================================http发送文件流===============================================================================
    public void inputStreamUpload(FTPClient ftpClient, String ftpFilePath,String fileName) throws Exception{
        //获取ftp的文件流
        InputStream inputStream = new BufferedInputStream(ftpClient.retrieveFileStream(ftpFilePath+fileName));
        //创建HttpClient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //请求远程上传数据接口wlyyUrl + "upload/ftpVoiceUpload"
        HttpPost post = new HttpPost(wlyyUrl+"upload/fastDFSVoiceByRequest");
        try {
            //文件路径请换成自己的
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            //第一个参数为 相当于 Form表单提交的file框的name值、第二个参数就是我们要发送的InputStream对象了、第三个参数是文件名
            builder.addBinaryBody("file", inputStream, ContentType.create("multipart/form-data"), fileName);
            //构建请求参数 普通表单项
            StringBody stringBody = new StringBody("12",ContentType.MULTIPART_FORM_DATA);
            builder.addPart("file",stringBody);
            HttpEntity entity = builder.build();
            post.setEntity(entity);
            //发送请求
            CloseableHttpResponse response = client.execute(post);
            entity = response.getEntity();
            if (entity != null) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
                //控制台输出请求结果
                Log.info(result);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                    ftpClient.completePendingCommand();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}