SSM框架使用第七集 - 文件上传
配置文件上传Controller
@Controller
@RequestMapping("/File")
public class FileController {
@Resource
public respond respond;
@RequestMapping(value = "/images",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> imageUpload(MultipartFile upload) throws IOException {
//获取原始文件名
String filename = upload.getOriginalFilename();
//获取随机ID
String uuid = UUID.randomUUID().toString().replace("-", "");
//保存新名字
String saveName = uuid + "_.jpg";
//根据在WEB.XML配置的动态路径获取Resources路径
String rootPath=System.getProperty("rootpath");
//保存文件
upload.transferTo(new File(rootPath+"/images/"+saveName));
return respond.OK("/images/"+saveName);
}
}