by Bar Zohan
16. March 2011 02:42
After working for some time like 2 days on file upload to a web service from an android device, finally figured out that it's the best way to use HttpURLConnection and write the file as an output stream manually. By using the code below you can access the file uploaded from a .Net web service using Request.Files[0];
HttpFileUploader:
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class HttpFileUploader implements Runnable{
URL connectURL;
String responseString;
String fileName;
byte[] dataToServer;
HttpFileUploader(String urlString, String fileName ){
try{
connectURL = new URL(urlString);
}catch(Exception ex){
Log.i("URL FORMATION","MALFORMATED URL");
}
this.fileName = fileName;
}
FileInputStream fileInputStream = null;
public String thirdTry(){
String result = "";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="3rd";
try {
Log.e(Tag,"Mission started");
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
DataOutputStream dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag,"Headers are written");
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.e(Tag,"File is written");
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){
b.append( (char)ch );
}
result=b.toString();
dos.close();
}
catch (MalformedURLException ex) {
Log.e(Tag, "error: " + ex.getMessage(), ex);
}
catch (IOException ioe) {
Log.e(Tag, "error: " + ioe.getMessage(), ioe);
}
return result;
}
public String Go()
{
String result = "";
try {
fileInputStream = Mobile.MainActivity.openFileInput(fileName);
result = thirdTry();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return result;
}
@Override
public void run() {
Go();
}
}
Usage:
HttpFileUploader upload = new HttpFileUploader("URL HERE!", "File Path Here");
String upResult = upload.Go();