0

我正在尝试将图像上传到“Imageshack.us”,他们有HTTP API ,我正在使用它来上传我的图像,但不幸的是我总是从服务器得到这个响应:

{ “成功”:假,
“过程时间”:33,
“结果”:{

  "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
           }
        }
     }
  ]    } }

这是我重新获取图像的代码(我正在尝试在 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();

    }

这实际上是对服务器代码的请求:

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();
}

也许有人有使用Imageshack的经验,或者我做错了什么?请帮忙 !在此先感谢,迈克。

4

0 回答 0