index页面
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>图片上传</title>
</head>
<body>
<div align="center">
<h2>--图片上传--</h2>
<form action="thumbnail" method="post" enctype="multipart/form-data" id="upload-form">
<h2>请选择上传的图片</h2>
<div>
<input type="file" name="image" id="image" />
<input type="submit" value="上传" />
</div>
</form>
</div>
</body>
</html>
Controller层
package com.imooc.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.imooc.service.ThumbnailService;
import com.imooc.service.UploadService;
@Controller
public class ThumbnailController {
@Autowired
private UploadService uploadService;
@Autowired
private ThumbnailService thumbnailService;
@RequestMapping(value="/thumbnail",method=RequestMethod.POST)
public ModelAndView thumbnail(@RequestParam("image") CommonsMultipartFile file,HttpSession session) throws Exception{
//获得上传在服务器的绝对路径信息
String uploadPath = "/images"; //相对路径
String realUploadPath = session.getServletContext().getRealPath(uploadPath);
String imageUrl = uploadService.uploadImage(file, uploadPath, realUploadPath);
String thumImageUrl = thumbnailService.uploadImage(file, uploadPath, realUploadPath);
ModelAndView ret = new ModelAndView();
ret.addObject("imageUrl", imageUrl);
ret.addObject("thumImageUrl", thumImageUrl);
ret.setViewName("thumbnail");
return ret;
}
}
service层
package com.imooc.service;
import java.io.IOException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import net.coobird.thumbnailator.Thumbnails;
@Service
public class ThumbnailService {
public static final int WIDTH=100;
public static final int HEIGHT=100;
public String uploadImage(CommonsMultipartFile file,String uploadPath,String realUploadPath){
//缩略图在服务器上保存的位置
String des = realUploadPath+"/thum_"+file.getOriginalFilename();
try {
//生成缩略图
Thumbnails.of(file.getInputStream()).size(WIDTH, HEIGHT).toFile(des);;
} catch (IOException e) {
e.printStackTrace();
}
return uploadPath+"/thum_"+file.getOriginalFilename();
}
}
package com.imooc.service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Service
public class UploadService {
public String uploadImage(CommonsMultipartFile file,String uploadPath,String realUploadPath){
InputStream is = null;
OutputStream os = null;
try {
//获得上传文件的输入流信息
is = file.getInputStream();
//创建一个目标路径文件
String des = realUploadPath+"/"+file.getOriginalFilename();
//通过目标路径创建输出流
os = new FileOutputStream(des);
byte[] buffer = new byte[4096];
int len = 0;
while((len = is.read(buffer))>0){
os.write(buffer);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
return uploadPath+"/"+file.getOriginalFilename();
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>thumbnail</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<!-- 使用.html作为请求后缀 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-mvc.xml
spring-mvc.xml
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<!-- 自动注册组件 -->
<mvc:annotation-driven/>
<!-- 自动扫描 com.jikexueyuan 包下面的所有组件(使用了springmvc注解) -->
<context:component-scan base-package="com.imooc.*" />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="4096"></property>
</bean>
<!-- 访问静态资源 -->
<mvc:resources location="/" mapping="/**"/>
</beans>
展示页面
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>操作结果</title>
</head>
<body>
<h4>图片展示</h4>
<hr>
<table width="100%">
<tr>
<td width="50%" align="center"><img alt="原图" src="${pageContext.request.contextPath}${imageUrl}" width="500" /></td>
<td width="50%" align="center"><img alt="缩略图" src="${pageContext.request.contextPath}${thumImageUrl}" /></td>
</tr>
</table>
</body>
</html>