Created
March 28, 2018 10:09
-
-
Save ibhavikmakwana/fc57202715f4046737cbad88b0126a24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.io.PrintWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLConnection; | |
public class MultipartUtility { | |
private static final String TAG =MultipartUtility.class.getSimpleName() ; | |
private final String boundary; | |
private static final String LINE_FEED = "\r\n"; | |
private HttpURLConnection httpConn; | |
private String charset="UTF-8"; | |
private OutputStream outputStream; | |
private PrintWriter writer; | |
public static int statusCode; | |
public MultipartUtility(String requestURL) | |
throws IOException { | |
// creates a unique boundary based on time stamp | |
boundary = "===" + System.currentTimeMillis() + "==="; | |
URL url = new URL(requestURL); | |
httpConn = (HttpURLConnection) url.openConnection(); | |
httpConn.setUseCaches(false); | |
httpConn.setDoOutput(true); // indicates POST method | |
httpConn.setDoInput(true); | |
httpConn.setRequestProperty("Content-Type", | |
"multipart/form-data; boundary=" + boundary); | |
httpConn.setRequestProperty("User-Agent", "CodeJava Agent"); | |
httpConn.setRequestProperty("Authorization", Globle.getAppInstance().getAuthKey1()); | |
httpConn.setRequestProperty(AppConstants.AUTH3, Globle.getAppInstance().getAuthKey2()); | |
outputStream = httpConn.getOutputStream(); | |
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), | |
true); | |
} | |
/** | |
* Adds a form field to the request | |
* @param name field name | |
* @param value field value | |
*/ | |
public void addFormField(String name, String value) { | |
writer.append("--" + boundary).append(LINE_FEED); | |
writer.append("Content-Disposition: form-data; name=\"" + name + "\"") | |
.append(LINE_FEED); | |
writer.append("Content-Type: text/plain; charset=" + charset).append( | |
LINE_FEED); | |
writer.append(LINE_FEED); | |
writer.append(value).append(LINE_FEED); | |
writer.flush(); | |
} | |
/** | |
* Adds a upload file section to the request | |
* @param fieldName name attribute in <input type="file" name="..." /> | |
* @param uploadFile a File to be uploaded | |
* @throws IOException | |
*/ | |
public void addFilePart(String fieldName, File uploadFile) | |
{ | |
try { | |
String fileName = uploadFile.getName(); | |
writer.append("--" + boundary).append(LINE_FEED); | |
writer.append( | |
"Content-Disposition: form-data; name=\"" + fieldName | |
+ "\"; filename=\"" + fileName + "\"") | |
.append(LINE_FEED); | |
writer.append( | |
"Content-Type: " | |
+ URLConnection.guessContentTypeFromName(fileName)) | |
.append(LINE_FEED); | |
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); | |
writer.append(LINE_FEED); | |
writer.flush(); | |
FileInputStream inputStream = null; | |
try { | |
inputStream = new FileInputStream(uploadFile); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
byte[] buffer = new byte[4096]; | |
int bytesRead = -1; | |
while ((bytesRead = inputStream.read(buffer)) != -1) { | |
outputStream.write(buffer, 0, bytesRead); | |
} | |
outputStream.flush(); | |
inputStream.close(); | |
writer.append(LINE_FEED); | |
writer.flush(); | |
} | |
catch (Exception e){ | |
Log.e(TAG,e.toString()); | |
} | |
} | |
public String finish() throws IOException { | |
String response = null; | |
StringBuilder builder=new StringBuilder(); | |
writer.append(LINE_FEED).flush(); | |
writer.append("--" + boundary + "--").append(LINE_FEED); | |
writer.close(); | |
// checks server's status code first | |
int status = httpConn.getResponseCode(); | |
statusCode=status; | |
if (status == HttpURLConnection.HTTP_OK) { | |
BufferedReader reader = new BufferedReader(new InputStreamReader( | |
httpConn.getInputStream())); | |
String line = null; | |
while ((line = reader.readLine()) != null) { | |
builder.append(line); | |
} | |
reader.close(); | |
httpConn.disconnect(); | |
} else { | |
throw new IOException("Server returned non-OK status: " + status); | |
} | |
response=builder.toString(); | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment