av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java基于spring boot本地上傳圖片示例解析

瀏覽:12日期:2023-08-26 11:45:53

前幾天項(xiàng)目中剛好需要上傳圖片的需求,當(dāng)時(shí)想的是用七牛云,因?yàn)槲矣闷吲T埔灿昧撕脦状?就是把圖片上傳到七牛云空間里面,數(shù)據(jù)庫(kù)里面保存的是這張上傳圖片的url地址 那么頁(yè)面訪問(wèn)也就很方便,考慮到項(xiàng)目部署的環(huán)境我就用了本地上傳,不牽涉數(shù)據(jù)庫(kù)的操作。我就花了半個(gè)小時(shí)寫了個(gè)本地上傳圖片的小demo。非常的簡(jiǎn)單。

下面是需要的依賴 pom.xml文件:

<?xml version='1.0' encoding='UTF-8'?> <project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.com.sctic</groupId> <artifactId>upload</artifactId> <version>0.0.1-SNAPSHOT</version> <name>upload</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency> </dependencies> <build> <plugins><plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId></plugin> </plugins> </build> </project>

控制器: UploadController

@Controller public class UploadController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Value('${scitc.upload.src}') private String rootPath; @Value('${scitc.upload.host}') private String uploadhost; @RequestMapping(value = '/uploadFile',method = {RequestMethod.POST,RequestMethod.GET}) @ResponseBody public String uploadFile(MultipartFile file) {//文件的完整名稱,如spring.jpeg String filename = file.getOriginalFilename(); //文件名,如spring String name = filename.substring(0,filename.indexOf('.')); //文件后綴,如.jpeg String suffix = filename.substring(filename.lastIndexOf('.'));//創(chuàng)建年月文件夾 Calendar date = Calendar.getInstance(); File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));//目標(biāo)文件 File descFile = new File(rootPath+File.separator+dateDirs+File.separator+filename); int i = 1; //若文件存在重命名 String newFilename = filename; while(descFile.exists()) {newFilename = name+'('+i+')'+suffix;String parentPath = descFile.getParent();descFile = new File(parentPath+File.separator+newFilename);i++; } //判斷目標(biāo)文件所在的目錄是否存在 if(!descFile.getParentFile().exists()) {//如果目標(biāo)文件所在的目錄不存在,則創(chuàng)建父目錄descFile.getParentFile().mkdirs(); } //將內(nèi)存中的數(shù)據(jù)寫入磁盤 try {file.transferTo(descFile); } catch (Exception e) {e.printStackTrace();logger.error('上傳失敗,cause:{}',e); } //完整的url String fileUrl = uploadhost + rootPath +dateDirs+ '/'+newFilename; return 'success:' + fileUrl; } }

注意:rootPath,uploadhost是可以通過(guò)application.properties或者application.yml進(jìn)行配置的。

由于要對(duì)外部資源進(jìn)行映射需要?jiǎng)?chuàng)建一個(gè)類繼承WebMvcConfigurationSupport這個(gè)適配器,下面是WebMvcConfigurer的這個(gè)配置類,代碼如下:

@Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { @Value('${scitc.upload.src}') private String src; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(src + '/**').addResourceLocations('file:' + src); } }

注意:這里的src也是從配置文件applicaiton.properties中得到了。

下面是application.properties配置:

server.port=8848 ##文件上傳config scitc.upload.host:127.0.0.1:${server.port} scitc.upload.src=/Users/jswzj/Desktop/uploads/ spring.servlet.multipart.maxFileSize=10MB spring.servlet.multipart.maxRequestSize=10MB server.port=8848 服務(wù)器的端口號(hào) scitc.upload.host:服務(wù)器ip地址 + server.port scitc.upload.src:你要把用戶上傳的圖片上傳到那個(gè)位置**

最后我們?cè)L問(wèn)這個(gè)接口效果圖如下:

java基于spring boot本地上傳圖片示例解析

上傳成功后拿到這個(gè)url地址 粘貼到瀏覽器地址上就能訪問(wèn)了

java基于spring boot本地上傳圖片示例解析

總結(jié):圖片上傳有很多的方式,當(dāng)然這個(gè)是根據(jù)業(yè)務(wù)的需求,很多人都喜歡把圖片的url上傳到數(shù)據(jù)庫(kù)中,用實(shí)體類來(lái)對(duì)圖片的高度、寬度、名稱、url進(jìn)行封裝,我覺(jué)得如果你部署的那臺(tái)服務(wù)器是有網(wǎng)絡(luò)的環(huán)境下建議用七牛云上傳,七牛云上傳把圖片保存到七牛云空間,那個(gè)url地址是不會(huì)發(fā)生變化的,不會(huì)應(yīng)為你項(xiàng)目的遷移或者服務(wù)器地址發(fā)生變化而受影響。看各自的需求吧。等有時(shí)間我會(huì)出一個(gè)七牛云上傳的demo讓大家學(xué)習(xí)。最后謝謝大家的支持,希望大家每天都要收獲。祝大家早日成為大神。

下面是這個(gè)demo的github的地址,希望大家fork,start一下,謝謝

https://github.com/zhoubiao188/springboot-upload

到此這篇關(guān)于java基于spring boot本地上傳圖片示例解析的文章就介紹到這了,更多相關(guān)spring boot本地上傳圖片內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 国产日韩欧美二区 | 亚洲欧美日韩精品久久亚洲区 | 一区免费视频 | 伊人精品久久久久77777 | 免费看淫片| a久久 | 瑟瑟视频在线看 | 日日夜夜天天干 | 亚洲一区视频在线播放 | 欧美激情精品久久久久久变态 | www.色五月.com| 国产一区三区在线 | 911精品国产 | 亚洲狠狠爱一区二区三区 | 国产福利在线播放麻豆 | 四虎影视一区二区 | 精品欧美乱码久久久久久1区2区 | 午夜在线视频一区二区三区 | 91久久爽久久爽爽久久片 | 99视频在线免费观看 | 国产一区精品在线 | 精品中文字幕一区 | 久久i| 国产成人精品免费视频 | 日韩久久久久 | 91观看 | 亚洲人成人一区二区在线观看 | 亚洲一区二区三区在线播放 | 国产精品日日做人人爱 | 免费毛片www com cn | 国产精品久久久久久久久久免费看 | 亚洲精品成人在线 | 99热视| 欧美男人天堂 | 久久久91精品国产一区二区精品 | 日韩av成人在线观看 | 青青操av | 亚洲欧美国产毛片在线 | 欧美888| 黄色大片在线视频 | 成人免费视频网站在线看 |