package com.example.multipartvolley;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public
class
MultipartActivity
extends
Activity {
private
final
Context context = this;
private
final
String twoHyphens =
"--"
;
private
final
String lineEnd =
"\r\n"
;
private
final
String boundary =
"apiclient-"
+ System.currentTimeMillis();
private
final
String mimeType =
"multipart/form-data;boundary="
+ boundary;
private
byte[] multipartBody;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multipart);
byte[] fileData1 = getFileDataFromDrawable(context, R.drawable.ic_action_android);
byte[] fileData2 = getFileDataFromDrawable(context, R.drawable.ic_action_book);
ByteArrayOutputStream bos =
new
ByteArrayOutputStream();
DataOutputStream dos =
new
DataOutputStream(bos);
try
{
buildPart(dos, fileData1,
"ic_action_android.png"
);
buildPart(dos, fileData2,
"ic_action_book.png"
);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
multipartBody = bos.toByteArray();
}
catch
(IOException e) {
e.printStackTrace();
}
String url =
"http://192.168.1.100/api/postfile"
;
MultipartRequest multipartRequest =
new
MultipartRequest(url, null, mimeType, multipartBody,
new
Response.Listener<NetworkResponse>() {
@Override
public
void onResponse(NetworkResponse response) {
Toast.makeText(context,
"Upload successfully!"
, Toast.LENGTH_SHORT).show();
}
},
new
Response.ErrorListener() {
@Override
public
void onErrorResponse(VolleyError error) {
Toast.makeText(context,
"Upload failed!\r\n"
+ error.toString(), Toast.LENGTH_SHORT).show();
}
});
VolleySingleton.getInstance(context).addToRequestQueue(multipartRequest);
}
@Override
public
boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_multipart, menu);
return
true;
}
@Override
public
boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if
(id == R.id.action_settings) {
return
true;
}
return
super.onOptionsItemSelected(item);
}
private
void buildPart(DataOutputStream dataOutputStream, byte[] fileData, String fileName) throws IOException {
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes(
"Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""
+ fileName +
"\""
+ lineEnd);
dataOutputStream.writeBytes(lineEnd);
ByteArrayInputStream fileInputStream =
new
ByteArrayInputStream(fileData);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024 * 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer =
new
byte[bufferSize];
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while
(bytesRead > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
}
private
byte[] getFileDataFromDrawable(Context context, int id) {
Drawable drawable = ContextCompat.getDrawable(context, id);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
ByteArrayOutputStream byteArrayOutputStream =
new
ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
return
byteArrayOutputStream.toByteArray();
}
}