阿里云oss存储
access key
LTAI5t9Q2c1iFfmBwmsR9MJk
Gg0EPFTcJlAnphiGa1WJm6cI39nfCa
后端配置
1 service模块下新建子模块service-alioss
2 配置启动类
这里启动会报一个错
提示没有数据源,因为我们没有数据库的相关操作,所以配置文件中没有写数据库的一些配置
3 配置文件
server.port=8002
# 服务名
spring.application.name=service-alioss
# 环境设置
spring.profiles.active=dev
# 阿里云 OSS
# 不同的服务器,地址不同
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
# 千万不要加空格
aliyun.oss.file.keyid=LTAI5t9Q2c1iFfmBwmsR9MJk
aliyun.oss.file.keysecreat=Gg0EPFTcJlAnphiGa1WJm6cI39nfCa
# bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=edu-zjx-0001
创建常量工具类ConstantPropertiesUtils.java
@Value() 表示获取到配置文件的值赋给下方定义的变量
实现 InitializingBean 类后重写afterProperties()方法,该方法默认在spring加载该类后执行
所以可以用于静态常量的初始化
4 文件流上传方法
@Override
public String uploadFileAvatar(MultipartFile file) {
// 获取阿里云存储相关常量
String endpoint = ConstantPropertiesUtils.END_POINT;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
// 返回的url地址
String uploadUrl = null;
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 获取上传文件输入流
InputStream inputStream = file.getInputStream();
// 获取文件名称
String fileName = file.getOriginalFilename();
// 调用oss方法实现上传
ossClient.putObject(bucketName, fileName, inputStream);
ossClient.shutdown();
uploadUrl = "https://"+bucketName+"."+endpoint+"/"+fileName;
return uploadUrl;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
5 编写controller
6 swagger测试
7 对于相同名字的文件,会把之前上传的文件覆盖掉,为了避免这种情况的出现,我们需要对文件进行随机唯一命名,对文件根据时间进行分类。
nginx
开启nginx后,任务窗口会有两个nginx进程,这是多路复用的结果
nginx需要在cmd窗口使用命令关闭,不能关掉窗口,关掉窗口后进程仍然存在!
nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 81;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
try_files $uri $uri/ /index.html last;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
server{
listen 9001;
server_name localhost;
location ~ /eduservice/{
proxy_pass http://localhost:8001;
}
location ~ /eduoss/{
proxy_pass http://localhost:8002;
}
}
}
功能开发
添加讲师头像
easyexcel
如何存储二级分类表
特点
- java领域解析生成excel比较有名的框架有apache poi,jxl等。但是他们都存在一个非常严重的问题就是耗内存,如果并发量很大的haul一定会OOM或者频繁的full gc。
- easyexcel是阿里巴巴开源的一个excel处理框架,使用简单,节省内存。excel能大大减少占用内存的主要原因是在解析excel时没有将文件数据一次性全部加载到内存中,而是从磁盘上一行行读取数据,逐个解析。
- easyexcel采用一行一行的解析模式,并将一行的解析结果以观察者模式通知处理(AnalysisEventListener),配合监听器使用
使用
1 引入依赖,注意版本要匹配
<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
<!--xls-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!--xlsx-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
</dependencies>
2 实现写操作
创建实体类
@Data
public class DemoData {
//设置表头名字
@ExcelProperty("学生编号")
private int sno;
@ExcelProperty("学生姓名")
private String sname;
}
public static List<DemoData> data(){
List<DemoData> list = new ArrayList<DemoData>();
for(int i=0;i<10;i++){
DemoData data = new DemoData();
data.setSno(i);
data.setSname("张三"+i);
list.add(data);
}
return list;
}
public static void write(){
String fileName = "E:\\subject.xlsx";
EasyExcel.write(fileName,DemoData.class).sheet("模板名字").doWrite(data());
}
效果
3 读操作需要监听器
创建实体类
@Data
@ToString
public class ReadData {
@ExcelProperty(index = 0) // index表示第几列
private int sid;
@ExcelProperty(index = 1)
private String sname;
}
监听器
public class ExcelListener extends AnalysisEventListener<ReadData> {
// 创建list集合封装最终的数据
List<ReadData> list = new ArrayList<ReadData>();
// 一行一行去读取excel内容
@Override
public void invoke(ReadData readData, AnalysisContext analysisContext) {
System.out.println("***"+readData);
list.add(readData);
}
// 读取表头信息
@Override
public void invokeHeadMap(Map<Integer,String> headMap,AnalysisContext context){
System.out.println("表头信息:"+headMap);
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}
读excel数据
public static void read(){
String fileName = "E:\\subject.xlsx";
EasyExcel.read(fileName,ReadData.class,new ExcelListener()).sheet().doRead();
}