util :
package com.jeeplus.modules.tools.utils;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
/**
* OSS方法测试类
* oss操作 请先开启
* @author ZD
*
*/
public class FileUploadUtils {
// 创建OSS客户端
private static OSSClient client;
// 设置连接
private static String bucket_name = "test";
private static String endpoint = "oss-cn-shanghai.aliyuncs.com";
private static String accessKeyId = "testaccessKeyId";
private static String accessKeySecret = "testaccessKeySecret";
/**
*
* 配置Conf连接属性
*
* @return conf
*/
public static ClientConfiguration conf() {
// 创建ClientConfiguration实例
ClientConfiguration conf = new ClientConfiguration();
// 设置OSSClient使用的最大连接数,默认1024
conf.setMaxConnections(200);
// 设置请求超时时间,默认50秒
conf.setSocketTimeout(10000);
// 设置失败请求重试次数,默认3次
conf.setMaxErrorRetry(3);
return conf;
}
/**
*
* 开启OSS
*
* @return boolean
*/
public static boolean open() {
if (client == null) {
client = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf());
}
return true;
}
/**
*
* 关闭OSS
*
* @return boolean
*/
public static boolean close() {
if (client != null) {
client.shutdown();
}
return true;
}
/**
*
* 列出当前Bucket下的所有文件
*/
public static void listObjects() {
ObjectListing objectListing = client.listObjects(bucket_name);
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")");
}
}
/**
*
* 生成URL
*/
public static void makeUrl(String key) {
try {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket_name, key,
HttpMethod.GET);
// 设置URL过期时间为1小时
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
request.setExpiration(expiration);
// 生成URL签名(HTTP GET请求)
URL signedUrl = client.generatePresignedUrl(request);
System.out.println("signed url for getObject: " + signedUrl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* 上传文件
*/
public static void upload(String filePath, String key) {
try {
// 创建上传Object的Metadata
ObjectMetadata meta = new ObjectMetadata();
// meta.setContentType("image/jpg");
meta.setContentType("text/html");
// 上传本地文件
System.out.println("=============================================================" + filePath);
PutObjectResult rs = client.putObject(bucket_name, key, new File(filePath), meta);
System.out.println("上传成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上传失败");
}
}
public static void upload2(File file, String key) {
try {
// 创建上传Object的Metadata
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("image/jpg"); //图片类型,网页类型为 text/html
// 上传本地文件
PutObjectResult rs = client.putObject(bucket_name, key, file, meta);
System.out.println("上传成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上传失败");
}
}
/**
* 上传文件流
*/
public static void OssUpload(String content, String fileName, String key) {
try {
client.putObject(bucket_name, key, new ByteArrayInputStream(content.getBytes()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 下载文件到本地
*/
public static void download(String srcPath, String destPath) {
try {
client.getObject(new GetObjectRequest(bucket_name, srcPath), new File(destPath));
System.out.println("下载成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("下载失败");
}
}
/**
* 流式下载
*/
public InputStream ossDownload() {
OSSObject ossObject = client.getObject(bucket_name, "UAT/upload_test_001.xml");
InputStream is = new DataInputStream(ossObject.getObjectContent());
return is;
}
/**
*
* Oss删除 文件
*
* @param name
* 路径+文件名
*/
public static boolean deleteFile(String name) {
try {
// open();
client.deleteObject(bucket_name, name);
System.out.println("========================================删除成功");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}