Java implements drag-and-drop upload and java drag-and-drop upload.
Java-based drag-and-drop upload
In project development, You need to develop the drag/drop upload function as required. OK!
First look:
Jsp upload front-end code:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html>
OK. After writing the front-end code, write the following code in the back-end:
<% @ Page import = "java. util. UUID "%> <% @ page import =" org. apache. commons. fileupload. fileItem "%> <% @ page import =" java. util. iterator "%> <% @ page import =" java. util. list "%> <% @ page import =" org. apache. commons. fileupload. servlet. servletFileUpload "%> <% @ page import =" org. apache. commons. fileupload. disk. diskFileItemFactory "%> <% @ page import =" org. apache. commons. fileupload. fileItemFactory "%> <% @ page import =" java. io. file "%> <% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <%/* 1. File Upload: */request. setCharacterEncoding ("UTF-8"); response. setCharacterEncoding ("UTF-8"); // obtain the file path String strPath = request. getRealPath ("/") + "/upload"; File file = new File (strPath); if (! File. exists () file. mkdirs (); FileItemFactory factory = new DiskFileItemFactory (); ServletFileUpload upload = new ServletFileUpload (factory); // obtain file information from the request object List items = upload. parseRequest (request); if (items! = Null) {for (int I = 0; I <items. size (); I ++) {Iterator iterator = items. iterator (); while (iterator. hasNext () {FileItem item = (FileItem) iterator. next (); if (item. isFormField () {continue;} else {String fileName = item. getName (); Long fileSize = item. getSize (); int pos = fileName. indexOf (". "); String ext = fileName. substring (pos, fileName. length (); fileName = UUID. randomUUID (). toString () + ext; File saveFile = new File (strPath, fileName); item. write (saveFile) ;}}}%>
OK. Now, the drag-and-drop upload function is complete. Of course, this is only a rough version. I have optimized the code to a plug-in and can use it directly !!
Source code download: Drag and Drop upload source code