摘要:今天我們嘗試整合,并決定建立一個非常簡單的微服務,使用作為前端渲編程語言進行前端頁面渲染基礎環境技術版本創建項目初始化項目修改增加和的支持開發存儲服務器一個簡單的應用類添加接口
今天我們嘗試Spring Boot整合Angular,并決定建立一個非常簡單的Spring Boot微服務,使用Angular作為前端渲編程語言進行前端頁面渲染.
基礎環境技術 | 版本 |
---|---|
Java | 1.8+ |
SpringBoot | 1.5.x |
初始化項目
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的支持
spring-learn-integration-springboot com.edurt.sli 1.0.0 4.0.0 spring-learn-integration-springboot-storage SpringBoot開發存儲服務器 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin ${dependency.springboot.version} true org.apache.maven.plugins maven-compiler-plugin ${plugin.maven.compiler.version} ${system.java.version}
一個簡單的應用類
/** * 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 *添加Rest API接口功能(提供上傳服務)* http://www.apache.org/licenses/LICENSE-2.0 *
* 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; /** *
SpringBootStorageIntegration
*Description : SpringBootStorageIntegration
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-10 15:53
*Author Email: qianmoQ
*/ @SpringBootApplication public class SpringBootStorageIntegration { public static void main(String[] args) { SpringApplication.run(SpringBootStorageIntegration.class, args); } }
創建一個controller文件夾并在該文件夾下創建UploadController Rest API接口,我們提供一個簡單的文件上傳接口
/** * 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 ** http://www.apache.org/licenses/LICENSE-2.0 *
* 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; /** *
UploadController
*Description : UploadController
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-10 15:55
*Author Email: qianmoQ
*/ @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類文件增加以下設置掃描路徑,以便掃描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 *啟動服務,測試API接口可用性* http://www.apache.org/licenses/LICENSE-2.0 *
* 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; /** *
SpringBootStorageIntegration
*Description : SpringBootStorageIntegration
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-10 15:53
*Author Email: qianmoQ
*/ @SpringBootApplication @ComponentScan(value = { "com.edurt.sli.sliss.controller" }) public class SpringBootStorageIntegration { public static void main(String[] args) { SpringApplication.run(SpringBootStorageIntegration.class, args); } }
在編譯器中直接啟動SpringBootStorageIntegration類文件即可,或者打包jar啟動,打包命令mvn clean package
測試上傳文件接口
curl localhost:8080/upload -F "file=@/Users/shicheng/Downloads/qrcode/qrcode_for_ambari.jpg"
返回結果
上傳文件成功
測試查詢文件接口
curl localhost:8080/upload
返回結果
["qrcode_for_ambari.jpg"]
測試刪除接口
curl -X DELETE "localhost:8080/upload?file=qrcode_for_ambari.jpg"
返回結果
刪除文件qrcode_for_ambari.jpg成功
再次查詢查看文件是否被刪除
curl localhost:8080/upload
返回結果
[]增加下載文件支持
在controller文件夾下創建DownloadController Rest API接口,我們提供一個文件下載接口
/** * 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 ** http://www.apache.org/licenses/LICENSE-2.0 *
* 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.*; /** *
DownloadController
*Description : DownloadController
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-10 16:21
*Author Email: qianmoQ
*/ @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");// 設置強制下載不打開 response.addHeader("Content-Disposition", "attachment;fileName=" + file);// 設置文件名 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 "文件下載失敗"; } }
測試下載文件
curl -o a.jpg "localhost:8080/download?file=qrcode_for_ambari.jpg"
出現以下進度條
% 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
返回結果
a.jpg文件大小設置
默認情況下,Spring Boot最大文件上傳大小為1MB,您可以通過以下應用程序屬性配置值:
配置文件
#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
代碼配置,創建一個config文件夾,并在該文件夾下創建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 *打包文件部署* http://www.apache.org/licenses/LICENSE-2.0 *
* 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; /** *
MultipartConfig
*Description : MultipartConfig
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-10 16:34
*Author Email: qianmoQ
*/ @Configuration public class MultipartConfig { @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setMaxFileSize("10240KB"); //KB,MB factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); } }
打包數據
mvn clean package -Dmaven.test.skip=true -X
運行打包后的文件即可
java -jar target/spring-learn-integration-springboot-storage-1.0.0.jar源碼地址
GitHub
Gitee
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/74896.html
摘要:筆主很早就開始用阿里云存儲服務當做自己的圖床了。阿里云對象存儲文檔,本篇文章會介紹到整合阿里云存儲服務實現文件上傳下載以及簡單的查看。 Github 地址:https://github.com/Snailclimb/springboot-integration-examples(SpringBoot和其他常用技術的整合,可能是你遇到的講解最詳細的學習案例,力爭新手也能看懂并且能夠在看完...
摘要:市長信箱郵件查詢服務使用構建工程一直想用做個微服務練練手為后續部署到打下基礎今天比較空閑就開始把部分想法落地了概覽用來練手的應用是一個市長信箱的內容抓取與檢索頁面鑒于我的八卦特質總想了解下周邊的一些投訴信息而成都的市長信箱是一個絕好的信息來 市長信箱郵件查詢服務: 使用SpringBoot構建工程 一直想用SpringBoot做個微服務,練練手, 為后續部署到docker打下基礎. 今...
摘要:文件服務器項目為文章共享社區,少不了的就是一個存儲文章的文件服務器,包括存儲一些圖片之類的靜態資源。例如數據庫的數據文件的配置文件和文件服務器目錄。 前言 這是一次完整的項目實踐,Angular頁面+Springboot接口+MySQL都通過Dockerfile打包成docker鏡像,通過docker-compose做統一編排。目的是實現整個項目產品的輕量級和靈活性,在將各個模塊的鏡像...
摘要:一概括,如果使用開發一個的應用創建一個項目并且導入相關包。創建一個編寫一個控制類需要一個部署應用的服務器如,特點設計目的是用來簡化新應用的初始搭建以及開發過程。啟動器可以和位于同一個包下,或者位于的上一級包中,但是不能放到的平級以及子包下。 一,Spring Boot 介紹 Spring Boot不是一個新的框架,默認配置了多種框架使用方式,使用SpringBoot很容易創建一個獨立運...
摘要:引入了新的環境和概要信息,是一種更揭秘與實戰六消息隊列篇掘金本文,講解如何集成,實現消息隊列。博客地址揭秘與實戰二數據緩存篇掘金本文,講解如何集成,實現緩存。 Spring Boot 揭秘與實戰(九) 應用監控篇 - HTTP 健康監控 - 掘金Health 信息是從 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...
閱讀 3452·2023-04-26 01:45
閱讀 2227·2021-11-23 09:51
閱讀 3641·2021-10-18 13:29
閱讀 3437·2021-09-07 10:12
閱讀 702·2021-08-27 16:24
閱讀 1773·2019-08-30 15:44
閱讀 2198·2019-08-30 15:43
閱讀 2953·2019-08-30 13:11