SpringBoot整合TomCat實現本地圖片服務器代碼解析
后臺控制層:
public static final String HEAD_IMG_DIR = 'D:/upload/'; // 本地存放圖片路徑 //圖片上傳 @RequestMapping('/upload') @ResponseBody public String upload(MultipartFile file) { //文件真實上傳名字 String filename = file.getOriginalFilename(); //文件大小 Long size = file.getSize(); String contentType = file.getContentType(); //文件臨時儲存到本地 String folder = HEAD_IMG_DIR; //生成保存的文件名字,這個名字要存到數據庫中 String uuid = UUID.randomUUID().toString(); try { file.transferTo(new File(folder + uuid)); } catch (IOException e) { e.printStackTrace(); } return uuid; // 返回給前臺 uuid 需和信息一起存到數據庫 }
Tomcat:
打開server.xml配置文件,在文件中加上以下代碼
<!-- A 'Service' is a collection of one or more 'Connectors' that share a single 'Container' Note: A 'Service' is not itself a 'Container', so you may not define subcomponents such as 'Valves' at this level. Documentation at /docs/config/service.html --> <!--配置TomCat本地服務器--> <Service name='newtest'> <!--分配8020端口 --> <Connector port='8020' protocol='HTTP/1.1' connectionTimeout='20000' URIEncoding='GBK' redirectPort='8443' /> <Engine name='newtest' defaultHost='localhost'> <!--name為項目訪問地址 此配置的訪問為http://localhost:8020 appBase配置tomcat下wabapps下的路徑--> <Host name='localhost' appBase='D://TomCat//webapps' unpackWARs='true' autoDeploy='true' xmlValidation='false' xmlNamespaceAware='false'> <!--資源地址--> <!-- 就是訪問http://localhost:8020這個地址就是到D://upload這個目錄下 --> <Context path='' docBase='D://upload' debug='0' reloadable='false'/> </Host> </Engine> </Service> <Service name='Catalina'>
前臺頁面:
url: ’http://127.0.0.1:8020/’,<wiz_tmp_tag contenteditable='false' style='display: none;'>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. 詳解Android studio 動態fragment的用法2. 編程語言PHP在Web開發領域的優勢在哪?3. 什么是python的自省4. Spring Boot和Thymeleaf整合結合JPA實現分頁效果(實例代碼)5. 解決Android studio xml界面無法預覽問題6. 基于android studio的layout的xml文件的創建方式7. Android如何加載Base64編碼格式圖片8. Springboot Druid 自定義加密數據庫密碼的幾種方案9. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現10. 圖文詳解vue中proto文件的函數調用

網公網安備