springmvc实现跨服务器文件上传功能
人气:01.创建一个新的maven工程并且部署tomcat,用于做图片服务器并且在webapp下创建uploads文件
2.在应用服务器上的pom.xml导入坐标
<!--跨服务器上传文件--> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.18.1</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.18.1</version> </dependency>
3.index.jsp
<h3>跨服务器文件上传</h3> <form action="/user/fileupload" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="upload"><br> <input type="submit" value="上传"> </form>
4.conteoller
@Controller @RequestMapping("/user") public class UserConteoller { /** * 跨服务器文件上传 * @return */ @RequestMapping("/fileupload") public String fileupload(HttpServletRequest request, MultipartFile upload) throws Exception { System.out.println("文件上传"); //定义上传文件服务器路径 String path = "http://localhost:9090/uploads/"; //获取上传文件的名称 String filename = upload.getOriginalFilename(); System.out.println(filename); //把文件的名称设置位置 uuid String uuid = UUID.randomUUID().toString().replace("-", ""); System.out.println(uuid); filename = uuid + "_" + filename; //完成跨服务器上传 //创建客户端对象 Client client = Client.create(); //和图片服务器进行连接 WebResource webResource = client.resource(path + filename); //上传文件 webResource.put(upload.getBytes()); return "success"; } }
您可能感兴趣的文章:
加载全部内容