注意:這裏的jar是okhttp-3.6.0.jar
圖片在:D:\workspace\7723gameTest\res\a.png
詳細代碼見百度雲盤:PostFile
package com.tester20170303;
import java.io.File;
import java.io.IOException;
import org.testng.annotations.Test;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class touxiangshangchuan {
public static final String TAG = "UploadHelper";
private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
private final OkHttpClient client = new OkHttpClient();
public String upload(String imageType,String uid,String mod,String sign,File file) throws Exception{
RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
/// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileName + "\""),
// RequestBody.create(MEDIA_TYPE_PNG, file))
// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"imagetype\""),
// RequestBody.create(null, imageType))
// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"uid\""),
// RequestBody.create(null, uid))
.addFormDataPart("avatar", "a.png", fileBody)
.addFormDataPart("imagetype", imageType)
.addFormDataPart("uid", uid)
.addFormDataPart("sign", sign)
.addFormDataPart("mod", mod)
.build();
Request request = new Request.Builder()
.url("http://gateway.dev.7723.com/index.php/v3/user/h")
.addHeader("app-id","7723cn_android_phone")
.addHeader("did", "12345678901234567980123456789012")
.addHeader("version","3.0.0")
.addHeader("nonce-str", "12345678901234567980123456789012")
.addHeader("time-stamp", "1478900121")
.addHeader("encypt-did", "0cb196a3547dff5ef13ea6e178f8a066055528df0d326735917f6dc696ebeec5")
.addHeader("method", "user.h")
.post(requestBody)
.build();
Response response;
try {
response = client.newCall(request).execute();
String jsonString = response.body().string();
// Log.d(TAG," upload jsonString ="+jsonString);
System.out.println(" upload jsonString ="+jsonString+"--------------------------------");
if(!response.isSuccessful()){
throw new Exception("upload error code "+response);
}else{
JSONObject jsonObject =JSONObject.fromObject(jsonString);
int errorCode = jsonObject.getInt("errorCode");
if(errorCode == 0){
//Log.d(TAG," upload data ="+jsonObject.getString("data"));
return jsonObject.getString("data");
}else {
throw new Exception("upload error code "+errorCode+",errorInfo="+jsonObject.getString("errorInfo"));
}
}
} catch (IOException e) {
//Log.d(TAG,"upload IOException ",e);
}catch (JSONException e){
//Log.d(TAG,"upload JSONException ",e);
}
return null;
}
@Test
public void test() throws Exception{
String imageType="jpg";
String uid="1";
String mod="avatar";
String sign="a";
File file=new File("D:\\workspace\\7723gameTest\\res\\a.png");
String a= upload( imageType, uid, mod,sign,file);
}
}
參考資料見:http://blog.csdn.net/android_freshman/article/details/51910937