Skip to content

Instantly share code, notes, and snippets.

@jervisliu
Created August 20, 2013 04:38
Show Gist options
  • Save jervisliu/6277157 to your computer and use it in GitHub Desktop.
Save jervisliu/6277157 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
private static final String STORAGE_URL = "http://storage.easemob.com";
private static final String POSTURL = STORAGE_URL + "/upload.php";
private static final String CLOUDDRIVE_POSTURL = STORAGE_URL + "/share/addfile.php";
public boolean sendFiletoServer(final String localFilePath,
final String remoteFilePath, final String appId, final String jid) {
File sourceFile = new File(localFilePath);
// check file exists
if (!sourceFile.isFile()) {
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 = 1024*10;
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(1024*10);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Host", "www.storage.easemob.com");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data; charset=utf-8; boundary=" + boundary + lineEnd);
//connection.setRequestProperty("Content-length", "0");
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);
}
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);
try {
while (bytesRead > 0) {
try {
totalReads += bytesRead;
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
int progress = (int)((totalReads / (float) fileSize) * 100);
if(progress > current_process + 5){
current_process = progress;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
} catch (Exception e) {
e.printStackTrace();
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
if (serverResponseCode == 200) {
}
BufferedReader rd = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line;
String lastLine = null;
while ((line = rd.readLine()) != null) {
lastLine = line;
}
rd.close();
fileInputStream.close();
outputStream.flush();
outputStream.close();
outputStream = null;
if (lastLine != null && lastLine.contains("Invalid file")) {
return false;
} else {
}
return true;
} catch (Exception ex) {
// Exception handling
ex.printStackTrace();
} finally {
connection.disconnect();
}
return false;
}
public static void main(String[] args) {
HttpTest t = new HttpTest();
t.sendFiletoServer(t.getClass().getResource("HttpTest.class").getPath(), "remoteFilePath", "weiquan4", "weiquan4_jliu");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment