/**
* @author :RukiHuang
* @description:文件上传配置类
* 配置MaxFileSize等属性
* @date :2022/9/2 9:45
*/
@Configuration
public class UploadConfig {
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个数据大小
factory.setMaxFileSize(DataSize.ofMegabytes(200));
//总上传文件的大小
factory.setMaxRequestSize(DataSize.ofGigabytes(10));
return factory.createMultipartConfig();
}
}
application.properties
配置文件中指定#文件上传
# 文件大小设置已在UploadConfig中配置,也可在配置文件中配置
#单个文件大小
spring.servlet.multipart.max-file-size=200MB
#总文件大小(允许存储文件的文件夹大小)
spring.servlet.multipart.max-request-size=10240MB
#文件上传的目标路径
file.upload.path=G:\\temp\\
#文件上传允许的类型
file.upload.allowType[0]=application/pdf
file.upload.allowType[1]=video/mp4
/**
* @author :RukiHuang
* @description:文件上传
* 上传路径
* 文件格式
* @date :2022/9/2 10:05
*/
@Component
@ConfigurationProperties(prefix = "file.upload")
public class UploadProperties {
private String path;
private List<String> allowTypeList;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List<String> getAllowType() {
return allowTypeList;
}
public void setAllowType(List<String> allowTypeList) {
this.allowTypeList = allowTypeList;
}
}
/**
* @author :RukiHuang
* @description:唯一ID生成器
* @date :2022/9/2 10:07
*/
public class IDUtils {
public static String generateUniqueId() {
return UUID.randomUUID().toString() + System.currentTimeMillis();
}
}
/**
* @author :RukiHuang
* @description:文件名替换工具 避免文件名重复
* @date :2022/9/2 10:09
*/
public class UploadUtils {
public static String generateFileName(String oldName) {
String suffix = oldName.substring(oldName.lastIndexOf("."));
return IDUtils.generateUniqueId() + suffix;
}
}
/**
* @author :RukiHuang
* @description:课件的Controller
* @date :2022/9/1 13:37
*/
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("/forum")
@RestController
public class CoursewareController {
private static Logger logger = LoggerFactory.getLogger(DocController.class);
@Autowired
CoursewareService coursewareService;
@Autowired
UploadService uploadService;
@RequestMapping("/coursewareUpload/uploadCourseware")
public ResponseResult uploadVideo(
@RequestParam("file") MultipartFile file
) {
String filename = null;
try {
filename = uploadService.uploadCourseware(file);
return ResponseResult.ok(filename, "课件上传成功");
} catch (IOException e) {
logger.error(e.getMessage());
return ResponseResult.failed(e.getMessage(),"课件上传失败");
}
}
@RequestMapping(value = "/coursewareUpload/submitCoursewareInfo", method = RequestMethod.POST)
public ResponseResult submitCoursewareInfo(
@RequestParam(name = "serverFileName")String serverFileName) {
try {
coursewareService.addCoursewareInfo(serverFileName);
return ResponseResult.ok("提交成功");
} catch (Exception e) {
logger.error(e.getMessage());
return ResponseResult.failed(e.getMessage(), "提交失败");
}
}
}
UploadService
/**
* @author :RukiHuang
* @description:上传service
* @date :2022/9/2 10:16
*/
@Service
public class UploadServiceImpl implements UploadService {
@Autowired
UploadProperties uploadProperties;
@Override
public String uploadCourseware(MultipartFile file) throws IOException {
System.out.println(file.getContentType());
if(!uploadProperties.getAllowType().get(0).equals(file.getContentType())) {
throw new IOException("课件上传类型错误");
}
String fileName = UploadUtils.generateFileName(file.getOriginalFilename());
File newFile = new File(uploadProperties.getPath()+"\\courseware\\" + fileName);//当前是在windows目录,部署到linux路径要修改为/courseware
file.transferTo(newFile);
System.out.println(newFile.getPath());
return fileName;
}
}
CoursewareService
/**
* @author :RukiHuang
* @description:课件service
* @date :2022/9/1 13:42
*/
@Service
public class CoursewareServiceImpl implements CoursewareService {
@Autowired
CoursewareDao coursewareDao;
@Override
public void addCoursewareInfo(String serverFileName) throws Exception {
String[] nameArray = serverFileName.split(" / ");
String coursewareName = nameArray[0];
String serverStorageName = nameArray[1];
String storagePath = "courseware/" + serverStorageName;
CoursewareInfoDto coursewareInfoDto = new CoursewareInfoDto(coursewareName, storagePath);
coursewareDao.addCoursewareInfo(coursewareInfoDto);
}
}
点击或拖拽文件至此
文档限制pdf格式,视频限制mp4格式(大小不超过200MB)
上传文件: {{ file.name }}
file
会被替换
作业指导书:
SOC文档:
教学视频:
工段岗位:
岗位职责: