springmvc實現跨服務器文件上傳功能
更新時間:2019年08月23日 15:55:15 作者:Hello_MAOSONG
這篇文章主要為大家詳細介紹了springmvc實現跨服務器文件上傳功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了springmvc實現跨服務器文件上傳功能的具體代碼,供大家參考,具體內容如下
1.創(chuàng)建一個新的maven工程并且部署tomcat,用于做圖片服務器并且在webapp下創(chuàng)建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;
//完成跨服務器上傳
//創(chuàng)建客戶端對象
Client client = Client.create();
//和圖片服務器進行連接
WebResource webResource = client.resource(path + filename);
//上傳文件
webResource.put(upload.getBytes());
return "success";
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring boot 默認靜態(tài)資源路徑與手動配置訪問路徑的方法
這篇文章主要介紹了Spring boot 默認靜態(tài)資源路徑與手動配置訪問路徑的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05
SpringBoot MainApplication類文件的位置詳解
這篇文章主要介紹了SpringBoot MainApplication類文件的位置詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Springboot如何通過filter修改Header的值
這篇文章主要介紹了Springboot如何通過filter修改Header的值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

