SpringBoot整合TomCat實(shí)現(xiàn)本地圖片服務(wù)器代碼解析
后臺(tái)控制層:
public static final String HEAD_IMG_DIR = 'D:/upload/'; // 本地存放圖片路徑 //圖片上傳 @RequestMapping('/upload') @ResponseBody public String upload(MultipartFile file) { //文件真實(shí)上傳名字 String filename = file.getOriginalFilename(); //文件大小 Long size = file.getSize(); String contentType = file.getContentType(); //文件臨時(shí)儲(chǔ)存到本地 String folder = HEAD_IMG_DIR; //生成保存的文件名字,這個(gè)名字要存到數(shù)據(jù)庫(kù)中 String uuid = UUID.randomUUID().toString(); try { file.transferTo(new File(folder + uuid)); } catch (IOException e) { e.printStackTrace(); } return uuid; // 返回給前臺(tái) uuid 需和信息一起存到數(shù)據(jù)庫(kù) }
Tomcat:
打開(kāi)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本地服務(wù)器--> <Service name='newtest'> <!--分配8020端口 --> <Connector port='8020' protocol='HTTP/1.1' connectionTimeout='20000' URIEncoding='GBK' redirectPort='8443' /> <Engine name='newtest' defaultHost='localhost'> <!--name為項(xiàng)目訪問(wèn)地址 此配置的訪問(wèn)為http://localhost:8020 appBase配置tomcat下wabapps下的路徑--> <Host name='localhost' appBase='D://TomCat//webapps' unpackWARs='true' autoDeploy='true' xmlValidation='false' xmlNamespaceAware='false'> <!--資源地址--> <!-- 就是訪問(wèn)http://localhost:8020這個(gè)地址就是到D://upload這個(gè)目錄下 --> <Context path='' docBase='D://upload' debug='0' reloadable='false'/> </Host> </Engine> </Service> <Service name='Catalina'>
前臺(tái)頁(yè)面:
url: ’http://127.0.0.1:8020/’,<wiz_tmp_tag contenteditable='false' style='display: none;'>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)2. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析3. python 統(tǒng)計(jì)list中各個(gè)元素出現(xiàn)的次數(shù)的幾種方法4. ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)碼5. 一文搞懂 parseInt()函數(shù)異常行為6. python學(xué)習(xí)之plot函數(shù)的使用教程7. Python 中random 庫(kù)的詳細(xì)使用8. Python 有可能刪除 GIL 嗎?9. 聊聊python在linux下與windows下導(dǎo)入模塊的區(qū)別說(shuō)明10. Python基于百度AI實(shí)現(xiàn)抓取表情包
