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

    Main implementation mode:
    (1) utilizeTimer The function of executing the code in the timing cycle of;

    (2) utilizeWatchService Real time monitor whether new files are added to the folder, Through blockingIO 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 picture to load!"); key =
watchService.take();// When no files are added, It's blocked here for (WatchEvent<?> event :
key.pollEvents()) { String fileName = path+"\\"+event.context();
System.out.println(" Add folder path for files"+fileName); File file1 =
files[files.length-1];// Get the latest file System.out.println(file1.getName());// Judging by suffix url
= uploadFile(file1,uploadAddres);// Upload server }if (!key.reset()) { break; // Interrupt cycle } }
}catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace();
} } }, 2000 , 3000);// First number2000 Express,2000ms Turn on timer later, Second number3000, Express3000ms Run once afterrun }
Need to importjar package
import; import; import; import; import; import; import;
import java.nio.file.FileSystems;// Blocking typeIO flowjar 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:
/** * takefile File upload to specifieddir Folder * @param file: Files to be uploaded * @param dir: Specified 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(); // Drop path and filenamefile In object File uploadFile = new
File(dir, fileName); // Output stream OutputStream out = new
FileOutputStream(uploadFile); // set file size1MB byte[] buffer = new byte[1024 *
1024]; int length; // Read the size of the file from the stream in 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 I / O stream, Free memory in.close();
out.close(); } catch (Exception e) { e.printStackTrace(); } return imgURL; }
notes: If inMyEclipse Use the above code in(eclipse No mistake), Need to be usedJava
 jre Switch to your own installationjre, The reason lies inmyeclipse Self containedjre No blockingio Streamingjar package, It's a mistake.

The switching method is as follows:

(1) First right click on the project name, Find and clickBuild Path, Post Clickconfigure build path, The following interface appears:

(2) Click in the figure aboveJRE System Library-------------> clickEdit

(3) The following interface appears, clickinstall JREs

(4) The following interface appears, clickEdit, Find your own installationjava JDK, Click OK

switchJRE after, The error will disappear.