11package com .qiniu .api .io ;
22
33import java .io .File ;
4- import java .io .InputStream ;
54import java .io .FileInputStream ;
5+ import java .io .InputStream ;
6+ import java .io .UnsupportedEncodingException ;
7+ import java .nio .charset .Charset ;
68import java .util .zip .CRC32 ;
79import java .util .zip .CheckedInputStream ;
810
911import org .apache .http .entity .mime .MultipartEntity ;
12+ import org .apache .http .entity .mime .content .AbstractContentBody ;
1013import org .apache .http .entity .mime .content .FileBody ;
1114import org .apache .http .entity .mime .content .InputStreamBody ;
1215import org .apache .http .entity .mime .content .StringBody ;
16+
1317import com .qiniu .api .config .Config ;
1418import com .qiniu .api .net .CallRet ;
1519import com .qiniu .api .net .Client ;
1620
17- import java .nio .charset .Charset ;
18-
1921public 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