Created
March 26, 2018 21:28
-
-
Save hatamiarash7/42131792edad15f7efeca95001026774 to your computer and use it in GitHub Desktop.
Android File Upload
This file contains hidden or 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
static void uploadFile(Context context, final String selectedFilePath) { | |
int serverResponseCode = 0; | |
HttpURLConnection connection; | |
DataOutputStream dataOutputStream; | |
String lineEnd = "\r\n"; | |
String twoHyphens = "--"; | |
String boundary = "*****"; | |
int bytesRead, bytesAvailable, bufferSize; | |
byte[] buffer; | |
int maxBufferSize = 1024 * 1024; | |
File selectedFile = new File(selectedFilePath); | |
if (selectedFile.isFile()) { | |
try { | |
FileInputStream fileInputStream = new FileInputStream(selectedFile); | |
String HOST = context.getResources().getString(R.string.url_host); | |
URL url = new URL(context.getResources().getString(R.string.url_dcim, HOST)); | |
connection = (HttpURLConnection) url.openConnection(); | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
connection.setUseCaches(false); | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Connection", "Keep-Alive"); | |
connection.setRequestProperty("ENCTYPE", "multipart/form-data"); | |
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); | |
connection.setRequestProperty("uploaded_file", selectedFilePath); | |
dataOutputStream = new DataOutputStream(connection.getOutputStream()); | |
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd); | |
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" | |
+ selectedFilePath + "\"" + lineEnd); | |
dataOutputStream.writeBytes(lineEnd); | |
bytesAvailable = fileInputStream.available(); | |
bufferSize = Math.min(bytesAvailable, maxBufferSize); | |
buffer = new byte[bufferSize]; | |
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); | |
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); | |
serverResponseCode = connection.getResponseCode(); | |
String serverResponseMessage = connection.getResponseMessage(); | |
Log.i("D", "SRI: " + serverResponseMessage + ": " + serverResponseCode); | |
if (serverResponseCode == 200) | |
Log.w("D", "O"); | |
fileInputStream.close(); | |
dataOutputStream.flush(); | |
dataOutputStream.close(); | |
} catch (FileNotFoundException ignore) { | |
} catch (MalformedURLException ignore) { | |
} catch (IOException ignore) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment