Skip to content

Commit

Permalink
Merge pull request #44 from JoshuaYin/master
Browse files Browse the repository at this point in the history
2.6.0
  • Loading branch information
joshua.yin authored Aug 7, 2020
2 parents 8d7722b + 2f7fbb5 commit 4a0c91e
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 99 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.5.2</version>
<version>latest-release-version</version>
</dependency>
```

Expand All @@ -36,7 +36,7 @@
/*
* your other dependencies
*/
implementation 'cn.ucloud.ufile:ufile-client-java:2.5.2'
implementation 'cn.ucloud.ufile:ufile-client-java:latest-release-version'
}
```

Expand Down
2 changes: 1 addition & 1 deletion ufile-sample-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.5.2</version>
<version>2.6.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class PutObjectSample {
private static final String TAG = "PutObjectSample";
private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

public static void main(String[] args) {
public static void main(String[] args) throws FileNotFoundException {
InputStream is = new ByteArrayInputStream(new byte[]{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07});
// 如果上传File,则文件的MimeType可以使用MimeTypeUtil.getMimeType(File)来获取,MimeTypeUtil可能支持的type类型不全,用户可以按需自行填写
File file = new File("");
String keyName = "";
String bucketName = "";
String mimeType = MimeTypeUtil.getMimeType(keyName);
putStream(is, mimeType, keyName, bucketName);
putStream(new FileInputStream(file), file.length(), mimeType, keyName, bucketName);
}

public static void putFile(File file, String mimeType, String nameAs, String toBucket) {
Expand Down Expand Up @@ -176,7 +176,7 @@ public void onError(Request request, ApiError error, UfileErrorBean response) {
});
}

public static void putStream(InputStream stream, String mimeType, String nameAs, String toBucket) {
public static void putStream(InputStream stream, long contentLength, String mimeType, String nameAs, String toBucket) {
try {
/**
* 上传回调策略
Expand All @@ -187,7 +187,7 @@ public static void putStream(InputStream stream, String mimeType, String nameAs,
.addCallbackBody(new PolicyParam("key", "value"))
.build();
PutObjectResultBean response = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.putObject(stream, mimeType)
.putObject(stream, contentLength, mimeType)
.nameAs(nameAs)
.toBucket(toBucket)
/**
Expand Down Expand Up @@ -244,7 +244,7 @@ public void onProgress(long bytesWritten, long contentLength) {
}
}

public static void putStreamAsync(InputStream stream, String mimeType, String nameAs, String toBucket) throws UfileClientException {
public static void putStreamAsync(InputStream stream, long contentLength, String mimeType, String nameAs, String toBucket) throws UfileClientException {
/**
* 上传回调策略
* 必须填写回调接口url(目前仅支持http,不支持https),可选填回调参数,回调参数请自行决定是否需要urlencode
Expand All @@ -254,7 +254,7 @@ public static void putStreamAsync(InputStream stream, String mimeType, String na
.addCallbackBody(new PolicyParam("key", "value"))
.build();
UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
.putObject(stream, mimeType)
.putObject(stream, contentLength, mimeType)
.nameAs(nameAs)
.toBucket(toBucket)
/**
Expand Down
11 changes: 10 additions & 1 deletion ufile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile</artifactId>
<packaging>pom</packaging>
<version>2.5.2</version>
<version>2.6.0</version>

<modules>
<module>ufile-core</module>
Expand Down Expand Up @@ -95,6 +95,15 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${plugin.maven.deploy.version}</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Jar包 -->
<plugin>
Expand Down
Binary file modified ufile/ufile-client-java/apidocs.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions ufile/ufile-client-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<artifactId>ufile</artifactId>
<groupId>cn.ucloud.ufile</groupId>
<version>2.5.2</version>
<version>2.6.0</version>
</parent>

<artifactId>ufile-client-java</artifactId>
<version>2.5.2</version>
<version>2.6.0</version>

<dependencies>
<dependency>
Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-core</artifactId>
<version>2.5.2</version>
<version>2.6.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ public Config setHttpClientConfig(HttpClient.Config httpClientConfig) {
}
}

/**
* 设置HttpClient异步线程池
*
* @param executorService HttpClient异步线程池
*/
@Deprecated
public static void setExecutorService(ExecutorService executorService) {
}

/**
* 配置并构建UfileClient
* 必须在调用bucket()和object()之前调用,否则无效
Expand All @@ -103,18 +94,6 @@ public synchronized static UfileClient configure(Config config) {
return createClient(config);
}

/**
* 获取HttpClient异步线程池
*
* @return HttpClient异步线程池
*/
@Deprecated
public static ExecutorService getExecutorService() {
if (mInstance == null)
return null;
return mInstance.httpClient.getExecutorService();
}

/**
* 获取UfileClient配置项
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ public DownloadStreamBean parseHttpResponse(Response response) throws UfileIOExc
long contentLength = response.body().contentLength();
result.setContentLength(contentLength);
result.setContentType(response.header("Content-Type"));
result.setContentMD5(response.header("Content-MD5"));
result.seteTag(response.header("ETag") == null ?
null : response.header("ETag").replace("\"", ""));

Expand All @@ -213,7 +214,6 @@ public DownloadStreamBean parseHttpResponse(Response response) throws UfileIOExc
InputStream is = response.body().byteStream();
if (outputStream == null) {
result.setInputStream(is);
FileUtil.close(response.body());
} else {
if (onProgressListener != null) {
switch (progressConfig.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ public PutFileApi putObject(File file, String mimeType) {
/**
* put 流
*
* @param inputStream 输入流
* @param mimeType mime类型
* @param inputStream 输入流
* @param contentLength 输入流的数据长度,bytesLength
* @param mimeType mime类型
* @return {@link PutStreamApi}
*/
public PutStreamApi putObject(InputStream inputStream, String mimeType) {
public PutStreamApi putObject(InputStream inputStream, long contentLength, String mimeType) {
return new PutStreamApi(authorizer, objectConfig, client.getHttpClient())
.from(inputStream, mimeType);
.from(inputStream, contentLength, mimeType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ public class PutStreamApi extends UfileObjectApi<PutObjectResultBean> {

private ProgressConfig progressConfig;

private ByteArrayOutputStream cacheOutputStream;
/**
* 上传流的数据长度
*/
private long contentLength = 0L;

/**
* 上传流的md5,若withVerifyMd5,则必须填写
*/
private String contentMD5;

/**
* 流写入的buffer大小,Default = 256 KB
Expand Down Expand Up @@ -117,12 +125,14 @@ public PutStreamApi nameAs(String keyName) {
/**
* 设置要上传的流和MIME类型
*
* @param inputStream 输入流
* @param mimeType MIME类型
* @param inputStream 输入流
* @param contentLength 输入流的数据长度,bytesLength
* @param mimeType MIME类型
* @return {@link PutStreamApi}
*/
public PutStreamApi from(InputStream inputStream, String mimeType) {
public PutStreamApi from(InputStream inputStream, long contentLength, String mimeType) {
this.inputStream = inputStream;
this.contentLength = contentLength;
this.mimeType = mimeType;
this.mediaType = MediaType.parse(mimeType);
return this;
Expand Down Expand Up @@ -150,15 +160,16 @@ public PutStreamApi withIopCmd(String iopCmd) {
return this;
}


/**
* 设置是否需要MD5校验
*
* @param isVerifyMd5 是否校验MD5
* @param contentMD5 数据的MD5值,若 isVerifyMd5 = true,则必须填写非空的 contentMD5
* @return {@link PutStreamApi}
*/
public PutStreamApi withVerifyMd5(boolean isVerifyMd5) {
public PutStreamApi withVerifyMd5(boolean isVerifyMd5, String contentMD5) {
this.isVerifyMd5 = isVerifyMd5;
this.contentMD5 = contentMD5;
return this;
}

Expand Down Expand Up @@ -259,7 +270,6 @@ protected void prepareData() throws UfileClientException {
parameterValidat();

String contentType = mediaType.toString();
String contentMD5 = "";
String date = dateFormat.format(new Date(System.currentTimeMillis()));

String url = generateFinalHost(bucketName, keyName);
Expand All @@ -274,7 +284,7 @@ protected void prepareData() throws UfileClientException {
.addHeader("Date", date)
.mediaType(mediaType);

builder.addHeader("Content-Length", String.valueOf(inputStream.available()));
builder.addHeader("Content-Length", String.valueOf(contentLength));

if (storageType != null)
builder.addHeader("X-Ufile-Storage-Class", storageType);
Expand All @@ -292,15 +302,10 @@ protected void prepareData() throws UfileClientException {
}
}

if (contentMD5 == null)
contentMD5 = "";
if (isVerifyMd5) {
try {
backupStream();
contentMD5 = HexFormatter.formatByteArray2HexString(Encoder.md5(new ByteArrayInputStream(cacheOutputStream.toByteArray())), false);
builder.addHeader("Content-MD5", contentMD5);
inputStream = new ByteArrayInputStream(cacheOutputStream.toByteArray());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
builder.addHeader("Content-MD5", contentMD5);
}

String authorization = authorizer.authorization((ObjectOptAuthParam) new ObjectOptAuthParam(HttpMethod.PUT, bucketName, keyName,
Expand All @@ -311,11 +316,7 @@ protected void prepareData() throws UfileClientException {
builder.params(inputStream);
builder.setProgressConfig(progressConfig);

FileUtil.close(cacheOutputStream);

call = builder.build(httpClient.getOkHttpClient());
} catch (IOException e) {
throw new UfileIOException(e.getMessage());
} catch (UfileClientException e) {
throw e;
}
Expand Down Expand Up @@ -344,23 +345,6 @@ protected void parameterValidat() throws UfileParamException {
"The required param 'bucketName' can not be null or empty");
}

private void backupStream() {
cacheOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[128 << 10];
int len = 0;
try {
while ((len = inputStream.read(buff)) > 0) {
cacheOutputStream.write(buff, 0, len);
}

cacheOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
FileUtil.close(inputStream);
}
}

private OnProgressListener onProgressListener;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ public class DownloadStreamBean extends BaseResponseBean {
private String contentType;
@SerializedName("Content-Length")
private long contentLength;
@SerializedName("Content-MD5")
private String contentMD5;
@SerializedName("InputStream")
private InputStream inputStream;

private transient Map<String,String> metadatas;
private transient Map<String, String> metadatas;

public String getContentType() {
return contentType;
Expand Down Expand Up @@ -61,11 +63,20 @@ public DownloadStreamBean setInputStream(InputStream inputStream) {
return this;
}

public Map<String,String> getMetadatas() {
public String getContentMD5() {
return contentMD5;
}

public DownloadStreamBean setContentMD5(String contentMD5) {
this.contentMD5 = contentMD5;
return this;
}

public Map<String, String> getMetadatas() {
return metadatas;
}

public void setMetadatas(Map<String,String> metadatas) {
public void setMetadatas(Map<String, String> metadatas) {
this.metadatas = metadatas;
}

Expand Down
Binary file modified ufile/ufile-core/apidocs.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions ufile/ufile-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile</artifactId>
<version>2.5.2</version>
<version>2.6.0</version>
</parent>

<artifactId>ufile-core</artifactId>
<version>2.5.2</version>
<version>2.6.0</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @date: 2018/11/22 15:00
*/
public class UfileConstants {
public static final String SDK_VERSION = "2.5.2";
public static final String SDK_VERSION = "2.6.0";
/**
* 默认分片大小(4MB)
*/
Expand Down
Loading

0 comments on commit 4a0c91e

Please sign in to comment.