Skip to content

Commit c10560d

Browse files
committed
Merge pull request #95 from simon-liubin/feature/mimiType
添加mimeType支持.设置extra的mimeType时使用次值,不设置按以前方式处理
2 parents 22120ef + 4ce7d57 commit c10560d

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/main/java/com/qiniu/api/io/IoApi.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package com.qiniu.api.io;
22

33
import java.io.File;
4-
import java.io.InputStream;
54
import java.io.FileInputStream;
5+
import java.io.InputStream;
6+
import java.io.UnsupportedEncodingException;
7+
import java.nio.charset.Charset;
68
import java.util.zip.CRC32;
79
import java.util.zip.CheckedInputStream;
810

911
import org.apache.http.entity.mime.MultipartEntity;
12+
import org.apache.http.entity.mime.content.AbstractContentBody;
1013
import org.apache.http.entity.mime.content.FileBody;
1114
import org.apache.http.entity.mime.content.InputStreamBody;
1215
import org.apache.http.entity.mime.content.StringBody;
16+
1317
import com.qiniu.api.config.Config;
1418
import com.qiniu.api.net.CallRet;
1519
import com.qiniu.api.net.Client;
1620

17-
import java.nio.charset.Charset;
18-
1921
public class IoApi {
2022

21-
public static final String UNDEFINED_KEY = "?";
23+
public static final String UNDEFINED_KEY = null;
2224
public static final int NO_CRC32 = 0;
2325
public static final int AUTO_CRC32 = 1;
2426
public static final int WITH_CRC32 = 2;
@@ -30,15 +32,12 @@ private static PutRet put(String uptoken, String key, File file,
3032
return new PutRet(new CallRet(400, new Exception(
3133
"File does not exist or not readable.")));
3234
}
33-
if (key == null) {
34-
key = UNDEFINED_KEY;
35-
}
3635
MultipartEntity requestEntity = new MultipartEntity();
3736
try {
3837
requestEntity.addPart("token", new StringBody(uptoken));
39-
FileBody fileBody = new FileBody(file);
38+
AbstractContentBody fileBody = buildFileBody(file, extra);
4039
requestEntity.addPart("file", fileBody);
41-
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
40+
setKey(requestEntity, key);
4241
if (extra.checkCrc != NO_CRC32) {
4342
if (extra.crc32 == 0) {
4443
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
@@ -55,13 +54,27 @@ private static PutRet put(String uptoken, String key, File file,
5554
return new PutRet(ret);
5655
}
5756

57+
private static FileBody buildFileBody(File file,PutExtra extra){
58+
if(extra.mimeType != null){
59+
return new FileBody(file, extra.mimeType);
60+
}else{
61+
return new FileBody(file);
62+
}
63+
}
64+
65+
private static void setKey(MultipartEntity requestEntity, String key) throws UnsupportedEncodingException{
66+
if(key != null){
67+
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
68+
}
69+
}
70+
5871
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
5972
MultipartEntity requestEntity = new MultipartEntity();
6073
try {
6174
requestEntity.addPart("token", new StringBody(uptoken));
62-
InputStreamBody inputBody= new InputStreamBody(reader,key);
75+
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
6376
requestEntity.addPart("file", inputBody);
64-
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
77+
setKey(requestEntity, key);
6578
if (extra.checkCrc != NO_CRC32) {
6679
if (extra.crc32 == 0) {
6780
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
@@ -78,12 +91,17 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
7891
return new PutRet(ret);
7992
}
8093

94+
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String key){
95+
if(extra.mimeType != null){
96+
return new InputStreamBody(reader, extra.mimeType, key);
97+
}else{
98+
return new InputStreamBody(reader, key);
99+
}
100+
}
101+
81102

82103
public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
83104
{
84-
if (key == null) {
85-
key = UNDEFINED_KEY;
86-
}
87105
PutRet ret = putStream(uptoken,key,reader,extra);
88106
return ret;
89107
}

0 commit comments

Comments
 (0)