属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts2(一般老项目使用)等。
消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。MD5由美国密码学家罗纳德·李维斯特(Ronald Linn Rivest)设计,于1992年公开,用以取代MD4算法。
pom.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelVersion > 4.0.0</modelVersion > <packaging > war</packaging > <name > FileUpload</name > <groupId > com</groupId > <artifactId > FileUpload</artifactId > <version > 1.0-SNAPSHOT</version > <dependencies > <dependency > <groupId > org.aspectj</groupId > <artifactId > aspectjweaver</artifactId > <version > 1.8.12</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 3.2.13.RELEASE</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > javax.servlet-api</artifactId > <version > 3.0.1</version > <scope > provided</scope > </dependency > <dependency > <groupId > commons-io</groupId > <artifactId > commons-io</artifactId > <version > 2.4</version > </dependency > <dependency > <groupId > commons-fileupload</groupId > <artifactId > commons-fileupload</artifactId > <version > 1.3.1</version > </dependency > </dependencies > </project >
springMVC-servlet.xml 配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc ="http://www.springframework.org/schema/mvc" xmlns:context ="http://www.springframework.org/schema/context" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire ="byName" > <context:component-scan base-package ="com" /> <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/" /> <property name ="suffix" value =".jsp" /> </bean > <bean class ="org.springframework.web.multipart.commons.CommonsMultipartResolver" name ="multipartResolver" > <property name ="maxUploadSize" value ="7340032" /> <property name ="defaultEncoding" value ="UTF-8" /> </bean > <mvc:annotation-driven /> <mvc:resources mapping ="/**" location ="/WEB-INF/" /> </beans >
web.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?xml version="1.0" encoding="UTF-8" ?> <web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > <servlet > <servlet-name > DispatcherServlet</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:springMVC-servlet.xml</param-value > </init-param > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > DispatcherServlet</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > </web-app >
FileUtil 工具类 创建文件夹 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.util;import java.io.File;public class FileUtil { public static void createDir (String path) { File file = new File (path); if (!file.exists()) file.mkdirs(); } }
MD5Util 工具类 加密 防止中文名图片可以上传不能显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.util;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Util { public static String EncoderByMd5 (String str) { MessageDigest md5 = null ; try { md5 = MessageDigest.getInstance("MD5" ); md5.update(str.getBytes("UTF-8" )); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte b[] = md5.digest(); int i; StringBuffer buf = new StringBuffer (); for (int offset = 0 ; offset < b.length; offset++) { i = b[offset]; if (i < 0 ) i += 256 ; if (i < 16 ) buf.append("0" ); buf.append(Integer.toHexString(i)); } System.out.println(buf.toString()); return buf.toString(); } public static void main (String[] args) { System.out.println(MD5Util.EncoderByMd5("Simon" )); } }
ImgController servlet层 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 package com.controller;import com.util.FileUtil;import com.util.MD5Util;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpSession;import java.io.File;import java.io.IOException;@Controller public class ImgController { @RequestMapping("/uploadImg") public String uploadImg (MultipartFile imgFile, HttpSession session, Model model) { String filename = imgFile.getOriginalFilename(); System.out.println(filename); String substring = filename.substring(0 , filename.lastIndexOf(".jpg" )); System.out.println(substring); String s = new StringBuffer (MD5Util.EncoderByMd5(substring)).append(".jpg" ).toString(); String path = session.getServletContext().getRealPath("/WEB-INF/img" ); System.out.println(path); FileUtil.createDir(path); File outFile = new File (path, s); model.addAttribute("img" , s); try { imgFile.transferTo(outFile); } catch (IOException e) { e.printStackTrace(); } return "index" ; } }
index.jsp 页面 1 2 3 4 5 6 7 8 9 10 11 12 13 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html > <head > <title > Title</title > </head > <body > <form action ="uploadImg" method ="post" enctype ="multipart/form-data" > <input type ="file" name ="imgFile" /> <input type ="submit" value ="上传" /> </form > <img src ="/img/${img}" height ="700" > </body > </html >