-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/design/spicsback/configuration/UploadProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/design/spicsback/entity/Information.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
src/main/java/com/design/spicsback/utils/UploadFileQiniu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters