一、编辑application.properties 文件
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb
设置图片上传大小
二、创建 UploadController
package com.springboot.controller;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.*;
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.MultipartFile;
@Controller
public class UploadController {
// 因为uploadPage.jsp 在WEB-INF下,不能直接从浏览器访问,所以要在这里加一个uploadPage跳转,这样就可以通过
@RequestMapping("/uploadPage")
public String uploadPage() {
return “uploadPage”; //过度跳转页
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)// 等价于@PostMapping("/upload")
public String uplaod(HttpServletRequest req, @RequestParam("file") MultipartFile file, Model m) {
//1. 接受上传的文件 @RequestParam("file") MultipartFile file
try {
//2.根据时间戳创建新的文件名,这样即便是第二次上传相同名称的文件,也不会把第一次的文件覆盖了
String fileName = System.currentTimeMillis() + file.getOriginalFilename();
//3.通过req.getServletContext().getRealPath("") 获取当前项目的真实路径,然后拼接前面的文件名
String destFileName = req.getServletContext().getRealPath("") + "uploaded" + File.separator + fileName;
//4.第一次运行的时候,这个文件所在的目录往往是不存在的,这里需要创建一下目录(创建到了webapp下uploaded文件夹下)
File destFile = new File(destFileName);
destFile.getParentFile().mkdirs();
//5.把浏览器上传的文件复制到希望的位置
file.transferTo(destFile);
//6.把文件名放在model里,以便后续显示用
m.addAttribute("fileName", fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "上传失败," + e.getMessage();
}
return "showImg";
}
}
关键代码说明:
(1)@RequestParam(“file”)
接收上传参数,这里的 参数"file"是指前端上传文件名 ——指的是 uploadPage.jsp选择图片:
neme=“file”
(2)file.transferTo(destFile)
把上传的文件file 复制到 descFile下,并且命名为 fileName
三、创建上传页面 uploadPage.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
四、创建展示上传图片页面 showImg.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>