基础写法
@RequestMapping("/a")
public String FileUpdate (HttpServletRequest request,MultipartFile file) {
String originalname=file.getOriginalFilename();
String newName= originalname.substring(originalname.lastIndexOf("."));
newName = UUID.randomUUID().toString()+newName;
String 文件放置位置="C:/Users/14779/Documents/IdeaProjects/qiyeji/src/main/resources/static";
File newFile=new File(文件放置位置,newName);
if(!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
try {
file.transferTo(newFile);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
return 文件放置位置+newName;
}
放在static文件夹中的使用例子
@ApiOperation(value = "_1_8_上传文件", notes="这里的参数只用关心前3个即可,切勿直接使用浏览器测试该api。<br>请使用vue的上传组件测试该api!")
@RequestMapping(value = "/上传文件/{员工工号} {客户姓名} {项目编号}",method = RequestMethod.POST)
public String _1_8_上传文件 (@PathVariable("员工工号") int 员工工号,@PathVariable("客户姓名") String 客户姓名,@PathVariable("项目编号") int 项目编号,
HttpServletRequest request, MultipartFile file) throws IOException {
String originalname=file.getOriginalFilename();
String newName= originalname.substring(originalname.lastIndexOf("."));
newName = UUID.randomUUID().toString()+newName;
String 项目更目录 = new File("").getCanonicalPath() ;
String 文件放置位置=项目更目录+"\\src\\main\\resources\\static\\"+员工工号+"\\";
File newFile=new File(文件放置位置,newName);
if(!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
try {
file.transferTo(newFile);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
int 同部门主管的工号=service员工.返回和该员工同部门的主管的员工工号(员工工号);
String 部门名=service员工.返回该员工的部门名(员工工号);
service审核文件.插入新审核文件("localhost:1234/"+员工工号+"/"+newName,客户姓名,员工工号,项目编号,同部门主管的工号,部门名,0,"",0,"");
return 文件放置位置+newName;
}