JAVA 监听文件夹变化,模拟POST提交文件流同步服务器文件

1.目录监听

package com.example.file_sync.utils;

import com.sun.istack.internal.logging.Logger;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;

/**
 * @title: FileListener
 * @projectName file_sync
 * @Date: 2021/7/22 9:53
 * @Author: lei.yu-esint
 * @Description:
 */
public class FileListener extends FileAlterationListenerAdaptor {
    private Logger log = Logger.getLogger(FileListener.class);

    /**
     * 文件创建执行
     */
    @Override
    public void onFileCreate(File file) {
        log.info("[新建]:" + file.getAbsolutePath());
    }

    /**
     * 文件创建修改
     */
    @Override
    public void onFileChange(File file) {
        log.info("[修改]:" + file.getAbsolutePath());
    }

    /**
     * 文件删除
     */
    @Override
    public void onFileDelete(File file) {
        log.info("[删除]:" + file.getAbsolutePath());
    }

    /**
     * 目录创建
     */
    @Override
    public void onDirectoryCreate(File directory) {
        log.info("[新建]:" + directory.getAbsolutePath());
    }

    /**
     * 目录修改
     */
    @Override
    public void onDirectoryChange(File directory) {
        log.info("[修改]:" + directory.getAbsolutePath());
    }

    /**
     * 目录删除
     */
    @Override
    public void onDirectoryDelete(File directory) {
        log.info("[删除]:" + directory.getAbsolutePath());
    }

    @Override
    public void onStart(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStart(observer);
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStop(observer);
    }

}

2.模拟调用

package com.example.file_sync;

import com.example.file_sync.utils.FileListener;
import com.example.file_sync.utils.PostRequest;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class FileSyncApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(FileSyncApplication.class, args);
        //文件开始同步
        doFileSunc();

        /**
         * 文件数:110,515
         * 文件夹大小:2.4G
         * 每轮遍历时间:2分30秒
         *
         * 所以如果每秒进行遍历的话,最大同步时间是:遍历时间X2 , 也就是五分钟
         */

    }

    //数据同步
    private static void doFileSunc() throws Exception {
        // 监控目录
        String rootDir = "F:\\ES";

        // 轮询间隔 5 秒
        long interval = TimeUnit.SECONDS.toMillis(1);

        // 创建过滤器,可以监听指定的文件
//        IOFileFilter directories = FileFilterUtils.and(
//                FileFilterUtils.directoryFileFilter(),
//                HiddenFileFilter.VISIBLE);
//        IOFileFilter files       = FileFilterUtils.and(
//                FileFilterUtils.fileFileFilter(),
//                FileFilterUtils.suffixFileFilter(".txt"));
//        IOFileFilter filter = FileFilterUtils.or(directories, files);
//        使用过滤器
//        FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir), filter);

        //不使用过滤器,监听目录下的全部类型的文件
        FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir));

        observer.addListener(new FileListener());
        //创建文件变化监听器
        FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
        // 开始监控
        monitor.start();
    }

}

3.通过监听文件夹、上传文件到指定服务器实现的文件同步

3.1 项目结构(spring boot 2.1.5)
在这里插入图片描述
下面是各个代码:

controller

package com.example.file_sync.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.file_sync.utils.FileListener;
import com.example.file_sync.utils.PostRequest;
import com.sun.istack.internal.logging.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @title: FileSyncController
 * @projectName file_sync
 * @Date: 2021/7/22 14:13
 * @Author: lei.yu-esint
 * @Description:
 */
@RestController
public class FileSyncController {

