Skip to content

Instantly share code, notes, and snippets.

@cmcdonaldca
Last active December 17, 2015 20:29
Show Gist options
  • Save cmcdonaldca/5668086 to your computer and use it in GitHub Desktop.
Save cmcdonaldca/5668086 to your computer and use it in GitHub Desktop.
Example how to reproduce Parse.com REST File upload problem
Sample code to reproduce REST API FileUpload 500 errors. ParseTest.java is an extraction from my android project and output is the text output after running that ParseTest class with:
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)
To try yourself:
-find two files (under 100K and over 200K) on your system and fill in the paths on Line 34 and 36
-Fill in an AppID and RESTKey you can test with on lines 99 and 100
-compile javac ParseTest.java
-run java ParseTest
-my output is shown in the output file
fixedLength:57889
totalBytes:57889
responseCode:201
responseString:{"url":"http://files.parse.com/----SNIP----deb-hello.txt","name":"--SNIP--deb-hello.txt"}
fixedLength:247368
Uploaded 114688 of 247368 bytes
Uploaded 229376 of 247368 bytes
totalBytes:247368
responseCode:500
responseString:<!DOCTYPE html>
<html>
<head>
<title>We're sorry, but something went wrong (500)</title>
<style type="text/css">
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
padding: 0 4em;
margin: 4em auto 0 auto;
border: 1px solid #ccc;
border-right-color: #999;
border-bottom-color: #999;
}
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
</style>
</head>
<body>
<!-- This file lives in public/500.html -->
<div class="dialog">
<h1>We're sorry, but something went wrong.</h1>
<p>We've been notified about this issue and we'll take a look at it shortly.</p>
</div>
</body>
</html>
import java.io.Console;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import javax.net.ssl.HttpsURLConnection;
public class ParseTest {
public static void main(String[] args){
Console console = System.console();
PrintWriter writer = console.writer();
if (console == null) {
System.err.println("No console.");
System.exit(1);
}
try {
String targetUrl = "https://api.parse.com/1/files/hello.txt";
// this file is 57K
String smallFile = "/Users/cmcdonaldca/Pictures/profile_12.jpg";
// this file is 242K
String biggerFile = "/Users/cmcdonaldca/Pictures/DSC_0351_2_3.jpg";
String mimeType = "image/jpeg";
HttpFile.upload(targetUrl, smallFile, mimeType, writer);
HttpFile.upload(targetUrl, biggerFile, mimeType, writer);
} catch (Exception ex) {
System.err.println(ex.toString());
System.exit(1);
}
}
public class Log {
public Log(PrintWriter writer) {
_writer = writer;
}
private PrintWriter _writer;
public void i(String tag, String message) {
_writer.println(tag + " - " + message);
}
}
public static class HttpFile {
private static final String LOG_TAG = "HttpFile";
private static final int MAX_BUFFER_SIZE = 16 * 1024;
public HttpFile() {
}
public static Object upload(String targetUrl, String localUrl, String mimeType, PrintWriter console) throws IOException {
HttpURLConnection conn = null;
try {
URL destinationUrl = new URL(targetUrl);
Boolean useHttps = destinationUrl.getProtocol().toLowerCase().equals("https");
//------------------ CLIENT REQUEST
// Open a HTTP connection to the URL based on protocol
if (useHttps) {
// Using standard HTTPS connection. Will not allow self signed certificate
conn = (HttpsURLConnection) destinationUrl.openConnection();
}
// Return a standard HTTP connection
else {
conn = (HttpURLConnection) destinationUrl.openConnection();
}
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("X-Parse-Application-Id", PUT_YOUR_ID);
conn.setRequestProperty("X-Parse-REST-API-Key", PUT_YOUR_KEY);
// Get a input stream of the file on the phone
InputStream sourceInputStream = new FileInputStream(localUrl);
long fixedLength = -1;
if (sourceInputStream instanceof FileInputStream) {
fixedLength = (int) ((FileInputStream)sourceInputStream).getChannel().size();
}
// setFixedLengthStreamingMode causes and OutOfMemoryException on pre-Froyo devices.
// http://code.google.com/p/android/issues/detail?id=3164
// It also causes OOM if HTTPS is used, even on newer devices.
conn.setFixedLengthStreamingMode((int)fixedLength);
//Map<String,List<String>> hf = conn.getHeaderFields();
console.println("fixedLength:" + fixedLength);
DataOutputStream dos = null;
try {
dos = new DataOutputStream( conn.getOutputStream() );
// create a buffer of maximum size
int bytesAvailable = sourceInputStream.available();
int bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = sourceInputStream.read(buffer, 0, bufferSize);
long totalBytes = 0;
long prevBytesRead = 0;
while (bytesRead > 0) {
totalBytes += bytesRead;
//result.setBytesSent(totalBytes);
dos.write(buffer, 0, bytesRead);
if (totalBytes > prevBytesRead + 102400) {
prevBytesRead = totalBytes;
console.println("Uploaded " + totalBytes + " of " + fixedLength + " bytes");
}
bytesAvailable = sourceInputStream.available();
bufferSize = Math.min(bytesAvailable, MAX_BUFFER_SIZE);
bytesRead = sourceInputStream.read(buffer, 0, bufferSize);
}
console.println("totalBytes:" + totalBytes);
dos.flush();
} finally {
safeClose(sourceInputStream);
safeClose(dos);
}
//------------------ read the SERVER RESPONSE
String responseString;
int responseCode = conn.getResponseCode();
console.println("responseCode:" + responseCode);
InputStream inStream = null;
try {
inStream = getInputStream(conn);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
// write bytes to file
while ((bytesRead = inStream.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
responseString = out.toString("UTF-8");
} finally {
safeClose(inStream);
}
console.println("responseString:" + responseString);
return responseString;
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
private static InputStream getInputStream(HttpURLConnection conn) throws IOException {
if (conn.getResponseCode() > 400) {
return conn.getErrorStream();
}
return conn.getInputStream();
}
private static void safeClose(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment