Small fix for upload

This commit is contained in:
stom79 2017-11-22 07:51:12 +01:00
parent bc58301b74
commit a087d271cc
1 changed files with 16 additions and 9 deletions

View File

@ -18,7 +18,6 @@ import android.content.SharedPreferences;
import android.os.Build;
import android.text.Html;
import android.text.SpannableString;
import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedInputStream;
@ -66,7 +65,7 @@ public class HttpsConnection {
private HttpsURLConnection httpsURLConnection;
private String since_id, max_id;
private Context context;
private int CHUNK_SIZE = 4096;
public HttpsConnection(){}
@ -208,7 +207,7 @@ public class HttpsConnection {
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead;
byte[] buffer = new byte[1024];
byte[] buffer = new byte[CHUNK_SIZE];
int contentSize = httpsURLConnection.getContentLength();
int downloadedFileSize = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
@ -282,7 +281,7 @@ public class HttpsConnection {
ByteArrayOutputStream ous = null;
try {
try {
byte[] buffer = new byte[4 * 1024]; // or other buffer size
byte[] buffer = new byte[CHUNK_SIZE]; // or other buffer size
ous = new ByteArrayOutputStream();
int read;
while ((read = inputStream.read(buffer)) != -1) {
@ -300,20 +299,28 @@ public class HttpsConnection {
}
byte[] pixels = ous.toByteArray();
int lengthSent = pixels.length;
lengthSent += 2 * (twoHyphens + boundary + twoHyphens + lineEnd).getBytes().length;
lengthSent += ("Content-Disposition: form-data; name=\"file\";filename=\"picture.png\"" + lineEnd).getBytes().length;
lengthSent += 2 * (lineEnd).getBytes().length;
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setFixedLengthStreamingMode(lengthSent);
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
httpsURLConnection.setDoInput(true);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setUseCaches(false);
httpsURLConnection.setRequestMethod("POST");
if (token != null)
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + token);
httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpsURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);
httpsURLConnection.setChunkedStreamingMode(-1);
DataOutputStream request = new DataOutputStream(httpsURLConnection.getOutputStream());
@ -325,16 +332,17 @@ public class HttpsConnection {
int totalSize = pixels.length;
int bytesTransferred = 0;
int chunkSize = 2048;
while (bytesTransferred < totalSize) {
int nextChunkSize = totalSize - bytesTransferred;
if (nextChunkSize > chunkSize) {
nextChunkSize = chunkSize;
if (nextChunkSize > CHUNK_SIZE) {
nextChunkSize = CHUNK_SIZE;
}
request.write(pixels, bytesTransferred, nextChunkSize);
bytesTransferred += nextChunkSize;
final int progress = 100 * bytesTransferred / totalSize;
((TootActivity)context).runOnUiThread(new Runnable() {
public void run() {
@ -346,7 +354,6 @@ public class HttpsConnection {
request.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
request.flush();
request.close();
if (200 != httpsURLConnection.getResponseCode()) {
Reader in = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();