Most of the time we have to upload our file on server from our Application but when we try to find a way we got all the different ways to do so that make us more confused. Some says to use some library and some says not to. So here is a method to upload a file to the server via Http Multi-part form data, it doesn’t need any external library and is quiet efficient as well.
Through it you can have a put request to send you file with extra data like in my case I have to send an additional reference tag along with it. So lets start. Here I am supposing that you have a File Adapter and you have selected the file you have to send. Basically you have the URI of the file. If you don’t please follow the link.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
public class UploadFile extends AsyncTask<Object, String, String> { String file_name = ""; @Override protected String doInBackground(Object[] params) { try { String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1024 * 1024; //todo change URL as per client ( MOST IMPORTANT ) URL url = new URL("http://webkul.com/upload"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs. connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Set HTTP method to POST. connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); FileInputStream fileInputStream; DataOutputStream outputStream; outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"reference\""+ lineEnd); outputStream.writeBytes(lineEnd); outputStream.writeBytes("my_refrence_text"); outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadFile\";filename=\"" + uri.getLastPathSegment() +"\"" + lineEnd); outputStream.writeBytes(lineEnd); fileInputStream = new FileInputStream(uri.getPath()); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) int serverResponseCode = connection.getResponseCode(); String result = null; if (serverResponseCode == 200) { StringBuilder s_buffer = new StringBuilder(); InputStream is = new BufferedInputStream(connection.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = br.readLine()) != null) { s_buffer.append(inputLine); } result = s_buffer.toString(); } fileInputStream.close(); outputStream.flush(); outputStream.close(); if (result != null) { Log.d("result_for upload", result); file_name = getDataFromInputStream(result, "file_name"); } } catch (Exception e) { e.printStackTrace(); } return file_name; } } |
So here are you. with this code you can send your file to server with extra form data.