-
-
Save jervisliu/6276653 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
private boolean sendFiletoServer(final String localFilePath, | |
final String remoteFilePath, final String appId, final String jid, final CloudOperationCallback listener) throws EaseMobException { | |
File sourceFile = new File(localFilePath); | |
// check file exists | |
if (!sourceFile.isFile()) { | |
Log.e(TAG, "Source file doesn't exist"); | |
listener.onError("Source file doesn't exist"); | |
return false; | |
} | |
HttpURLConnection connection = null; | |
DataOutputStream outputStream = null; | |
int current_process = 0; | |
String lineEnd = "\r\n"; | |
String twoHyphens = "--"; | |
String boundary = "*****"; | |
int bytesRead, bytesAvailable, bufferSize; | |
byte[] buffer; | |
int maxBufferSize = NetUtils.getUploadBufSize(EaseMob.applicationContext); | |
int fileSize = (int)sourceFile.length(); | |
int totalReads = 0; | |
try { | |
FileInputStream fileInputStream = new FileInputStream(new File( | |
localFilePath)); | |
URL url = null; | |
if(jid != null) { | |
url = new URL(CLOUDDRIVE_POSTURL); | |
} else { | |
url = new URL(POSTURL); | |
} | |
connection = (HttpURLConnection) url.openConnection(); | |
// Allow Inputs & Outputs | |
connection.setDoInput(true); | |
connection.setDoOutput(true); | |
connection.setUseCaches(false); | |
connection.setChunkedStreamingMode(NetUtils.getUploadBufSize(EaseMob.applicationContext)); | |
// Enable POST method | |
connection.setRequestMethod("POST"); | |
connection.setRequestProperty("Host", STORAGE_URL); | |
connection.setRequestProperty("Connection", "Keep-Alive"); | |
connection.setRequestProperty("Content-Type", | |
"multipart/form-data; charset=utf-8; boundary=" + boundary + lineEnd); | |
connection.connect(); | |
outputStream = new DataOutputStream(connection.getOutputStream()); | |
outputStream.writeBytes(twoHyphens + boundary + lineEnd); | |
String connstr = ""; | |
if (appId != null) { | |
outputStream.writeBytes("Content-Disposition: form-data; name=\"app\"" + lineEnd + lineEnd); | |
outputStream.writeBytes(appId + lineEnd); | |
outputStream.writeBytes(twoHyphens + boundary + lineEnd); | |
} | |
if(jid != null) { | |
outputStream.writeBytes("Content-Disposition: form-data; name=\"id\"" + lineEnd + lineEnd); | |
outputStream.writeBytes(jid + lineEnd); | |
outputStream.writeBytes(twoHyphens + boundary + lineEnd); | |
} | |
outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\""); | |
outputStream.write(remoteFilePath.getBytes("UTF-8")); | |
outputStream.writeBytes("\"" + lineEnd); | |
if (sourceFile.getName().endsWith(".3gp") || sourceFile.getName().endsWith(".amr")) { | |
connstr += ("Content-Type: " + "audio/3gp" | |
+ lineEnd); | |
} else if(sourceFile.getName().endsWith(".mp4")){ | |
connstr += ("Content-Type: " + "video/mpeg4" | |
+ lineEnd); | |
}else { | |
connstr +=("Content-Type: " + "image/png" | |
+ lineEnd); | |
} | |
Log.d("connstr", connstr); | |
outputStream.writeBytes(connstr); | |
outputStream.writeBytes(lineEnd); | |
bytesAvailable = fileInputStream.available(); | |
bufferSize = Math.min(bytesAvailable, maxBufferSize); | |
buffer = new byte[bufferSize]; | |
// Read file | |
bytesRead = fileInputStream.read(buffer, 0, bufferSize); | |
Log.d("Image length", bytesAvailable + ""); | |
try { | |
while (bytesRead > 0) { | |
try { | |
totalReads += bytesRead; | |
outputStream.write(buffer, 0, bufferSize); | |
} catch (OutOfMemoryError e) { | |
e.printStackTrace(); | |
throw new EaseMobException("outofmemoryerror"); | |
} | |
int progress = (int)((totalReads / (float) fileSize) * 100); | |
if(progress > current_process + 5){ | |
current_process = progress; | |
Log.d("HttpFileManager", current_process+""); | |
listener.onProgress(progress); | |
} | |
bytesAvailable = fileInputStream.available(); | |
bufferSize = Math.min(bytesAvailable, maxBufferSize); | |
bytesRead = fileInputStream.read(buffer, 0, bufferSize); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new EaseMobException("error:" + e.toString()); | |
} | |
outputStream.writeBytes(lineEnd); | |
outputStream.writeBytes(twoHyphens + boundary + twoHyphens | |
+ lineEnd); | |
BufferedReader rd = new BufferedReader( | |
new InputStreamReader(connection.getInputStream())); | |
String line; | |
String lastLine = null; | |
while ((line = rd.readLine()) != null) { | |
Log.d(TAG, "RESULT Message: " + line); | |
lastLine = line; | |
} | |
rd.close(); | |
// Responses from the server (code and message) | |
int serverResponseCode = connection.getResponseCode(); | |
String serverResponseMessage = connection.getResponseMessage(); | |
Log.i("Server Response Code ", "" + serverResponseCode); | |
Log.i("Server Response Message", serverResponseMessage); | |
if (serverResponseCode == 200) { | |
Log.d("http", "file server resp 200 ok"); | |
} | |
fileInputStream.close(); | |
outputStream.flush(); | |
outputStream.close(); | |
outputStream = null; | |
if (lastLine != null && lastLine.contains("Invalid file")) { | |
listener.onError("Invalid file"); | |
return false; | |
} else { | |
listener.onProgress(100); | |
} | |
return true; | |
} catch (Exception ex) { | |
// Exception handling | |
ex.printStackTrace(); | |
throw new EaseMobException(ex.getMessage()); | |
} finally { | |
connection.disconnect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment