本节课讲解SpringBoot的文件上传,使用MultipartFile file,MultipartFile 是File类的子类,源自SpringMVC,我们进入MultipartFile的源码查看他有很多api,等下会具体使用到,下面开始开发:
在static下面新建一个update.html页面,写一个表单提交
<!DOCTYPE html>
<html>
<head>
<title>uploadimg.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3"></meta>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="/js/test.js" type="text/javascript"></script>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/upload">
文件:<input type="file" name="head_img"/>
姓名:<input type="text" name="name"/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
注意:如果上传文件的话表单要加上enctype="multipart/form-data"属性。运行直接访问http://localhost:8080/upload.html
因为页面在static目录下,所以直接访问页面名就行了
下面新建一个返回给前端的结果类:
package com.qzsun.springbootdemo.entity;
import java.io.Serializable;
public class JsonData implements Serializable {
private static final long serialVersionUID = 1L;
//状态码,0表示成功,-1表示失败
private int code;
//结果
private Object data;
//错误描述
private String msg;
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setCode(int code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public JsonData(int code, Object data) {
super();
this.code = code;
this.data = data;
}
public JsonData(int code, String msg,Object data) {
super();
this.code = code;
this.msg = msg;
this.data = data;
}
}
继续写一个controller
package com.qzsun.springbootdemo.controller;
import com.qzsun.springbootdemo.entity.JsonData;
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;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
* @author sunqizheng
* @Title: UploadController
* @ProjectName springbootdemo
* @Description: TODO
* @date 2019/1/1813:34
*/
@Controller
public class UploadController {
private static final String filePath = "D:\\SpringBoot2.0\\SpringBootDemo4\\src\\main\\resources\\static\\images\\";
@RequestMapping(value = "upload")
@ResponseBody
public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) {
//file.isEmpty(); 判断图片是否为空
//file.getSize(); 图片大小进行判断
String name = request.getParameter("name");
System.out.println("用户名:"+name);
// 获取文件名
String fileName = file.getOriginalFilename();
System.out.println("上传的文件名为:" + fileName);
// 获取文件的后缀名,比如图片的jpeg,png
String suffixName = fileName.substring(fileName.lastIndexOf("."));
System.out.println("上传的后缀名为:" + suffixName);
// 文件上传后的路径
fileName = UUID.randomUUID() + suffixName;
System.out.println("转换后的名称:"+fileName);
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
return new JsonData(0, fileName);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new JsonData(-1, "fail to save ", null);
}
}
MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效),注意:在images目录后面要加上\\,不然会存在目录外面,不在images里面
启动运行并上传图片,发现返回成功
访问图片http://localhost:8080/images/853b8442-0772-410f-bb4b-2a3887ec1efc.jpg