    private Logger log = Logger.getLogger(FileListener.class);

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public void uploadFile(HttpServletRequest request, HttpServletResponse response) {
        String fileName = request.getParameter("fileName");
        log.info("filename:"+fileName);
        String fileFullPath = "F:/ES/aaa/" + fileName;
        InputStream input = null;
        FileOutputStream fos = null;
        try {
            input = request.getInputStream();
            File file = new File("F:/ES/aaa/");
            if(!file.exists()){
                file.mkdirs();
            }
            fos = new FileOutputStream(fileFullPath);
            int size = 0;
            byte[] buffer = new byte[1024];
            while ((size = input.read(buffer,0,1024)) != -1) {
                fos.write(buffer, 0, size);
            }

            //响应信息 json字符串格式
            Map<String,Object> responseMap = new HashMap<String,Object>();
            responseMap.put("flag", true);

            //生成响应的json字符串
            String jsonResponse = JSONObject.toJSONString(responseMap);
            PostRequest.sendResponse(jsonResponse,response);
        } catch (IOException e) {
            //响应信息 json字符串格式
            Map<String,Object> responseMap = new HashMap<String,Object>();
            responseMap.put("flag", false);
            responseMap.put("errorMsg", e.getMessage());
            String jsonResponse = JSONObject.toJSONString(responseMap);
//            PostRequest.sendResponse(jsonResponse,response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            if(input != null){
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @RequestMapping("/doUploadFile")
    public void douploadFile() {

        //模拟发送数据
        String pathName = "F:/ES/aaa/1.txt";
        String fileName="1.txt";
        try {
            PostRequest.uploadFile(pathName,fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

FileListener

package com.example.file_sync.utils;

import com.sun.istack.internal.logging.Logger;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;

/**
 * @title: FileListener
 * @projectName file_sync
 * @Date: 2021/7/22 9:53
 * @Author: lei.yu-esint
 * @Description:
 */
public class FileListener extends FileAlterationListenerAdaptor {
    private Logger log = Logger.getLogger(FileListener.class);

    /**
     * 文件创建执行
     */
    @Override
    public void onFileCreate(File file) {
        log.info("[新建]:" + file.getAbsolutePath());
    }

    /**
     * 文件创建修改
     */
    @Override
    public void onFileChange(File file) {
        log.info("[修改]:" + file.getAbsolutePath());
    }

    /**
     * 文件删除
     */
    @Override
    public void onFileDelete(File file) {
        log.info("[删除]:" + file.getAbsolutePath());
    }

    /**
     * 目录创建
     */
    @Override
    public void onDirectoryCreate(File directory) {
        log.info("[新建]:" + directory.getAbsolutePath());
    }

    /**
     * 目录修改
     */
    @Override
    public void onDirectoryChange(File directory) {
        log.info("[修改]:" + directory.getAbsolutePath());
    }

    /**
     * 目录删除
     */
    @Override
    public void onDirectoryDelete(File directory) {
        log.info("[删除]:" + directory.getAbsolutePath());
    }

    @Override
    public void onStart(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStart(observer);
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStop(observer);
    }

}

package com.example.file_sync.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.FileInputStream;

/**
 * @title: PostRequest
 * @projectName file_sync
 * @Date: 2021/7/22 15:19
 * @Author: lei.yu-esint
 * @Description:
 */

public class PostRequest {

    private static final String host = "http://localhost:8020";

    private static final String doUrl = host + "/uploadFile";

    String outFilePath = PropertiesUtil.readValue("params.properties", "fileUpload.decriptTest");

    /**
     * 模拟POST 上传文件
     * @param pathName
     * @param fileName
     * @throws Exception
     */
    public static void uploadFile(String pathName,String fileName) throws Exception {

        DataInputStream in = null;
        OutputStream out = null;
        HttpURLConnection conn = null;
        JSONObject resposeTxt = null;
        InputStream ins = null;
        ByteArrayOutputStream outStream = null;
        try {

            URL url = new URL(doUrl+"?fileName="+fileName);
            conn = (HttpURLConnection) url.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "text/html");
            conn.setRequestProperty("Cache-Control", "no-cache");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.connect();
            conn.setConnectTimeout(10000);
            out = conn.getOutputStream();

            File file = new File(pathName);
            in = new DataInputStream(new FileInputStream(file));

            int bytes = 0;
            byte[] buffer = new byte[1024];
            while ((bytes = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytes);
            }
            out.flush();

            // 返回流
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                ins = conn.getInputStream();
                outStream = new ByteArrayOutputStream();
                byte[] data = new byte[1024];
                int count = -1;
                while ((count = ins.read(data, 0, 1024)) != -1) {
                    outStream.write(data, 0, count);
                }
                data = null;
                resposeTxt = JSONObject.parseObject(new String(outStream
                        .toByteArray(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (ins != null) {
                ins.close();
            }
            if (outStream != null) {
                outStream.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    /**
     * 返回响应
     *
     * @throws Exception
     */
    public static void sendResponse(String responseString, HttpServletResponse response) throws Exception {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter pw = null;
        try {
            pw = response.getWriter();
            pw.write(responseString);
            pw.flush();
        } finally {
            IOUtils.closeQuietly(pw);
        }
    }
}
package com.example.file_sync.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesUtil {
	//根据Key读取路径
	public static String getPath(String fileName,String subFile){
		String value = "";
		InputStream in = null;
		try {
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream(
					fileName);
			Properties props = new Properties();
			props.load(in);
			value = props.getProperty("fileUpload."+subFile);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}
	
	// 根据key读取value
	public static String readValue(String fileName, String key) {
		String value = "";
		InputStream in = null;
		try {
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream(
					fileName);
			Properties props = new Properties();
			props.load(in);
			value = props.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	// 读取properties的全部信息
	public static void readProperties(String fileName) {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = PropertiesUtil.class.getClassLoader().getResourceAsStream(
					fileName);
			props.load(in);
			Enumeration<?> en = props.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String property = props.getProperty(key);
				System.out.println(key + "=" + property);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

源码下载

https://download.csdn.net/download/YL3126/20432039

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 在Java后端,使用HttpClient库进行POST请求提交文件,包括以下步骤: 1. 导入HttpClient库的依赖: ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ``` 2. 创建HttpClient对象: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 3. 创建HttpPost对象,并设置URL和请求体: ```java HttpPost httpPost = new HttpPost("http://example.com/upload"); File file = new File("path/to/file"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("file", new FileBody(file)); HttpEntity multipart = builder.build(); httpPost.setEntity(multipart); ``` 4. 执行POST请求并获取响应: ```java CloseableHttpResponse response = httpClient.execute(httpPost); ``` 在服务端接收文件的代码如下: 1. 创建Spring Boot Controller并监听指定URL: ```java @RestController public class FileUploadController { @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) { // 处理文件 // ... return "File uploaded successfully."; } } ``` 2. 接收到请求时,Spring框架会自动将文件绑定到`MultipartFile`对象,并调用`handleFileUpload`方法进行处理。 以上就是使用HttpClient库进行Java后端HTTP POST请求提交文件以及服务端接收文件的简单示例。 ### 回答2: 在Java后端中使用HttpClient进行post请求提交文件的步骤如下: 1. 引入相关的依赖,如Apache HttpClient: ```xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ``` 2. 创建HttpClient对象: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); ``` 3. 创建HttpPost请求对象: ```java HttpPost httpPost = new HttpPost(url); ``` 其中,`url`为服务端接收文件的接口地址。 4. 创建File实例或通过其他方式获取要上传的文件: ```java File file = new File("path/to/file"); FileBody fileBody = new FileBody(file); ``` 5. 创建HttpEntity,并将文件对象添加到其中: ```java MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addPart("file", fileBody); HttpEntity httpEntity = builder.build(); ``` 6. 设置请求的Entity为刚创建的HttpEntity: ```java httpPost.setEntity(httpEntity); ``` 7. 发送post请求并获取响应: ```java CloseableHttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); ``` 以上是使用HttpClient在Java后端进行post请求提交文件的基本步骤。 在服务端接收文件的代码示例如下(以Spring Boot为例): 1. 创建用于接收文件的接口: ```java @PostMapping("/upload") public void uploadFile(MultipartFile file) { // 处理文件的业务逻辑 } ``` 2. 在接口中使用MultipartFile对象接收文件,Spring Boot会自动将上传的文件封装成MultipartFile对象。 3. 可以在接口中进行一些与文件相关的处理,如保存文件、读取文件内容等。 以上就是使用Java后端的HttpClinet进行文件提交以及在服务端接收文件的简单介绍。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的小蜗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值