Java 实现实时监听文件夹是否有新图片增加并下载 求代码实例
8条回答 默认 最新
关注 主要实现方式:
(1)利用Timer的定时循环执行代码的功能;(2)利用WatchService实时监听文件夹是否有新文件增加,通过阻塞式IO流实现文件上传服务器。
private static String path = "E:\\Kankan"; public static void getFile() throws FileNotFoundException, IOException{ final Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { WatchKey key; try { WatchService watchService = FileSystems.getDefault().newWatchService(); Paths.get(path).register(watchService, StandardWatchEventKinds.ENTRY_CREATE); while (true) { File file = new File(path);//path为监听文件夹 File[] files = file.listFiles(); System.out.println("等待图片加载!"); key = watchService.take();//没有文件增加时,阻塞在这里 for (WatchEvent<?> event : key.pollEvents()) { String fileName = path+"\\"+event.context(); System.out.println("增加文件的文件夹路径"+fileName); File file1 = files[files.length-1];//获取最新文件 System.out.println(file1.getName());//根据后缀判断 url = uploadFile(file1,uploadAddres);//上传服务器 }if (!key.reset()) { break; //中断循环 } } }catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, 2000 , 3000);//第一个数字2000表示,2000ms以后开启定时器,第二个数字3000,表示3000ms后运行一次run }
需要导入的jar包
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.file.FileSystems;//阻塞式IO流jar包 import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask;
文件上传服务器代码:
/** * 将file文件上传到指定dir文件夹中 * @param file:待上传文件 * @param dir:指定路径 * @throws FileNotFoundException * @throws IOException */ final static String uploadAddres = System.getProperty("catalina.home")+"\\webapps\\TiaoZhanB\\uploadFile"; public static String uploadFile(File file , String dir) throws FileNotFoundException, IOException { String imgURL = null; try { InputStream in = new FileInputStream(file); System.out.println("服务器路径:" + dir); // 获取文件名称 String fileName = file.getName(); // 路径和文件名丢进file对象里 File uploadFile = new File(dir, fileName); // 输出流 OutputStream out = new FileOutputStream(uploadFile); // 设置文件大小1MB byte[] buffer = new byte[1024 * 1024]; int length; // 用循环从流中读取文件的大小 while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } // 设置图片标题和全路径 imgURL = "uploadFile/" + fileName; System.out.println("绝对路径为"+imgURL); // 关闭输入流输出流,释放内存 in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } return imgURL; }
参考:https://blog.csdn.net/guanmao4322/article/details/82730404
解决 无用评论 打赏 举报
悬赏问题
- ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信
- ¥15 python怎么在已有视频文件后添加新帧
- ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
- ¥15 fluent里模拟降膜反应的UDF编写
- ¥15 MYSQL 多表拼接link
- ¥15 关于某款2.13寸墨水屏的问题
- ¥15 obsidian的中文层级自动编号
- ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
- ¥15 神经网络模型一直不能上GPU
- ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!