目录正文基础环境创建项目添加Rest api接口功能(提供上传服务)启动服务,测试API接口可用性增加下载文件支持文件大小设置打包文件部署正文 今天我们尝试Spring Boot整合
今天我们尝试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
<?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开发存储服务器</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>
package com.edurt.sli.sliss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootStorageIntegration {
public static void main(String[] args) {
SpringApplication.run(SpringBootStorageIntegration.class, args);
}
}
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;
@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 + "成功";
}
}
package com.edurt.sli.sliss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@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
返回结果
[]
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.*;
@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
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;
@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
以上就是SpringBoot开发存储服务器实现过程详解的详细内容,更多关于SpringBoot 存储服务器的资料请关注编程网其它相关文章!
--结束END--
本文标题: SpringBoot开发存储服务器实现过程详解
本文链接: https://lsjlt.com/news/174302.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0