MD5加密 springMVC文件上传显示示例

Spring MVC

属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts2(一般老项目使用)等。

MD5

消息摘要算法(英语: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">
<!--1MB = 1024KB 1KB = 1024B 单位字节/B-->
<property name="maxUploadSize" value="7340032"/><!--7MB-->
<!--默认编码格式-->
<property name="defaultEncoding" value="UTF-8"/>
</bean>

<!--使 @Controller 注解生效-->
<mvc:annotation-driven/>

<!--配置静态文件资源的访问-->
<!--否则 WEB-INF 下的 css 等资源文件访问不到-->
<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">
<!--加载springMVC-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置加载的springMVC-servlet.xml 默认为 servlet-name 的值 XXX-servlet.xml-->
<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;

/**
* EncoderByMd5
*/
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>

示例下载