Tweaked File Transfer to fix CB-74

This commit is contained in:
Joe Bowser 2012-03-13 11:46:25 -07:00
parent 8a7af93765
commit d27064794c

View File

@ -225,7 +225,7 @@ public class FileTransfer extends Plugin {
FileUploadResult result = new FileUploadResult(); FileUploadResult result = new FileUploadResult();
// Get a input stream of the file on the phone // Get a input stream of the file on the phone
InputStream fileInputStream = getPathFromUri(file); FileInputStream fileInputStream = (FileInputStream) getPathFromUri(file);
HttpURLConnection conn = null; HttpURLConnection conn = null;
DataOutputStream dos = null; DataOutputStream dos = null;
@ -295,37 +295,53 @@ public class FileTransfer extends Plugin {
conn.setRequestProperty("Cookie", cookie); conn.setRequestProperty("Cookie", cookie);
} }
// Should set this up as an option
if (chunkedMode) {
conn.setChunkedStreamingMode(maxBufferSize);
}
dos = new DataOutputStream( conn.getOutputStream() ); /*
* Store the non-file portions of the multipart data as a string, so that we can add it
// Send any extra parameters * to the contentSize, since it is part of the body of the HTTP request.
*/
String extraParams = "";
try { try {
for (Iterator iter = params.keys(); iter.hasNext();) { for (Iterator iter = params.keys(); iter.hasNext();) {
Object key = iter.next(); Object key = iter.next();
if(key.toString() != "headers") if(key.toString() != "headers")
{ {
dos.writeBytes(LINE_START + BOUNDRY + LINE_END); extraParams += LINE_START + BOUNDRY + LINE_END;
dos.writeBytes("Content-Disposition: form-data; name=\"" + key.toString() + "\";"); extraParams += "Content-Disposition: form-data; name=\"" + key.toString() + "\";";
dos.writeBytes(LINE_END + LINE_END); extraParams += LINE_END + LINE_END;
dos.write(params.getString(key.toString()).getBytes()); extraParams += params.getString(key.toString());
dos.writeBytes(LINE_END); extraParams += LINE_END;
} }
} }
} catch (JSONException e) { } catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e); Log.e(LOG_TAG, e.getMessage(), e);
} }
dos.writeBytes(LINE_START + BOUNDRY + LINE_END); extraParams += LINE_START + BOUNDRY + LINE_END;
dos.writeBytes("Content-Disposition: form-data; name=\"" + fileKey + "\";" + " filename=\""); extraParams += "Content-Disposition: form-data; name=\"" + fileKey + "\";" + " filename=\"";
String midParams = "\"" + LINE_END + "Content-Type: " + mimeType + LINE_END + LINE_END;
String tailParams = LINE_END + LINE_START + BOUNDRY + LINE_START + LINE_END;
// Should set this up as an option
if (chunkedMode) {
conn.setChunkedStreamingMode(maxBufferSize);
}
else
{
int stringLength = extraParams.length() + midParams.length() + tailParams.length() + fileName.getBytes("UTF-8").length;
Log.d(LOG_TAG, "String Length: " + stringLength);
int fixedLength = (int) fileInputStream.getChannel().size() + stringLength;
Log.d(LOG_TAG, "Content Length: " + fixedLength);
conn.setFixedLengthStreamingMode(fixedLength);
}
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(extraParams);
//We don't want to chagne encoding, we just want this to write for all Unicode. //We don't want to chagne encoding, we just want this to write for all Unicode.
dos.write(fileName.getBytes("UTF-8")); dos.write(fileName.getBytes("UTF-8"));
dos.writeBytes("\"" + LINE_END); dos.writeBytes(midParams);
dos.writeBytes("Content-Type: " + mimeType + LINE_END);
dos.writeBytes(LINE_END);
// create a buffer of maximum size // create a buffer of maximum size
bytesAvailable = fileInputStream.available(); bytesAvailable = fileInputStream.available();
@ -346,8 +362,7 @@ public class FileTransfer extends Plugin {
} }
// send multipart form data necesssary after file data... // send multipart form data necesssary after file data...
dos.writeBytes(LINE_END); dos.writeBytes(tailParams);
dos.writeBytes(LINE_START + BOUNDRY + LINE_START + LINE_END);
// close streams // close streams
fileInputStream.close(); fileInputStream.close();