SpringBoot開發(fā)存儲(chǔ)服務(wù)器實(shí)現(xiàn)過程詳解
正文
今天我們嘗試Spring Boot整合Angular,并決定建立一個(gè)非常簡(jiǎn)單的Spring Boot微服務(wù),使用Angular作為前端渲編程語言進(jìn)行前端頁面渲染.
基礎(chǔ)環(huán)境
技術(shù) | 版本 |
---|---|
Java | 1.8+ |
SpringBoot | 1.5.x |
創(chuàng)建項(xiàng)目
- 初始化項(xiàng)目
mvn archetype:generate -DgroupId=com.edurt.sli.sliss -DartifactId=spring-learn-integration-springboot-storage -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
- 修改pom.xml增加java和springboot的支持
<?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"> <parent> <artifactId>spring-learn-integration-springboot</artifactId> <groupId>com.edurt.sli</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring-learn-integration-springboot-storage</artifactId> <name>SpringBoot開發(fā)存儲(chǔ)服務(wù)器</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${dependency.springboot.version}</version> <configuration> <fork>true</fork> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${plugin.maven.compiler.version}</version> <configuration> <source>${system.java.version}</source> <target>${system.java.version}</target> </configuration> </plugin> </plugins> </build> </project>
- 一個(gè)簡(jiǎn)單的應(yīng)用類
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.sliss; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * <p> SpringBootStorageIntegration </p> * <p> Description : SpringBootStorageIntegration </p> * <p> Author : qianmoQ </p> * <p> Version : 1.0 </p> * <p> Create Time : 2019-06-10 15:53 </p> * <p> Author Email: <a href="mailTo:shichengoooo@163.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >qianmoQ</a> </p> */ @SpringBootApplication public class SpringBootStorageIntegration { public static void main(String[] args) { SpringApplication.run(SpringBootStorageIntegration.class, args); } }
添加Rest API接口功能(提供上傳服務(wù))
- 創(chuàng)建一個(gè)controller文件夾并在該文件夾下創(chuàng)建UploadController Rest API接口,我們提供一個(gè)簡(jiǎn)單的文件上傳接口
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.sliss.controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; /** * <p> UploadController </p> * <p> Description : UploadController </p> * <p> Author : qianmoQ </p> * <p> Version : 1.0 </p> * <p> Create Time : 2019-06-10 15:55 </p> * <p> Author Email: <a href="mailTo:shichengoooo@163.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >qianmoQ</a> </p> */ @RestController @RequestMapping(value = "upload") public class UploadController { // 文件上傳地址 private final static String UPLOADED_FOLDER = "/Users/shicheng/Desktop/test/"; @PostMapping public String upload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "上傳文件不能為空"; } try { byte[] bytes = file.getBytes(); Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename()); Files.write(path, bytes); return "上傳文件成功"; } catch (IOException ioe) { return "上傳文件失敗,失敗原因: " + ioe.getMessage(); } } @GetMapping public Object get() { File file = new File(UPLOADED_FOLDER); String[] filelist = file.list(); return filelist; } @DeleteMapping public String delete(@RequestParam(value = "file") String file) { File source = new File(UPLOADED_FOLDER + file); source.delete(); return "刪除文件" + file + "成功"; } }
- 修改SpringBootAngularIntegration類文件增加以下設(shè)置掃描路徑,以便掃描Controller
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.sliss; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; /** * <p> SpringBootStorageIntegration </p> * <p> Description : SpringBootStorageIntegration </p> * <p> Author : qianmoQ </p> * <p> Version : 1.0 </p> * <p> Create Time : 2019-06-10 15:53 </p> * <p> Author Email: <a href="mailTo:shichengoooo@163.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >qianmoQ</a> </p> */ @SpringBootApplication @ComponentScan(value = { "com.edurt.sli.sliss.controller" }) public class SpringBootStorageIntegration { public static void main(String[] args) { SpringApplication.run(SpringBootStorageIntegration.class, args); } }
啟動(dòng)服務(wù),測(cè)試API接口可用性
在編譯器中直接啟動(dòng)SpringBootStorageIntegration類文件即可,或者打包jar啟動(dòng),打包命令mvn clean package
- 測(cè)試上傳文件接口
curl localhost:8080/upload -F "file=@/Users/shicheng/Downloads/qrcode/qrcode_for_ambari.jpg"
返回結(jié)果
上傳文件成功
- 測(cè)試查詢文件接口
curl localhost:8080/upload
返回結(jié)果
["qrcode_for_ambari.jpg"]
- 測(cè)試刪除接口
curl -X DELETE 'localhost:8080/upload?file=qrcode_for_ambari.jpg'
返回結(jié)果
刪除文件qrcode_for_ambari.jpg成功
再次查詢查看文件是否被刪除
curl localhost:8080/upload
返回結(jié)果
[]
增加下載文件支持
- 在controller文件夾下創(chuàng)建DownloadController Rest API接口,我們提供一個(gè)文件下載接口
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.sliss.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.*; /** * <p> DownloadController </p> * <p> Description : DownloadController </p> * <p> Author : qianmoQ </p> * <p> Version : 1.0 </p> * <p> Create Time : 2019-06-10 16:21 </p> * <p> Author Email: <a href="mailTo:shichengoooo@163.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >qianmoQ</a> </p> */ @RestController @RequestMapping(value = "download") public class DownloadController { private final static String UPLOADED_FOLDER = "/Users/shicheng/Desktop/test/"; @GetMapping public String download(@RequestParam(value = "file") String file, HttpServletResponse response) { if (!file.isEmpty()) { File source = new File(UPLOADED_FOLDER + file); if (source.exists()) { response.setContentType("application/force-download");// 設(shè)置強(qiáng)制下載不打開 response.addHeader("Content-Disposition", "attachment;fileName=" + file);// 設(shè)置文件名 byte[] buffer = new byte[1024]; FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; try { fileInputStream = new FileInputStream(source); bufferedInputStream = new BufferedInputStream(fileInputStream); OutputStream outputStream = response.getOutputStream(); int i = bufferedInputStream.read(buffer); while (i != -1) { outputStream.write(buffer, 0, i); i = bufferedInputStream.read(buffer); } return "文件下載成功"; } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { return "文件下載失敗,失敗原因: " + e.getMessage(); } } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { return "文件下載失敗,失敗原因: " + e.getMessage(); } } } } } return "文件下載失敗"; } }
- 測(cè)試下載文件
curl -o a.jpg 'localhost:8080/download?file=qrcode_for_ambari.jpg'
出現(xiàn)以下進(jìn)度條
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 148k 0 148k 0 0 11.3M 0 --:--:-- --:--:-- --:--:-- 12.0M
查詢是否下載到本地文件夾
ls a.jpg
返回結(jié)果
a.jpg
文件大小設(shè)置
默認(rèn)情況下,Spring Boot最大文件上傳大小為1MB,您可以通過以下應(yīng)用程序?qū)傩耘渲弥担?/p>
- 配置文件
#http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties #search multipart spring.http.multipart.max-file-size=10MB spring.http.multipart.max-request-size=10MB
- 代碼配置,創(chuàng)建一個(gè)config文件夾,并在該文件夾下創(chuàng)建MultipartConfig
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.sliss.config; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.MultipartConfigElement; /** * <p> MultipartConfig </p> * <p> Description : MultipartConfig </p> * <p> Author : qianmoQ </p> * <p> Version : 1.0 </p> * <p> Create Time : 2019-06-10 16:34 </p> * <p> Author Email: <a href="mailTo:shichengoooo@163.com" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >qianmoQ</a> </p> */ @Configuration public class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setMaxFileSize("10240KB"); //KB,MB factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); } }
打包文件部署
- 打包數(shù)據(jù)
mvn clean package -Dmaven.test.skip=true -X
運(yùn)行打包后的文件即可
java -jar target/spring-learn-integration-springboot-storage-1.0.0.jar
以上就是SpringBoot開發(fā)存儲(chǔ)服務(wù)器實(shí)現(xiàn)過程詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 存儲(chǔ)服務(wù)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java中JDBC實(shí)現(xiàn)動(dòng)態(tài)查詢的實(shí)例詳解
從多個(gè)查詢條件中隨機(jī)選擇若干個(gè)組合成一個(gè)DQL語句進(jìn)行查詢,這一過程叫做動(dòng)態(tài)查詢。下面通過實(shí)例代碼給大家講解JDBC實(shí)現(xiàn)動(dòng)態(tài)查詢的方法,需要的朋友參考下吧2017-07-07解決java main函數(shù)中的args數(shù)組傳值問題
這篇文章主要介紹了解決java main函數(shù)中的args數(shù)組傳值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java編寫簡(jiǎn)單計(jì)算器的完整實(shí)現(xiàn)過程
這篇文章主要給大家介紹了關(guān)于Java編寫簡(jiǎn)單計(jì)算器的完整實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹
這篇文章主要為大家介紹了SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05SpringBoot中驗(yàn)證用戶上傳的圖片資源的方法
這篇文章主要介紹了在SpringBoot中驗(yàn)證用戶上傳的圖片資源,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼
這篇文章主要介紹了Spring Data Jpa Mysql使用utf8mb4編碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù)
這篇文章主要介紹了java如何使用正則表達(dá)式限制特殊字符的個(gè)數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11使用maven對(duì)springboot項(xiàng)目進(jìn)行瘦身分離jar的多種處理方案
springboot項(xiàng)目打包一般我們都使用它自帶的spring-boot-maven-plugin插件,這個(gè)插件默認(rèn)情況下,會(huì)把所有的依賴包全部壓縮到一個(gè)jar里面,今天給大家分享幾種方案來如何減小我們的打包文件,需要的朋友可以參考下2024-02-02