博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot开发存储服务器
阅读量:6225 次
发布时间:2019-06-21

本文共 12978 字,大约阅读时间需要 43 分钟。

今天我们尝试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}
${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 * 

* 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

*/@SpringBootApplicationpublic class SpringBootStorageIntegration { public static void main(String[] args) { SpringApplication.run(SpringBootStorageIntegration.class, args); }}复制代码

添加Rest API接口功能(提供上传服务)


  • 创建一个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 * 

* 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); }}复制代码

启动服务,测试API接口可用性


在编译器中直接启动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  Speed100  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 multipartspring.http.multipart.max-file-size=10MBspring.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

*/@Configurationpublic 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复制代码

源码地址


转载地址:http://oqfna.baihongyu.com/

你可能感兴趣的文章
Windows Server 2012 RDS系列:桌面虚拟化(4)
查看>>
分割超大Redis数据库例子
查看>>
apue.h源代码
查看>>
C#更改系统时间
查看>>
关于空指针NULL、野指针、通用指针
查看>>
云计算的价值
查看>>
如何选择嵌入式软件开发平台
查看>>
创建可扩展性系统-8-1
查看>>
android的第一个小程序,调用相机拍照,访问网络图片
查看>>
spark2.x由浅入深深到底系列六之RDD java api用JdbcRDD读取关系型数据库
查看>>
好未来谢华亮:AI 在教育行业中的应用
查看>>
10种排序算法总结
查看>>
Wi-Fi WPS种类
查看>>
Linux逻辑卷管理LVM学习总结备忘
查看>>
用户配置文件的类型
查看>>
syslogd日志集中管理
查看>>
使用vCenter Server在开机状态下克隆虚拟机
查看>>
网页上PNG透明图片的ie6bug
查看>>
不错的东西: AutoMapper
查看>>
docker:架构拆解
查看>>