一、案例  

  1.1  配置application.properties

#主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev
server.port=8888 logging.config=classpath:log4j2-dev.xml
spring.mvc.view.prefix: /WEB-INF/templates/
spring.mvc.view.suffix: .jsp
  • spring.http.multipart.enabled=true #默认支持文件上传.
  • spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘.
  • spring.http.multipart.location= # 上传文件的临时目录
  • spring.http.multipart.max-file-size=1Mb # 最大支持文件大小
  • spring.http.multipart.max-request-size=10Mb # 最大支持请求大小

  1.2  编写IndexController

  • 该控制器只用于处理 “/” 和“/index”请求,使之跳转到index.jsp页面
package com.shyroke.controller;

import java.util.ArrayList;
import java.util.List; 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.servlet.ModelAndView; @Controller
@RequestMapping(value = "/")
public class IndexController { @RequestMapping(value="index")
public String index() {
return "index";
} @RequestMapping()
public String index2() {
return "index";
}
}

  1.3  编写index.jsp

<body>
<form method="POST" enctype="multipart/form-data" action="/file/upload">
文件:<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
</body>

  1.4  编写FileController

package com.shyroke.controller;

import java.io.File;
import java.io.IOException; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; @Controller
@RequestMapping(value = "/file")
public class FileController { private static final Logger logger = LoggerFactory.getLogger(FileController.class); @RequestMapping(value = "upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "文件为空";
} // 获取文件名
String fileName = file.getOriginalFilename();
logger.info("上传的文件名为:" + fileName); // 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
logger.info("上传的后缀名为:" + suffixName); // 文件上传路径
String filePath = "E://"; // 解决中文问题,liunx下中文路径,图片显示问题
// fileName = UUID.randomUUID() + suffixName; File dest = new File(filePath + fileName); // 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
} try {
file.transferTo(dest);
return "上传成功";
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败";
} }

  1.5  结果

最新文章

  1. Xamarin.ios——First APP
  2. 《GK101任意波发生器》升级固件发布(版本:1.0.2build198)
  3. log4net(c#) 配置及使用
  4. css(html)背景图优化合并
  5. C#中抽象类和接口的区别3
  6. Java 英文面试题
  7. Git 常用命令整理(转)
  8. 1. github配置
  9. redis缓存服务器集群搭建
  10. Redhat5_linux 系统环境下 oracl11g的安装教程图解
  11. PAT 1008 数组元素循环右移问题 (20)(代码)
  12. centos6.4安装GitLab
  13. Qt5布局管理(二)——QDockWidget停靠窗口类
  14. 好久没有写过SQL了,今天写了一句select in留存
  15. 系统管理命令之who
  16. EasyUI tree 优化--点击文字折叠展开效果
  17. consul 小結
  18. dpdk CUSE
  19. ASP.NET MVC4 新手入门教程之六 ---6.编辑视图与编辑方法
  20. jmeter 线程组之间的参数传递

热门文章

  1. Flutter移动电商实战 --(43)详细页_补充首页跳转到详细页
  2. 【Oracle/Java】批量删除16张十万数据的表 单线程耗时45秒 多线程耗时38秒
  3. Android插件化技术——原理篇
  4. ASP如何将table导出EXCEL表格
  5. 2013年各大小IT公司待遇,绝对真实,一线数据!(初版)
  6. GPS nmealib学习 问题
  7. osg fbx 模型结构操作
  8. Qt使用boost库
  9. 根据DELTA自动生成SQL语句
  10. CSS3 @font-face详细用法