Skip to content

Instantly share code, notes, and snippets.

@bitristan
Created January 16, 2020 05:56
Show Gist options
  • Select an option

  • Save bitristan/d0b57ab39e659aee4211fd7985e2c7b3 to your computer and use it in GitHub Desktop.

Select an option

Save bitristan/d0b57ab39e659aee4211fd7985e2c7b3 to your computer and use it in GitHub Desktop.
private static final String HTTP_END = "\r\n";
private static final String HTTP_START = "--";
private static final String HTTP_BOUNDARY = "*******";
private boolean uploadFileToServer(File file) {
if (file == null || !file.exists()) {
LogUtil.d(TAG, "file not exists, ignore upload.");
return true;
}
try {
LogUtil.d(TAG, "begin upload file to server.");
final URL url = new URL("url");
final String name = file.getName();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + HTTP_BOUNDARY);
Map<String, String> params = new HashMap<>();
params.put("params1", "params1");
params.put("params2", "params2");
DataOutputStream outStream =
new DataOutputStream(connection.getOutputStream());
outStream.writeBytes(appendPostParams(params).toString());
outStream.flush();
outStream.writeBytes(HTTP_START + HTTP_BOUNDARY + HTTP_END);
String fileName = meeting.id + "-" + source + extractFullSuffix(name);
outStream.writeBytes("Content-Disposition: form-data;" +
"name=\"wavFile\";filename=\"" + fileName + "\"" + HTTP_END);
outStream.writeBytes(HTTP_END);
FileInputStream inStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
int length;
while ((length = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, length);
}
outStream.writeBytes(HTTP_END);
outStream.writeBytes(HTTP_START + HTTP_BOUNDARY + HTTP_START + HTTP_END);
inStream.close();
outStream.flush();
int code = connection.getResponseCode();
LogUtil.d(TAG, "responseCode = " + code);
InputStream responseStream = connection.getInputStream();
StringBuilder sb = new StringBuilder();
int data;
while ((data = responseStream.read()) != -1) {
sb.append((char) data);
}
LogUtil.d(TAG, "response: " + sb.toString());
outStream.close();
return true;
} catch (Exception e) {
LogUtil.e(TAG, "error upload file to server", e);
return false;
}
}
private StringBuilder appendPostParams(Map<String,String> params){
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet() ){
sb.append(HTTP_START)
.append(HTTP_BOUNDARY)
.append(HTTP_END).append("Content-Disposition: form-data; name=\"")
.append(entry.getKey())
.append("\"")
.append(HTTP_END)
.append("Content-Type: text/plain; charset=UTF-8")
.append(HTTP_END)
.append("Content-Transfer-Encoding: 8bit")
.append(HTTP_END)
.append(HTTP_END)
.append(entry.getValue())
.append(HTTP_END);
}
return sb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment