Java implements simulated QQ Space Image Upload and javaqq Space Image Upload

Source: Internet
Author: User
Tags saveform

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:


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.