package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class UploadActivity extends Activity implements OnClickListener {
private final String TAG = "UploadActivity";
private static final String path = "/mnt/sdcard/";
private String uploadUrl = "http://192.168.1.102:8080/Android/";
private Button btnAsync, btnHttpClient, btnCommonPost;
private AsyncHttpClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(.activity_upload);
initView();
client = new AsyncHttpClient();
}
private void initView() {
btnCommonPost = (Button) findViewById(.button1);
btnHttpClient = (Button) findViewById(.button2);
btnAsync = (Button) findViewById(.button3);
(this);
(this);
(this);
}
@Override
public void onClick(View v) {
long startTime = ();
String tag = null;
try {
switch (()) {
case .button1:
upLoadByCommonPost();
tag = "CommonPost====>";
break;
case .button2:
upLoadByHttpClient4();
tag = "HttpClient====>";
break;
case .button3:
upLoadByAsyncHttpClient();
tag = "AsyncHttpClient====>";
break;
default:
break;
}
} catch (Exception e) {
();
}
(TAG, tag + "wasteTime = "
+ (() - startTime));
}
private void upLoadByCommonPost() throws IOException {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
(128 * 1024);
(true);
(true);
(false);
("POST");
("Connection", "Keep-Alive");
("Charset", "UTF-8");
("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
());
(twoHyphens + boundary + end);
("Content-Disposition: form-data; name=\"uploadfile\"; filename=\""
+ (("/") + 1) + "\"" + end);
(end);
FileInputStream fis = new FileInputStream(path);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = (buffer)) != -1) {
(buffer, 0, count);
}
();
(end);
(twoHyphens + boundary + twoHyphens + end);
();
InputStream is = ();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = ();
(TAG, result);
();
();
}
private void upLoadByHttpClient4() throws ClientProtocolException,
IOException {
HttpClient httpclient = new DefaultHttpClient();
().setParameter(
CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(uploadUrl);
File file = new File(path);
MultipartEntity entity = new MultipartEntity();
FileBody fileBody = new FileBody(file);
("uploadfile", fileBody);
(entity);
HttpResponse response = (httppost);
HttpEntity resEntity = ();
if (resEntity != null) {
(TAG, (resEntity));
}
if (resEntity != null) {
();
}
().shutdown();
}
private void upLoadByAsyncHttpClient() throws FileNotFoundException {
RequestParams params = new RequestParams();
("uploadfile", new File(path));
(uploadUrl, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int arg0, String arg1) {
super.onSuccess(arg0, arg1);
(TAG, arg1);
}
});
}
}