Java implements simulated QQ Space Image Upload and javaqq Space Image Upload
Java implements simulated QQ Space Image Upload
First, check the effect:
First, write our upload jsp code, as follows:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Then write our background code. Here I will simply use Servlet
Package com. wonders. upload; import java. io. file; import java. io. IOException; import java. util. iterator; import java. util. list; import java. util. UUID; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. fileupload. fileItem; import org. apache. commons. fileupload. fileIte MFactory; import org. apache. commons. fileupload. fileUploadException; import org. apache. commons. fileupload. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileUpload; public class UploadQQServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {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 the file information List items from the request object; try {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; request. getSession (). setAttribute ("fileName", fileName); File saveFile = new File (strPath, fileName); item. write (saveFile); response. sendRedirect ("UploadQQ. jsp ") ;}}}} catch (Exception e) {e. printStackTrace ();}}}
OK, this code is used together!
Download source code: http://download.csdn.net/detail/u010506940/8401307
Java implements simulated QQ Space Image Upload
First, check the effect: