This paper mainly states a function of real-time monitoring whether there are files added in the folder , It can be used for the development of actual file upload function .

    Main implementation methods :
    (1) utilize Timer The function of timing loop execution code ;

    (2) utilize WatchService Whether there are new files added in the real-time monitoring folder , By blocking IO Stream implementation file upload server .

    The code is as follows :
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 For listening folder File[] files =
file.listFiles(); System.out.println(" Wait for the picture to load !"); key =
watchService.take();// When no files are added , It's stuck here for (WatchEvent<?> event :
key.pollEvents()) { String fileName = path+"\\"+event.context();
System.out.println(" Add file folder path "+fileName); File file1 =
files[files.length-1];// Get the latest files System.out.println(file1.getName());// Judging by suffix url
= uploadFile(file1,uploadAddres);// Upload server }if (!key.reset()) { break; // Interrupt loop } }
}catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace();
} } }, 2000 , 3000);// First number 2000 express ,2000ms Turn on the timer later , The second number 3000, express 3000ms Run once after run }
Need to import jar package
import; import; import; import; import; import; import;
import java.nio.file.FileSystems;// Blocking type IO flow jar package 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 upload server code :
/** * take file File upload to specified dir Folder * @param file: Files to be uploaded * @param dir: Specify the path * @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(" Server path :" + dir); // Get file name
String fileName = file.getName(); // Path and file name file In the object File uploadFile = new
File(dir, fileName); // Output stream OutputStream out = new
FileOutputStream(uploadFile); // set file size 1MB byte[] buffer = new byte[1024 *
1024]; int length; // Read the size of the file from the stream with a loop while ((length = > 0) {
out.write(buffer, 0, length); } // Set picture title and full path imgURL = "uploadFile/" +
fileName; System.out.println(" The absolute path is "+imgURL); // Close input stream output stream , Free memory in.close();
out.close(); } catch (Exception e) { e.printStackTrace(); } return imgURL; }
notes : If MyEclipse The above code is used in (eclipse It won't go wrong ), Need to be used Java
 jre Switch to self installed jre, The reason is that myeclipse Self contained jre There is no blocking io Flowing jar package , There will be a mistake .

The switching method is as follows :

(1) First, right-click on the project name , Find and click Build Path, Then click configure build path, The following interface appears :

(2) Click in the picture above JRE System Library-------------> click Edit

(3) The following interface appears , click install JREs

(4) The following interface appears , click Edit, Find your own installed java JDK, Click OK

switch JRE after , The error will disappear .