注意:web的版本必须大于2.5
-
-
- <multipart-config>
-
- <max-file-size>5242880max-file-size>
- <max-request-size>20971520max-request-size>
- <file-size-threshold>0file-size-threshold>
- multipart-config>

-
- <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>
- <form action="${request.contextPath}/brand/add" method="post" enctype="multipart/form-data">
- Logo:<input type="file" name="logoFile"><br>
- <input type="submit" value="提交">
- form>
- import com.xxx.constant.WegoConst;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.Part;
- import java.io.IOException;
- import java.util.UUID;
-
- /**
- * @author JngKang
- * @date 2022-05-16 09:27
- */
- @Controller
- @MultipartConfig
- @RequestMapping("brand")
- public class BrandController {
-
- @PostMapping("add")
- public void add(HttpServletRequest request) throws ServletException, IOException {
- final Part part = request.getPart("logoFile");
- // 获取请求头
- final String header = part.getHeader("content-disposition");
- // 获取文件名
- final String fileName = getFileName(header);
- final String suffix = fileName.substring(fileName.lastIndexOf("."));
- final String newFileName = UUID.randomUUID() + suffix;
- final String filePath = WegoConst.FILEPATH + newFileName;
- try {
- part.write(filePath);
- System.out.println(fileUrl);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
-
- /**
- * 根据请求头解析出文件名
- *
- * @param header 请求头
- * @return 文件名
- */
- public String getFileName(String header) {
- String[] temp = header.split(";")[2].split("=");
- //获取文件名,兼容各种浏览器的写法
- String fileName = temp[1].substring(temp[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
- return fileName;
- }
- }
- import com.xxx.constant.WegoConst;
- import com.xxx.domain.Brand;
- import com.xxx.service.BrandService;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- import javax.annotation.Resource;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.MultipartConfig;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.Part;
- import java.io.IOException;
- import java.util.UUID;
-
- /**
- * @author JngKang
- * @date 2022-05-16 09:27
- */
- @Controller
- @MultipartConfig
- @RequestMapping("brand")
- public class BrandController {
-
- @Resource
- private BrandService brandService;
-
- @PostMapping("add")
- public String add(HttpServletRequest request, Brand brand) throws ServletException, IOException {
- final Part part = request.getPart("logoFile");
- // 获取请求头
- final String header = part.getHeader("content-disposition");
- // 获取文件名
- final String fileName = getFileName(header);
- final String suffix = fileName.substring(fileName.lastIndexOf("."));
- final String newFileName = UUID.randomUUID() + suffix;
- final String filePath = WegoConst.FILEPATH + newFileName;
- final String fileUrl = "http://localhost:8082/" + newFileName;
- try {
- part.write(filePath);
- System.out.println(fileUrl);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- brand.setLogo(fileUrl);
- int insert = brandService.insert(brand);
- if (insert == 1) {
- return "redirect:/brand/list";
- }
- return "redirect:/brand/openAddPage";
- }
-
- /**
- * 根据请求头解析出文件名
- *
- * @param header 请求头
- * @return 文件名
- */
- public String getFileName(String header) {
- String[] temp = header.split(";")[2].split("=");
- //获取文件名,兼容各种浏览器的写法
- String fileName = temp[1].substring(temp[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
- return fileName;
- }
- }