springboot上传文件及文件上传限制大小异常捕获
生活随笔
收集整理的這篇文章主要介紹了
springboot上传文件及文件上传限制大小异常捕获
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一個(gè)簡單的頁面上傳文件,上傳的接口是/upload,請求方法是post,enctype(表單類型)是multipart/form-data?
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> <form action="/upload" method="post" enctype="multipart/form-data"><input type="file" name="uploadFile" value="請選擇文件" multiple><input type="submit" value="上傳"> </form> </body> </html>在項(xiàng)目中新建一個(gè)webapp/uploadFile的文件夾?
接下來編寫文件上傳的controller接口:?
import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile;@Controller public class controller {SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");@RequestMapping("/test")public String test() {return "upload";}@PostMapping("/upload")@ResponseBodypublic String upload(MultipartFile uploadFile, HttpServletRequest req) {//獲取uploadFile文件在該項(xiàng)目的絕對路徑String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");System.out.println("realPath:" + realPath);String format = sdf.format(new Date());File folder = new File(realPath + format);System.out.println("folder:" + folder);//若果不存在該文件夾,則新建if (!folder.isDirectory()) {folder.mkdirs();}String oldName = uploadFile.getOriginalFilename();String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());try {uploadFile.transferTo(new File(folder, newName));System.out.println("保存文件成功");String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/"+ format + newName;return filePath;} catch (IOException e) {// e.printStackTrace();}return "上傳失敗!";} }?
全局異常之上傳文件超過了限制大小
@ControllerAdvice是最常見的全局異常處理。
@ControllerAdvice結(jié)合@ExceptionHandler客戶以定義全局異常捕獲機(jī)制!
import java.io.IOException;import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.ModelAndView;@ControllerAdvice public class CustomExceptionHandler {@ExceptionHandler(MaxUploadSizeExceededException.class)public ModelAndView uploadException(MaxUploadSizeExceededException e) throws IOException {ModelAndView mv = new ModelAndView();mv.addObject("msg", "最大上傳文件為1M,上傳文件大小超出限制!");mv.setViewName("error");return mv;}@ExceptionHandler(Exception.class)public void myexce(Exception e) {System.out.println("myexce>>>"+e.getMessage());} }新建一個(gè)報(bào)錯(cuò)頁面:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <div th:text="${msg}"></div></body> </html>?
總結(jié)
以上是生活随笔為你收集整理的springboot上传文件及文件上传限制大小异常捕获的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Marvell 创始人斥资 20 亿美元
- 下一篇: Ajax请求Session超时解决