在后台上传资源文件过程中,一般都是上传到自己的远程 Linux 服务器上,如果自己不想搭建自己的静态资服务器,我们可以使用七牛云服务器,在这里我们不讨论七牛云的优劣,只分享讨论它的使用方法即可。
1、注册自己的七牛账号,并且实名认证之后,可以使用它的对象存储功能
进入七牛官网 :www.qiniu.com 注册自己的账号
管理控制台 --> 选择对象存储 --> 新建存储空间 --> 填写表单 --> 创建完成
2、到自己的个人中心查看 AccessKey/SecretKey 并在后台代码配置这两个参数
3、使用七牛 java 的 sdk
查看下载地址:https://developer.qiniu.com/kodo/sdk/1239/java
<dependencies>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.11</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
4、完成以上步骤,那就直接上代码
package com.thinkgem.jeesite.modules.zsgt.utils;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.file.Paths;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
/**
* 七牛云图片上传
*
* @author hp
*/
public class QiniuUpload {
/**
* 七牛云图片路径
*/
public static final String QINIU_IMG_PATH = "xxx";
// ...生成上传凭证,然后准备上传
private static String accessKey = "xxx";
private static String secretKey = "xxx";
private static String bucket = "xxx";
// 构造一个带指定Zone对象的配置类
private static Configuration cfg = new Configuration(Zone.zone2());
// 上传文件到七牛云
public String upload(File file) {
String fileName = "";
UploadManager uploadManager = new UploadManager(cfg);
// 默认不指定key的情况下,以文件内容的hash值作为文件名
String key = new Date().getTime() + "_" + file.getName();
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(file, key, upToken);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
fileName = putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
// ignore
}
}
return fileName;
}
// 删除文件
public boolean delete(String hasName) {
boolean deleteType = false;
Auth auth = Auth.create(accessKey, secretKey);
BucketManager bucketManager = new BucketManager(auth, cfg);
try {
bucketManager.delete(bucket, hasName);
deleteType = true;
} catch (QiniuException ex) {
// 如果遇到异常,说明删除失败
System.err.println(ex.code());
System.err.println(ex.response.toString());
}
return deleteType;
}
public static String uploadBlock(File file) {
String fileName = "";
// 默认不指定key的情况下,以文件内容的hash值作为文件名
String key = new Date().getTime() + "_" + file.getName();
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), bucket).toString();
try {
// 设置断点续传文件进度保存目录
FileRecorder fileRecorder = new FileRecorder(localTempDir);
UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
try {
Response response = uploadManager.put(file, key, upToken);
// 解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
fileName = putRet.key;
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
// ignore
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
return fileName;
}
public static String upload(HttpServletRequest request, MultipartFile img){
String path = request.getSession().getServletContext().getRealPath("/file/upload/");// 生成一个目录
File file;
try {
file = FileUtil.getFile(path, img);
} catch (Exception e) {
throw new RuntimeException("上传文件失败");
}
String fileName = QiniuUpload.uploadBlock(file);
return QiniuUpload.QINIU_IMG_PATH + URLEncoder.encode(fileName);// 访问路径
}
}
使用你的 AccessKey/SecretKey 然后调用 QiniuUpload.upload(request, Img) 即可实现上传操作,更多上传操作请看 java 七牛 sdk 文档 ,使用七牛封装好的方法会更加简单。
水平有限,若有问题请留言交流!
互相学习,共同进步 :) 转载请注明出处谢谢!
来源:oschina
链接:https://my.oschina.net/u/3198481/blog/2245465