Loading...
墨滴

Tony1029

2021/05/28  阅读:31  主题:橙心

看门狗监控文件夹变化,把新文件上传

java 看门狗监控文件夹变化,把新文件上传

  final Timer timer = new Timer();
            String finalBanci = banci;
            timer.schedule(new TimerTask() {
                public void run() {
                    WatchKey key;
                    Date date=new Date();
                    SimpleDateFormat format=new  SimpleDateFormat("yyyyMMdd");
                    System.out.println("班次:"+ finalBanci);
                    path="D:\\Images\\"+ finalBanci +format.format(date)+"\\合格";
                    try {
                        WatchService watchService = FileSystems.getDefault().newWatchService();
                        Paths.get(path).register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
                        while (true) {

                            System.out.println("监听文件夹!");
                            key = watchService.take();//没有文件增加时,阻塞在这里
                            for (WatchEvent<?> event : key.pollEvents()) {
                                String fileName = path+"\\"+event.context();
                                System.out.println("新文件来了---"+fileName);
                                File file = new File(fileName);//path为监听文件夹
                                System.out.println(file.getName());//最新文件
                                Thread.sleep(1000);
                                InputStream in = new FileInputStream(file);
                                String fname=file.getName();
                                String tyname=fname.substring(0,fname.lastIndexOf("."));
                                String tynames=  soapController.soapApi(tyname).replace("锛�","+");
                                tyname=tyname+"["+tynames+"]"+".bmp";
                                minioutils.upload(in,tyname,file);
                            }if (!key.reset()) {
                                break; //中断循环
                            }
                        }
                    }catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }, 2000 , 5000);//第一个数字2000表示,2000ms以后开启定时器,第二个数字3000,表示3000ms后运行一次run

文件发现上传,minio文件上传

package com.mfm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;
import io.minio.policy.PolicyType; 
import io.minio.MinioClient;

@Service
public class MinioUtils {
    private static String url = "http://172.16.22.26:9000";  //minio服务的IP端口
// private static String url = "http://127.0.0.1:9000"
    private static String accessKey = "IN2XFOG9DKBOBA97AB9E";  
    private static String secretKey = "3AMu0NTlALHzRkZbcCdgoDCfP2SnZlgqiA+Zhvzm";
  
    
    
//    private static String accessKey = "J8HILY8GNQEI881N9D17";  
//    private static String secretKey = "wvLaY+5cd23G5VpmiH8meo0nxVCNa3+ZR4lFp70M";
    String BUCKETNAME="upload";
    
    File lastImg;
    
    
    /**
     * 文件上传
     * @param file
     * @return
     */
    public String upload(MultipartFile file) throws Exception
    {
     String s=null;
         try {
             MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
             //存入bucket不存在则创建,并设置为只读
             if (!minioClient.bucketExists(BUCKETNAME)) {
                 minioClient.makeBucket(BUCKETNAME);
                 minioClient.setBucketPolicy(BUCKETNAME, "*.*",PolicyType.READ_ONLY);
             }
             String filename = System.currentTimeMillis()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
             // 文件存储的目录结构
             String objectName =  filename;
             // 存储文件
             minioClient.putObject(BUCKETNAME, objectName, file.getInputStream(), file.getContentType());
             s=url + "/" + BUCKETNAME + "/" + objectName;
         } catch (Exception e) {
         }
         return s;
    }
    /**
     * 文件上传
     * @param file
     * @return
     */
    public String upload(InputStream in,String finame,File f) throws Exception
    {
     System.out.println("文件上传--开始");
     String s=null;
         try {
          
             MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
             //存入bucket不存在则创建,并设置为只读
             if (!minioClient.bucketExists(BUCKETNAME)) {
                 minioClient.makeBucket(BUCKETNAME);
                 minioClient.setBucketPolicy(BUCKETNAME, "*.*",PolicyType.READ_ONLY);
             }
             // 文件存储的目录结构
             String objectName =  finame;
             // 存储文件
             minioClient.putObject(BUCKETNAME, objectName, in,"image/jpeg");
             s=url + "/" + BUCKETNAME + "/" + objectName;
             in.close();
             System.out.println("文件上传--结束");
             
         } catch (Exception e) {
          System.out.println(e.getMessage());
         }
         return s;
    }
    
    //下载minio服务的文件
     @GetMapping("download")
     public String download(HttpServletResponse response){
         try {
           MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
           InputStream fileInputStream = minioClient.getObject("file""test.jpg");
           response.setHeader("Content-Disposition""attachment;filename=" + "test.jpg");
           response.setContentType("application/force-download");
           response.setCharacterEncoding("UTF-8");
           IOUtils.copy(fileInputStream,response.getOutputStream());
           return "下载完成";
         }catch (Exception e){
             return "下载失败";
         }
     }
     
     
     public String upload1(InputStream in,String finame) throws Exception
     {
      System.out.println("文件上传--开始");
      String s=null;
          try {
           
              MinioClient minioClient = new MinioClient(url, accessKey, secretKey);
              //存入bucket不存在则创建,并设置为只读
              if (!minioClient.bucketExists("buhege")) {
                  minioClient.makeBucket("buhege");
                  minioClient.setBucketPolicy("buhege""*.*",PolicyType.READ_ONLY);
              }
              // 文件存储的目录结构
              String objectName =  finame;
              // 存储文件
              minioClient.putObject("buhege", objectName, in,"image/jpeg");
              s=url + "/" + "buhege" + "/" + objectName;
              in.close();
              System.out.println("文件上传--结束");
          } catch (Exception e) {
           System.out.println(e.getMessage());
          }
          return s;
     }
    
}

Tony1029

2021/05/28  阅读:31  主题:橙心

作者介绍

Tony1029