public void writeTo(OutputStream outstream) throws IOException { outstream.write(topBytes); try { InputStream mediaStream = AppMonk.getContentResolver().openInputStream(media); IOTricks.copyStreamToStream(mediaStream, outstream, 32 * 1024); mediaStream.close(); } catch (Exception e) { } outstream.write(bottomBytes); }
public long getContentLength() { int size = 0; size += topBytes.length; try { InputStream mediaStream = AppMonk.getContentResolver().openInputStream(media); size += IOTricks.countBytesInStream(mediaStream); mediaStream.close(); } catch (Exception e) { return 0; } size += bottomBytes.length; return size; }
public MIMEFileUploadEntity( List<NameValuePair> params, String fileParamName, Uri media, String mediaType) { StringBuffer sb = new StringBuffer(); sb.append("----------MIMEFileUploadEntityBoundary-"); sb.append(Double.toString(Math.random()).substring(2)) .append(Double.toString(Math.random()).substring(2)) .append("--"); String boundary = sb.toString(); this.media = media; this.contentType = "multipart/form-data; boundary=" + boundary; String fileName; if (mediaType == null) { mediaType = AppMonk.getContentResolver().getType(media); } if (mediaType == null) { if (media.toString().endsWith(".jpg")) { mediaType = "image/jpeg"; } else if (media.toString().endsWith(".png")) { mediaType = "image/png"; } } if (mediaType.startsWith("image/jpeg")) { fileName = "Mobile-Upload.jpg"; } else if (mediaType.startsWith("image/png")) { fileName = "Mobile-Upload.png"; } else if (mediaType.startsWith("video")) { fileName = "Mobile-Upload.3gp"; } else { fileName = "Mobile-Upload"; } sb.setLength(0); for (NameValuePair pair : params) { sb.append(twoHyphens).append(boundary).append(lineEnd); sb.append("Content-Disposition: form-data; name=\"") .append(pair.getName()) .append("\"") .append(lineEnd); sb.append(lineEnd); sb.append(pair.getValue().trim()); sb.append(lineEnd); } sb.append(twoHyphens).append(boundary).append(lineEnd); sb.append("Content-Disposition: form-data; name=\"") .append(fileParamName) .append("\"; filename=\"") .append(fileName) .append("\"") .append(lineEnd); sb.append("Content-Type: ").append(mediaType).append(lineEnd); sb.append("Content-Transfer-Encoding: binary").append(lineEnd); sb.append(lineEnd); this.topBytes = sb.toString().getBytes(); try { if (SimpleHttpClient.debug) { String headers = sb.toString(); for (NameValuePair pair : params) { if (pair.getName().contains("ass")) { // "Password" or // "PassWord" or // "password"... headers = headers.replace(pair.getValue(), "xxxxxx"); } } Log.d("MIME", "MIME Headers: " + headers); } } catch (Exception e) { } sb.setLength(0); sb.append(lineEnd); sb.append(twoHyphens); sb.append(boundary); sb.append(twoHyphens); sb.append(lineEnd); this.bottomBytes = sb.toString().getBytes(); }