Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.qiniu.android.storage;
  
  
 import  org.json.JSONObject;
 
 import java.io.File;
 import java.util.Map;

表单上传

通过表单,你可以将一个图片或者一个文本文件等上传到七牛服务器。这个表单 就是标准的http表单,即enctype="multipart/form-data" 格式的表单。

 
 final class FormUploader {

    
上传数据,并以指定的key保存文件

Parameters:
httpManager HTTP连接管理器
data 上传的数据
key 上传的数据保存的文件名
token 上传凭证
completionHandler 上传完成后续处理动作
options 上传时的可选参数
 
     static void upload(HttpManager httpManagerConfiguration configbyte[] dataString keyUpToken tokenfinal UpCompletionHandler completionHandler,
                        final UploadOptions options) {
         post(datanullkeytokencompletionHandleroptionshttpManagerconfig);
     }

    
上传文件,并以指定的key保存文件

Parameters:
httpManager HTTP连接管理器
file 上传的文件
key 上传的数据保存的文件名
token 上传凭证
completionHandler 上传完成后续处理动作
options 上传时的可选参数
 
     static void upload(HttpManager httpManagerConfiguration configFile fileString keyUpToken tokenUpCompletionHandler completionHandler,
                        UploadOptions options) {
         post(nullfilekeytokencompletionHandleroptionshttpManagerconfig);
     }
 
     private static void post(byte[] dataFile fileString kfinal UpToken tokenfinal UpCompletionHandler completionHandler,
                              final UploadOptions optionsInfinal HttpManager httpManagerfinal Configuration config) {
         final String key = k;
         Map<StringStringparams = new HashMap<StringString>();
         final PostArgs args = new PostArgs();
         if (k != null) {
             params.put("key"key);
             args.fileName = key;
         } else {
             args.fileName = "?";
         }
 
         params.put("token"token.token);
 
         final UploadOptions options = optionsIn != null ? optionsIn : UploadOptions.defaultOptions();
         params.putAll(options.params);
 
         if (options.checkCrc) {
             long crc = 0;
             if (file != null) {
                 try {
                     crc = Crc32.file(file);
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             } else {
                 crc = Crc32.bytes(data);
             }
             params.put("crc32""" + crc);
         }
 
         final ProgressHandler progress = new ProgressHandler() {
             @Override
             public void onProgress(int bytesWrittenint totalSize) {
                 double percent = (doublebytesWritten / (doubletotalSize;
                 if (percent > 0.95) {
                     percent = 0.95;
                 }
                 options.progressHandler.progress(keypercent);
             }
         };
 
         args.data = data;
         args.file = file;
        args.mimeType = options.mimeType;
        args.params = params;
        CompletionHandler completion = new CompletionHandler() {
            @Override
            public void complete(ResponseInfo info, JSONObject response) {
                if (info.isOK()) {
                    options.progressHandler.progress(key, 1.0);
                    completionHandler.complete(keyinforesponse);
                } else if (options.cancellationSignal.isCancelled()) {
                    ResponseInfo i = ResponseInfo.cancelled();
                    completionHandler.complete(keyinull);
                } else if (info.needRetry() || (info.isNotQiniu() && !token.hasReturnUrl())) {
                    CompletionHandler retried = new CompletionHandler() {
                        @Override
                        public void complete(ResponseInfo info, JSONObject response) {
                            if (info.isOK()) {
                                options.progressHandler.progress(key, 1.0);
                            }
                            completionHandler.complete(keyinforesponse);
                        }
                    };
                    String host = config.upHost;
                    if (info.needSwitchServer()) {
                        host = config.upHostBackup;
                    }
                    boolean forceIp = false;
                    if (info.isNotQiniu() && !token.hasReturnUrl()) {
                        forceIp = true;
                    }
                    httpManager.multipartPost(genUploadAddress(hostconfig.upPort), argsprogressretriedoptions.cancellationSignalforceIp);
                } else {
                    completionHandler.complete(keyinforesponse);
                }
            }
        };
        httpManager.multipartPost(genUploadAddress(config.upHostconfig.upPort), argsprogresscompletionoptions.cancellationSignalfalse);
    }
    private static String genUploadAddress(String hostint port) {
        return "http://" + host + ":" + port + "/";
    }
New to GrepCode? Check out our FAQ X