Skip to content

Commit

Permalink
七牛云上传图片
Browse files Browse the repository at this point in the history
  • Loading branch information
K-ON-RMTY committed Feb 17, 2021
1 parent 25e05bf commit 3cb359d
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 5 deletions.
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,43 @@
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<!--七牛云依赖-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 使用@ConfigurationProperties(prefix="upload")需要配置
https://www.choupangxia.com/2020/03/24/configuration-annotation-processor-not-found-in-classpath/-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.design.spicsback.configuration;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix="upload")
@Data
public class UploadProperties {
// 域名
private String domain;
private String accessKey;
private String secretKey;
// 存储空间名
private String bucket;
}
18 changes: 14 additions & 4 deletions src/main/java/com/design/spicsback/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.design.spicsback.controller;

import com.design.spicsback.entity.Information;
import com.design.spicsback.entity.User;
import com.design.spicsback.service.UserService;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -28,10 +29,19 @@ public class UserController {
* @param password 密码
* @return 用户数据
*/
@GetMapping("login")
public User selectOne(String username, String password) {
return this.userService.queryByUserNameAndPwd(username, password);

@PostMapping("login")
public Information<User> selectOne(String username, String password) {
System.out.println(username);
User user = this.userService.queryByUserNameAndPwd(username, password);
if (user == null){
// 返回错误信息
return Information.error(200,"用户名或密码错误");
}else {
// 敏感信息不传回
user.setPassword("");
user.setId(0);
return Information.success(200,"登录成功",user);
}
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/design/spicsback/entity/Information.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.design.spicsback.entity;

import lombok.Builder;
import lombok.Data;

// 返回信息类
@Data
@Builder
public class Information<T> {
//对象
private T data;
//描述
private String msg;
//状态码
private Integer status;

/**
* @param status 错误代码
* @param msg 描述信息
* @return 错误信息
*/
public static Information error(Integer status,String msg){
return Information.builder().status(status).msg(msg).build();
}

/**
*
* @return 返回简单成功信息
*/
public static Information success(String msg){
return Information.builder().status(200).msg(msg+"成功").build();
}

/**
*
* @param status 错误代码
* @param msg 描述
* @param data 数据
* @param <T> 范型
* @return 成功信息
*/
public static <T>Information<T> success(Integer status,String msg,T data){
return Information.<T>builder().status(status).msg(msg).data(data).build();
}

}
57 changes: 57 additions & 0 deletions src/main/java/com/design/spicsback/utils/UploadFileQiniu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.design.spicsback.utils;

import com.design.spicsback.configuration.UploadProperties;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
/*
参考:
https://www.cnblogs.com/Johnson-lin/p/12059067.html
其访问的链接主要是将空间绑定的域名(可以是七牛空间的默认域名或者是绑定的自定义域名)拼接上空间里面的文件名即可访问
* */
public class UploadFileQiniu {
private UploadProperties properties;
// 构建一个带指定Region对象的配置类
private Configuration config = new Configuration(Region.region2());
// 默认请求协议为 https,可设置为http请求
// config.useHttpsDomains = false
private UploadManager uploadManager = new UploadManager(config);
public UploadFileQiniu(UploadProperties properties) {
this.properties = properties;
}

public String uploadFile(MultipartFile file) {
Auth auth = Auth.create(properties.getAccessKey(),properties.getSecretKey());
String token = auth.uploadToken(properties.getBucket());
try {
String originalFilename = file.getOriginalFilename();
// 文件后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
// 设置文件名称,保证唯一
String fileKey = UUID.randomUUID().toString()+suffix;
// 上传结果
Response response = uploadManager.put(file.getInputStream(), fileKey, token, null, null);
// 解析上传结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
// 返回图片url
return properties.getDomain()+fileKey;
} catch (QiniuException e) {
Response r = e.response;
System.out.println(r.toString());
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ spring.datasource.tomcat.commit-on-return=true
spring.datasource.url=jdbc:mysql:https:///spics?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.servlet.multipart.max-file-size=3MB
spring.servlet.multipart.max-request-size=10MB
spring.devtools.restart.enabled=true
spring.devtools.restart.enabled=true
upload.qiniu.domain=qom3or861.hn-bkt.clouddn.com
upload.qiniu.access-key=NNZ5NPzNtf-SEvs6V_XRGtPRG-V7L0pquHkmzkSq
upload.qiniu.secret-key=NNZ5NPzNtf-SEvs6V_XRGtPRG-V7L0pquHkmzkSq
upload.qiniu.bucket=savepics

0 comments on commit 3cb359d

Please sign in to comment.