1、maven 中导入jar包,需要你添加到私服中,否则下载不下来(可以去官网下载)
cn.ucloud
ufile_sdk
1.0.1
2、添加ufile.properties配置文件
ufile.bucketName=gs-qn-cpt
ufile.UCloudPublicKey=mSKsidaQtg/g51+VZ73Myrshg4kis6Ml5zfGG40kdEvbQvh1
ufile.UCloudPrivateKey=927da985e96eed5bd6d51bc1f1e1434809b17e81
ufile.ProxySuffix=.ufile.ucloud.cn
ufile.DownloadProxySuffix=.ufile.ucloud.com.cn
3、写ufile的工具类
package com.qn.api.util;
import cn.ucloud.ufile.UFileClient;
import cn.ucloud.ufile.UFileConfig;
import cn.ucloud.ufile.UFileRequest;
import cn.ucloud.ufile.UFileResponse;
import cn.ucloud.ufile.sender.DeleteSender;
import cn.ucloud.ufile.sender.GetSender;
import cn.ucloud.ufile.sender.PutSender;
import org.apache.http.Header;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import java.io.*;
/**
* ufile操作工具
* Created by win 10 on 2018/4/11.
*/
public class UfileUtil {
private static final Logger logger = LoggerFactory.getLogger(UfileUtil.class);
private static final ResourceUtil resourceUtil=ResourceUtil.getResource("ufile");
// 文件分类地址,可以自定义
private static final String bucketName = resourceUtil.getValue("ufile.bucketName");
private static final String UCloudPublicKey=resourceUtil.getValue("ufile.UCloudPublicKey");
private static final String UCloudPrivateKey=resourceUtil.getValue("ufile.UCloudPrivateKey");
private static final String ProxySuffix=resourceUtil.getValue("ufile.ProxySuffix");
private static final String DownloadProxySuffix=resourceUtil.getValue("ufile.DownloadProxySuffix");
public void init() {
UFileConfig.getInstance().setUcloudPublicKey(UCloudPublicKey);
UFileConfig.getInstance().setUcloudPrivateKey(UCloudPrivateKey);
UFileConfig.getInstance().setProxySuffix(ProxySuffix);
UFileConfig.getInstance().setDownloadProxySuffix(DownloadProxySuffix);
}
/**
* 上传文件
* @param relativeDir 文件上传至ufile的地址
* @param fileName 文件上传到ufile的新名称,可以和旧名称不同
* @param contentType 自定义文件类型
* @param inputStream
* @param contentLength
* @throws IOException
*/
public void uploadFile(String relativeDir, String fileName, String
contentType, InputStream inputStream, long contentLength) throws IOException {
UFileRequest request = new UFileRequest();
request.setBucketName(bucketName);
String relativeUrl = relativeDir + "/" + fileName;
request.setKey(relativeUrl);
request.setContentType(contentType);
request.setInputStream(inputStream);
request.setContentLength(contentLength);
UFileClient ufileClient = new UFileClient();
putFile(ufileClient, request);
ufileClient.shutdown();
inputStream.close();
}
public void downloadFile(String relativeUrl, String contentType, OutputStream
outputStream) {
UFileRequest request = new UFileRequest();
request.setBucketName(bucketName);
request.setKey(relativeUrl);
UFileClient ufileClient = null;
try {
ufileClient = new UFileClient();
getFile(ufileClient, request, outputStream);
}
catch (Exception e) {
logger.error("读取回执发生异常,relativeUrl={},{}", relativeUrl, e);
}
finally {
ufileClient.shutdown();
try {
outputStream.close();
}
catch (IOException e) {
logger.error("", e);
}
}
}
public void deleteFile(String relativeUrl) {
UFileRequest request = new UFileRequest();
request.setBucketName(bucketName);
request.setKey(relativeUrl);
UFileClient ufileClient = null;
try {
ufileClient = new UFileClient();
deleteFile(ufileClient, request);
}
finally {
ufileClient.shutdown();
}
}
private void putFile(UFileClient ufileClient, UFileRequest request) throws
IOException {
PutSender sender = new PutSender();
sender.makeAuth(ufileClient, request);
UFileResponse response = sender.send(ufileClient, request);
if (response != null) {
InputStream inputStream = response.getContent();
if (inputStream != null) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(inputStream));
String s = "";
while ((s = reader.readLine()) != null) {
logger.info(s);
}
inputStream.close();
}
}
}
private void getFile(UFileClient ufileClient, UFileRequest request,
OutputStream outputStream) {
GetSender sender = new GetSender();
sender.makeAuth(ufileClient, request);
UFileResponse response = sender.send(ufileClient, request);
if (response != null) {
if (response.getStatusLine().getStatusCode() != 200 && response.getContent()
!= null) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(response.getContent()));
String input;
while ((input = br.readLine()) != null) {
logger.info(input);
}
}
catch (IOException e) {
logger.error("", e);
}
finally {
if (response.getContent() != null) {
try {
response.getContent().close();
}
catch (IOException e) {
logger.error("", e);
}
}
}
}
else {
InputStream inputStream = null;
try {
inputStream = response.getContent();
int bufSize = 1024 * 4;
byte[] buffer = new byte[bufSize];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, bytesRead);
}
}
catch (IOException e) {
logger.error("", e);
}
finally {
if (inputStream != null) {
try {
inputStream.close();
}
catch (IOException e) {
logger.error("", e);
}
}
if (outputStream != null) {
try {
outputStream.close();
}
catch (IOException e) {
logger.error("", e);
}
}
}
}
}
}
private void deleteFile(UFileClient ufileClient, UFileRequest request) {
DeleteSender sender = new DeleteSender();
sender.makeAuth(ufileClient, request);
UFileResponse response = sender.send(ufileClient, request);
if (response != null) {
System.out.println("status line: " + response.getStatusLine());
Header[] headers = response.getHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println("header " + headers[i].getName() + " : " +
headers[i].getValue());
}
System.out.println("body length: " + response.getContentLength());
if (response.getContent() != null) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(response.getContent()));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input);
}
}
catch (IOException e) {
logger.error("", e);
}
finally {
if (br != null) {
try {
br.close();
}
catch (IOException e) {
logger.error("", e);
}
}
}
}
}
}
}