Srpin Boot实现文件上传
@Controller
public class UploadController {
@RequestMapping("upload")
@ResponseBody
public String upload(MultipartFile file) throws IOException {
String exName= file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = UUID.randomUUID().toString()+exName;
FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(new File("D:\\data\\"+fileName)));
System.out.println(file);
return fileName;
}
}
前端代码
<form action="/jobadmin/jobregister/upload" method="post" enctype="multipart/form-data">
<div class="col-xs-2">
<input type="file" name="filePath">
</div>
<div class="col-xs-2">
<input class="btn btn-block btn-success" type="submit" value="文件上传">
</div>
</form>