I'm trying to upload image to the "Imageshack.us" , they have HTTP API and I am using it to upload my image, but unfortunately I always get this response from server :
{ "success":false,
"process_time":33,
"result":{"max_filesize":5242880, "space_limit":52428800, "space_used":0, "space_left":52428800, "passed":0, "failed":1, "total":1, "images":[ ] }, "error":{ "error_code":1, "error_message":"One or more file uploads have failed", "error_info":[ "failed_uploads", { "test.PNG":{ "error_code":1, "error_message":"Not a valid image file", "error_info":{ "Content-Type":"", "filesize":0 } } } ] } }
Here is my code for rereiving image (I am trying to achieve all this on Android) :
public static String storeDrawable(){
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),
R.drawable.pctemplate3);
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
OutputStream outStream = null;
File file = new File(extStorageDirectory, "test.PNG");
try {
outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
}
return file.getAbsolutePath();
}
and here is actually request to server code :
try {
Charset charset = Charset.defaultCharset();
String param = Settings.getInstance().getUserAuthToken();
File binaryFile = new File(path);
String boundary = Long.toHexString(System.currentTimeMillis()); // Just
// generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection = new URL("https://api.imageshack.us/v1/images").openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
PrintWriter writer = null;
try {
OutputStream output = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, charset),
true); // true = autoFlush, important!
// Send normal param.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"auth_token\"")
.append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF);
writer.append(param).append(CRLF).flush();
// Send text file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"api_key\"")
.append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF);
writer.append(API_KEY).append(CRLF).flush();
// Send binary file.
writer.append("--" + boundary).append(CRLF);
writer.append(
"Content-Disposition: form-data; name=\"file@\"; filename=\""
+ binaryFile.getName() + "\"").append(CRLF);
writer.append(
"Content-Type: "
+ URLConnection.guessContentTypeFromName(binaryFile
.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
InputStream input = new FileInputStream(binaryFile);
try {
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
output.flush(); // Important! Output cannot be closed. Close of
// writer will close output as well.
} finally {
try { input.close(); } catch (IOException logOrIgnore) {}
}
writer.append(CRLF).flush(); // CRLF is important! It indicates end
// of binary boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF);
InputStream response = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(response));
String line;
StringBuffer responseStr = new StringBuffer();
while ((line = rd.readLine()) != null) {
responseStr.append(line);
responseStr.append('\r');
}
rd.close();
} finally {
if (writer != null) writer.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe someone has experience with Imageshack, or I am doing something wrong in general ? Please HELP ! Thanks in advance , Mike.