摘要:今天我們嘗試整合,并決定建立一個非常簡單的微服務(wù),使用作為前端渲編程語言進(jìn)行前端頁面渲染基礎(chǔ)環(huán)境技術(shù)版本創(chuàng)建項目初始化項目修改增加和的支持開發(fā)存儲服務(wù)器一個簡單的應(yīng)用類添加接口
今天我們嘗試Spring Boot整合Angular,并決定建立一個非常簡單的Spring Boot微服務(wù),使用Angular作為前端渲編程語言進(jìn)行前端頁面渲染.
基礎(chǔ)環(huán)境技術(shù) | 版本 |
---|---|
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開發(fā)存儲服務(wù)器 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}
一個簡單的應(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 *添加Rest API接口功能(提供上傳服務(wù))* 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); } }
創(chuàng)建一個controller文件夾并在該文件夾下創(chuàng)建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類文件增加以下設(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 *啟動服務(wù),測試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"
返回結(jié)果
上傳文件成功
測試查詢文件接口
curl localhost:8080/upload
返回結(jié)果
["qrcode_for_ambari.jpg"]
測試刪除接口
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接口,我們提供一個文件下載接口
/** * 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");// 設(shè)置強制下載不打開 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 "文件下載失敗"; } }
測試下載文件
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)建一個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 *打包文件部署* 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(); } }
打包數(shù)據(jù)
mvn clean package -Dmaven.test.skip=true -X
運行打包后的文件即可
java -jar target/spring-learn-integration-springboot-storage-1.0.0.jar源碼地址
GitHub
Gitee
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/74896.html
摘要:筆主很早就開始用阿里云存儲服務(wù)當(dāng)做自己的圖床了。阿里云對象存儲文檔,本篇文章會介紹到整合阿里云存儲服務(wù)實現(xiàn)文件上傳下載以及簡單的查看。 Github 地址:https://github.com/Snailclimb/springboot-integration-examples(SpringBoot和其他常用技術(shù)的整合,可能是你遇到的講解最詳細(xì)的學(xué)習(xí)案例,力爭新手也能看懂并且能夠在看完...
摘要:市長信箱郵件查詢服務(wù)使用構(gòu)建工程一直想用做個微服務(wù)練練手為后續(xù)部署到打下基礎(chǔ)今天比較空閑就開始把部分想法落地了概覽用來練手的應(yīng)用是一個市長信箱的內(nèi)容抓取與檢索頁面鑒于我的八卦特質(zhì)總想了解下周邊的一些投訴信息而成都的市長信箱是一個絕好的信息來 市長信箱郵件查詢服務(wù): 使用SpringBoot構(gòu)建工程 一直想用SpringBoot做個微服務(wù),練練手, 為后續(xù)部署到docker打下基礎(chǔ). 今...
摘要:文件服務(wù)器項目為文章共享社區(qū),少不了的就是一個存儲文章的文件服務(wù)器,包括存儲一些圖片之類的靜態(tài)資源。例如數(shù)據(jù)庫的數(shù)據(jù)文件的配置文件和文件服務(wù)器目錄。 前言 這是一次完整的項目實踐,Angular頁面+Springboot接口+MySQL都通過Dockerfile打包成docker鏡像,通過docker-compose做統(tǒng)一編排。目的是實現(xiàn)整個項目產(chǎn)品的輕量級和靈活性,在將各個模塊的鏡像...
摘要:一概括,如果使用開發(fā)一個的應(yīng)用創(chuàng)建一個項目并且導(dǎo)入相關(guān)包。創(chuàng)建一個編寫一個控制類需要一個部署應(yīng)用的服務(wù)器如,特點設(shè)計目的是用來簡化新應(yīng)用的初始搭建以及開發(fā)過程。啟動器可以和位于同一個包下,或者位于的上一級包中,但是不能放到的平級以及子包下。 一,Spring Boot 介紹 Spring Boot不是一個新的框架,默認(rèn)配置了多種框架使用方式,使用SpringBoot很容易創(chuàng)建一個獨立運...
摘要:引入了新的環(huán)境和概要信息,是一種更揭秘與實戰(zhàn)六消息隊列篇掘金本文,講解如何集成,實現(xiàn)消息隊列。博客地址揭秘與實戰(zhàn)二數(shù)據(jù)緩存篇掘金本文,講解如何集成,實現(xiàn)緩存。 Spring Boot 揭秘與實戰(zhàn)(九) 應(yīng)用監(jiān)控篇 - HTTP 健康監(jiān)控 - 掘金Health 信息是從 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...
閱讀 3457·2023-04-26 01:45
閱讀 2246·2021-11-23 09:51
閱讀 3648·2021-10-18 13:29
閱讀 3445·2021-09-07 10:12
閱讀 709·2021-08-27 16:24
閱讀 1780·2019-08-30 15:44
閱讀 2201·2019-08-30 15:43
閱讀 2960·2019-08-30 13:11