@Controller
@RequestMapping("/file")
public class Upload {
@RequestMapping("/test2")
public String test2(String username, MultipartFile imgs) throws Exception {
/*
* 跨服务器上传
* */
//1、指定上传的位置
String realPath = "http://localhost:8081/pic/";
//2、获取上传文件名称
String fileName = imgs.getOriginalFilename();
//3、组装上传路径
String path = realPath +"/"+ fileName;
//4、实现上传操作
//imgs.transferTo(new File(path));
//创建客户端对象
Client client = Client.create();
//URL
WebResource resource = client.resource(path);//http://localhost:8081/pic/abc.png
//写入操作
resource.put(imgs.getBytes());
return "success";
}
@RequestMapping("/test1")
public String test1(HttpServletRequest req, String username, MultipartFile imgs) throws Exception {
/*
* 本地服务器上传
* */
// System.out.println(username);
// System.out.println(imgs.getOriginalFilename());
//
//1、指定上传的位置
String realPath = req.getServletContext().getRealPath("/pic/");
File file = new File(realPath);
if (!file.exists()) {
file.mkdirs();
}
//2、获取上传文件名称
String fileName = imgs.getOriginalFilename();
//3、组装上传路径
String path = realPath +"/"+ fileName;
//4、实现上传操作
imgs.transferTo(new File(path));
return “success”;
}
}