Authentication went well, I get the auth_token. Then I am using this simple
code to upload an image:
<code>
public static void sendFile() throws FileNotFoundException {
String iFileName = "postcard_template.jpg";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag = "fSnd";
fileInputStream = new FileInputStream(iFileName);
try {
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection) new URL(
HTTP_UPLOAD_ROUTE_URL).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("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"api_key\""
+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(API_KEY);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"auth_token\""
+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(AUTH_TOEKN);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\""
+ iFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b = new StringBuffer();
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
String s = b.toString();
dos.close();
} catch (MalformedURLException ex) {
}
catch (IOException ioe) {
}
}</code>
And I always get the same responce :
<code>
{"success":false,"process_time":372,"result":{"max_filesize":5242880,"space_limi
t":52428800,"space_used":359,"space_left":52428441,"passed":0,"failed":1,"total"
:1,"images":[]},"error":{"error_code":1,"error_message":"One or more file
uploads have
failed","error_info":["failed_uploads",{"postcard_template.jpg":{"error_code":1,
"error_message":"Internal error. Unable to upload the file. Please try
again.","error_info":{"Content-Type":"","filesize":20}}}]}}
</code>
THe last field "filesize" can vary.
Original issue reported on code.google.com by irenobro...@gmail.com on 27 Oct 2013 at 10:54
Original issue reported on code.google.com by
irenobro...@gmail.com
on 27 Oct 2013 at 10:54