资讯专栏INFORMATION COLUMN

SpringBoot开发存储服务器

godruoyi / 1353人阅读

摘要:今天我们尝试整合,并决定建立一个非常简单的微服务,使用作为前端渲编程语言进行前端页面渲染基础环境技术版本创建项目初始化项目修改增加和的支持开发存储服务器一个简单的应用类添加接口

今天我们尝试Spring Boot整合Angular,并决定建立一个非常简单的Spring Boot微服务,使用Angular作为前端渲编程语言进行前端页面渲染.

基础环境
技术 版本
Java 1.8+
SpringBoot 1.5.x
创建项目

初始化项目

</>复制代码

  1. 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的支持

</>复制代码

  1. spring-learn-integration-springboot
  2. com.edurt.sli
  3. 1.0.0
  4. 4.0.0
  5. spring-learn-integration-springboot-storage
  6. SpringBoot开发存储服务器
  7. org.springframework.boot
  8. spring-boot-starter-web
  9. org.springframework.boot
  10. spring-boot-maven-plugin
  11. ${dependency.springboot.version}
  12. true
  13. org.apache.maven.plugins
  14. maven-compiler-plugin
  15. ${plugin.maven.compiler.version}
  16. ${system.java.version}
  17. ${system.java.version}

一个简单的应用类

</>复制代码

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *

  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *

  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.edurt.sli.sliss;
  19. import org.springframework.boot.SpringApplication;
  20. import org.springframework.boot.autoconfigure.SpringBootApplication;
  21. /**
  22. *

    SpringBootStorageIntegration

  23. *

    Description : SpringBootStorageIntegration

  24. *

    Author : qianmoQ

  25. *

    Version : 1.0

  26. *

    Create Time : 2019-06-10 15:53

  27. *

    Author Email: qianmoQ

  28. */
  29. @SpringBootApplication
  30. public class SpringBootStorageIntegration {
  31. public static void main(String[] args) {
  32. SpringApplication.run(SpringBootStorageIntegration.class, args);
  33. }
  34. }
添加Rest API接口功能(提供上传服务)

创建一个controller文件夹并在该文件夹下创建UploadController Rest API接口,我们提供一个简单的文件上传接口

</>复制代码

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *

  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *

  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.edurt.sli.sliss.controller;
  19. import org.springframework.web.bind.annotation.*;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. /**
  27. *

    UploadController

  28. *

    Description : UploadController

  29. *

    Author : qianmoQ

  30. *

    Version : 1.0

  31. *

    Create Time : 2019-06-10 15:55

  32. *

    Author Email: qianmoQ

  33. */
  34. @RestController
  35. @RequestMapping(value = "upload")
  36. public class UploadController {
  37. // 文件上传地址
  38. private final static String UPLOADED_FOLDER = "/Users/shicheng/Desktop/test/";
  39. @PostMapping
  40. public String upload(@RequestParam("file") MultipartFile file) {
  41. if (file.isEmpty()) {
  42. return "上传文件不能为空";
  43. }
  44. try {
  45. byte[] bytes = file.getBytes();
  46. Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
  47. Files.write(path, bytes);
  48. return "上传文件成功";
  49. } catch (IOException ioe) {
  50. return "上传文件失败,失败原因: " + ioe.getMessage();
  51. }
  52. }
  53. @GetMapping
  54. public Object get() {
  55. File file = new File(UPLOADED_FOLDER);
  56. String[] filelist = file.list();
  57. return filelist;
  58. }
  59. @DeleteMapping
  60. public String delete(@RequestParam(value = "file") String file) {
  61. File source = new File(UPLOADED_FOLDER + file);
  62. source.delete();
  63. return "删除文件" + file + "成功";
  64. }
  65. }

修改SpringBootAngularIntegration类文件增加以下设置扫描路径,以便扫描Controller

</>复制代码

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *

  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *

  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.edurt.sli.sliss;
  19. import org.springframework.boot.SpringApplication;
  20. import org.springframework.boot.autoconfigure.SpringBootApplication;
  21. import org.springframework.context.annotation.ComponentScan;
  22. /**
  23. *

    SpringBootStorageIntegration

  24. *

    Description : SpringBootStorageIntegration

  25. *

    Author : qianmoQ

  26. *

    Version : 1.0

  27. *

    Create Time : 2019-06-10 15:53

  28. *

    Author Email: qianmoQ

  29. */
  30. @SpringBootApplication
  31. @ComponentScan(value = {
  32. "com.edurt.sli.sliss.controller"
  33. })
  34. public class SpringBootStorageIntegration {
  35. public static void main(String[] args) {
  36. SpringApplication.run(SpringBootStorageIntegration.class, args);
  37. }
  38. }
启动服务,测试API接口可用性

</>复制代码

  1. 在编译器中直接启动SpringBootStorageIntegration类文件即可,或者打包jar启动,打包命令mvn clean package

测试上传文件接口

</>复制代码

  1. curl localhost:8080/upload -F "file=@/Users/shicheng/Downloads/qrcode/qrcode_for_ambari.jpg"

返回结果

</>复制代码

  1. 上传文件成功

测试查询文件接口

</>复制代码

  1. curl localhost:8080/upload

返回结果

</>复制代码

  1. ["qrcode_for_ambari.jpg"]

测试删除接口

</>复制代码

  1. curl -X DELETE "localhost:8080/upload?file=qrcode_for_ambari.jpg"

返回结果

</>复制代码

  1. 删除文件qrcode_for_ambari.jpg成功

再次查询查看文件是否被删除

</>复制代码

  1. curl localhost:8080/upload

返回结果

</>复制代码

  1. []
增加下载文件支持

在controller文件夹下创建DownloadController Rest API接口,我们提供一个文件下载接口

</>复制代码

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *

  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *

  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.edurt.sli.sliss.controller;
  19. import org.springframework.web.bind.annotation.GetMapping;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestParam;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.io.*;
  25. /**
  26. *

    DownloadController

  27. *

    Description : DownloadController

  28. *

    Author : qianmoQ

  29. *

    Version : 1.0

  30. *

    Create Time : 2019-06-10 16:21

  31. *

    Author Email: qianmoQ

  32. */
  33. @RestController
  34. @RequestMapping(value = "download")
  35. public class DownloadController {
  36. private final static String UPLOADED_FOLDER = "/Users/shicheng/Desktop/test/";
  37. @GetMapping
  38. public String download(@RequestParam(value = "file") String file,
  39. HttpServletResponse response) {
  40. if (!file.isEmpty()) {
  41. File source = new File(UPLOADED_FOLDER + file);
  42. if (source.exists()) {
  43. response.setContentType("application/force-download");// 设置强制下载不打开
  44. response.addHeader("Content-Disposition", "attachment;fileName=" + file);// 设置文件名
  45. byte[] buffer = new byte[1024];
  46. FileInputStream fileInputStream = null;
  47. BufferedInputStream bufferedInputStream = null;
  48. try {
  49. fileInputStream = new FileInputStream(source);
  50. bufferedInputStream = new BufferedInputStream(fileInputStream);
  51. OutputStream outputStream = response.getOutputStream();
  52. int i = bufferedInputStream.read(buffer);
  53. while (i != -1) {
  54. outputStream.write(buffer, 0, i);
  55. i = bufferedInputStream.read(buffer);
  56. }
  57. return "文件下载成功";
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. } finally {
  61. if (bufferedInputStream != null) {
  62. try {
  63. bufferedInputStream.close();
  64. } catch (IOException e) {
  65. return "文件下载失败,失败原因: " + e.getMessage();
  66. }
  67. }
  68. if (fileInputStream != null) {
  69. try {
  70. fileInputStream.close();
  71. } catch (IOException e) {
  72. return "文件下载失败,失败原因: " + e.getMessage();
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return "文件下载失败";
  79. }
  80. }

测试下载文件

</>复制代码

  1. curl -o a.jpg "localhost:8080/download?file=qrcode_for_ambari.jpg"

出现以下进度条

</>复制代码

  1. % Total % Received % Xferd Average Speed Time Time Time Current
  2. Dload Upload Total Spent Left Speed
  3. 100 148k 0 148k 0 0 11.3M 0 --:--:-- --:--:-- --:--:-- 12.0M

查询是否下载到本地文件夹

</>复制代码

  1. ls a.jpg

返回结果

</>复制代码

  1. a.jpg
文件大小设置

默认情况下,Spring Boot最大文件上传大小为1MB,您可以通过以下应用程序属性配置值:

配置文件

</>复制代码

  1. #http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
  2. #search multipart
  3. spring.http.multipart.max-file-size=10MB
  4. spring.http.multipart.max-request-size=10MB

代码配置,创建一个config文件夹,并在该文件夹下创建MultipartConfig

</>复制代码

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *

  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *

  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package com.edurt.sli.sliss.config;
  19. import org.springframework.boot.web.servlet.MultipartConfigFactory;
  20. import org.springframework.context.annotation.Bean;
  21. import org.springframework.context.annotation.Configuration;
  22. import javax.servlet.MultipartConfigElement;
  23. /**
  24. *

    MultipartConfig

  25. *

    Description : MultipartConfig

  26. *

    Author : qianmoQ

  27. *

    Version : 1.0

  28. *

    Create Time : 2019-06-10 16:34

  29. *

    Author Email: qianmoQ

  30. */
  31. @Configuration
  32. public class MultipartConfig {
  33. @Bean
  34. public MultipartConfigElement multipartConfigElement() {
  35. MultipartConfigFactory factory = new MultipartConfigFactory();
  36. factory.setMaxFileSize("10240KB"); //KB,MB
  37. factory.setMaxRequestSize("102400KB");
  38. return factory.createMultipartConfig();
  39. }
  40. }
打包文件部署

打包数据

</>复制代码

  1. mvn clean package -Dmaven.test.skip=true -X

运行打包后的文件即可

</>复制代码

  1. java -jar target/spring-learn-integration-springboot-storage-1.0.0.jar
源码地址

GitHub

Gitee

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74896.html

相关文章

  • SpringBoot 整合 阿里云OSS 存储服务,快来免费搭建一个自己的图床

    摘要:笔主很早就开始用阿里云存储服务当做自己的图床了。阿里云对象存储文档,本篇文章会介绍到整合阿里云存储服务实现文件上传下载以及简单的查看。 Github 地址:https://github.com/Snailclimb/springboot-integration-examples(SpringBoot和其他常用技术的整合,可能是你遇到的讲解最详细的学习案例,力争新手也能看懂并且能够在看完...

    邹强 评论0 收藏0
  • 市长信箱邮件查询服务: 使用SpringBoot构建工程

    摘要:市长信箱邮件查询服务使用构建工程一直想用做个微服务练练手为后续部署到打下基础今天比较空闲就开始把部分想法落地了概览用来练手的应用是一个市长信箱的内容抓取与检索页面鉴于我的八卦特质总想了解下周边的一些投诉信息而成都的市长信箱是一个绝好的信息来 市长信箱邮件查询服务: 使用SpringBoot构建工程 一直想用SpringBoot做个微服务,练练手, 为后续部署到docker打下基础. 今...

    supernavy 评论0 收藏0
  • 一个网站的微服务架构实战(1)docker和 docker-compose

    摘要:文件服务器项目为文章共享社区,少不了的就是一个存储文章的文件服务器,包括存储一些图片之类的静态资源。例如数据库的数据文件的配置文件和文件服务器目录。 前言 这是一次完整的项目实践,Angular页面+Springboot接口+MySQL都通过Dockerfile打包成docker镜像,通过docker-compose做统一编排。目的是实现整个项目产品的轻量级和灵活性,在将各个模块的镜像...

    CODING 评论0 收藏0
  • Spring Boot 《一》开发一个“HelloWorld”的 web 应用

    摘要:一概括,如果使用开发一个的应用创建一个项目并且导入相关包。创建一个编写一个控制类需要一个部署应用的服务器如,特点设计目的是用来简化新应用的初始搭建以及开发过程。启动器可以和位于同一个包下,或者位于的上一级包中,但是不能放到的平级以及子包下。 一,Spring Boot 介绍 Spring Boot不是一个新的框架,默认配置了多种框架使用方式,使用SpringBoot很容易创建一个独立运...

    chaosx110 评论0 收藏0
  • spring boot - 收藏集 - 掘金

    摘要:引入了新的环境和概要信息,是一种更揭秘与实战六消息队列篇掘金本文,讲解如何集成,实现消息队列。博客地址揭秘与实战二数据缓存篇掘金本文,讲解如何集成,实现缓存。 Spring Boot 揭秘与实战(九) 应用监控篇 - HTTP 健康监控 - 掘金Health 信息是从 ApplicationContext 中所有的 HealthIndicator 的 Bean 中收集的, Spring...

    rollback 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<