spring boot 2.x 上传文件 (图片)
import cn.hutool.core.lang.UUID;
import com.eagle.common.dto.ControllerResult;
import com.eagle.common.util.ErrorCode;
import com.eagle.monitor.reportForms.surplus.service.ConstructionStatisticsService;
import com.eagle.monitor.reportForms.surplus.vo.TheKeyTaskDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
/**
* @Author LiGuangLong
* @Date 2021-10-11 20:12
* @Version 1.0
**/
@RestController
@RequestMapping("surplusadd")
@Api(tags = "surplus文件上传---")
public class AddController {
@Value("${filePath.path}")
private String filePath;
@Resource
private ConstructionStatisticsService constructionStatisticsService;
@ApiOperation("大屏附件上传接口")
@PostMapping(value = "/fileUpload")
public String fileUpload(@RequestParam(value = "file") MultipartFile file) {
if (file.isEmpty()) {
return "file文件为空 上传错误";
}
// 文件名
String fileName = file.getOriginalFilename();
// 后缀名
assert fileName != null;
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 新文件名
fileName = UUID.randomUUID() + suffixName;
File dest = new File(filePath + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
return filePath + fileName;
}
}
配置本地映射路径

配置类
package com.eagle.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* springboot内置tomcat虚拟路径配置
* @author TYL
*/
@Configuration
public class FileConfig extends WebMvcConfigurationSupport {
@Autowired
private GlobalConfig globalConfig;
@Value("${filePath.path}")
private String filepath;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler(globalConfig.getFileStaticAccessPath() + "**")
.addResourceLocations("file:/" + globalConfig.getFileUploadFolder());
String os = System.getProperty("os.name");
//如果是Windows系统
if (os.toLowerCase().startsWith("win")) {
registry.addResourceHandler("jgdpfile/**").addResourceLocations("file:" + filepath);
} else { //linux 和mac
registry.addResourceHandler("jgdpfile/**").addResourceLocations("file:" + filepath);
}
// 配置swagger
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
jsp前端 当然使用elmentui 更简单
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
<label>上传图片</label>
<input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
<p>图片:</p>
<img src="${filename }"/>
</body>
</html>
访问http://127.0.0.1:28081/jgdpfile/file_name
切记 类只能被继承一次 多个不起作用

浙公网安备 33010602011771号