springmvc配置文件添加配置,此用于方法二才需要配置
<!-- 多部分文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="104857600" />
<property name="maxInMemorySize" value="4096" />
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
前端界面
需要在form添加enctype=”multipart/form-data”
<!DOCTYPE html>
<html>
<head>
<#include "/common/public.ftlh">
<#assign ctx=request.getContextPath()>
</head>
<body class="easyui-layout">
<form class="appForm" action="${ctx}/uploadFile/method2" method="post" enctype="multipart/form-data">
<div class="easyui-panel appContainer" data-options="border:false,fit:true">
<table class ="appTable" style="width:100%;">
<tr>
<td class="label" style="width:120px;">文件名:</td>
<td>
<input name="file" class="easyui-filebox" style="width:25%;"/>
</td>
</tr>
<tr>
<td class="label" style="width:120px;">提交:</td>
<td>
<input name="submit" type="submit" value="提交" class="easyui-linkbutton" style="width:25%;"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台controller类
package com.lancy.action;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
//方法一和二都是可行的方法。
@Controller
@RequestMapping(value = "/uploadFile")
public class uploadFileAction {
Logger log = Logger.getLogger(uploadFileAction.class);
@Value("#{configProperties['testFilePath']}")
private String filePath;//读取配置,文件保存路径
//这里的@RequestParam("file")的名称需要和前端页面的命名一致
//上传图片显示图片
@RequestMapping(value = "/method1", method = RequestMethod.POST)
public ModelAndView method1(@RequestParam("file")CommonsMultipartFile file) throws IOException{
//获取本地物理地址
String physicalPath = request.getSession().getServletContext().getRealPath("upload") + File.separator + file.getOriginalFilename();
//相对工程的路径
String path = "upload" + File.separator + file.getOriginalFilename();
File newFile = new File(physicalPath);
file.transferTo(newFile);
ModelAndView view = new ModelAndView("/res/success");//定位到上传成功页面
view.addObject("fileUrl",path);
return view;
}
//上传文件
@RequestMapping(value="/method2", method = RequestMethod.POST)
public ModelAndView method2(HttpServletRequest request) throws IOException {
long startTime = System.currentTimeMillis();
CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if(resolver.isMultipart(request)){
MultipartHttpServletRequest servletRequest = (MultipartHttpServletRequest)request;
Iterator iterator = servletRequest.getFileNames();
while(iterator.hasNext()){
MultipartFile file = servletRequest.getFile(iterator.next().toString());
if(file != null){
String path = filePath + file.getOriginalFilename();
file.transferTo(new File(path));
}
}
}
long endTime = System.currentTimeMillis();
System.out.println("运行时间:" + String.valueOf(endTime - startTime) + "ms");
ModelAndView view = new ModelAndView("/res/success");
return view;
}
}
显示页面
<!doctype html>
<html class="no-js">
<head>
<title>500 ERROR</title>
<#include "/common/public.ftlh">
<#assign ctx=request.getContextPath()>
</head>
<body>
<img alt="" src="${ctx}/${(fileUrl)! }" />
</body>
</html>
可参考相关博客
http://blog.csdn.net/cheung1021/article/details/7084673/
http://www.cnblogs.com/fjsnail/p/3491033.html
http://blog.csdn.net/luckey_zh/article/details/46867957