SpringMVC文件上传到服务器
//将图片上传到服务器
@RequestMapping(path = "/pic.do")
@ResponseBody
public Map pic(@RequestParam("file") MultipartFile file) throws Exception {
String basepath = "http://192.168.0.1:8080/images/";
String directory = "fruit/";
String uuid_pic = UUID.randomUUID().toString().replaceAll("-", "");
//获取图片的原始名字
String oldname = file.getOriginalFilename();
//获取图片的后缀名
String suffix = oldname.substring(oldname.lastIndexOf("."));
//上传服务器所在的地址
String path = basepath + directory + uuid_pic + suffix;
Client client = Client.create();
// 和图片服务器进行连接
WebResource webResource = client.resource(path);
Map map = new HashMap();
map.put("code", 0);
// 上传文件
webResource.put(file.getBytes());
map.put("msg", "上传成功");
map.put("code", 100);
//保存新头像地址到map集合,
// 以传递到前端,保存到前端表单
map.put("address", directory + uuid_pic + suffix);
return map;
}