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