Java implements drag-and-drop upload and java drag-and-drop upload.

Source: Internet
Author: User

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.