codeminders / imageshackapi

Automatically exported from code.google.com/p/imageshackapi
0 stars 0 forks source link

Java connection refused #40

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. run the code I have provided below

What is the expected output? What do you see instead?
I expect a standard return from the ImageshackAPI instead I recieve that the 
connection have been refused. I presume this is due to me not knowing what type 
the file should be in when I upload.

Code:

    public String ImageShack (String imageDir, String myKey) {

        //set file
        BufferedImage image = null;
        File file = new File(imageDir);

        //Set namevalue pairs
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        try
        {
            image = ImageIO.read(file);
            ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
            ImageIO.write(image, "png", byteArray);
            byteArray.flush();
            //byte[] byteImage = byteArray.toByteArray();
            Base64 base = new Base64(false);
            String dataImage = base.encodeAsString(byteArray.toByteArray());
            byteArray.close();
            dataImage = java.net.URLEncoder.encode(dataImage, "ISO-8859-1");

            //Assign name valued pars
            params.add(new BasicNameValuePair("key", myKey));
            params.add(new BasicNameValuePair("fileupload", dataImage));
            params.add(new BasicNameValuePair("format", "json"));

            //Create HTTPClient and Client
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.imageshack.us/upload_api.php");

            //set entities
           httpPost.setEntity(new UrlEncodedFormEntity(params));

            //Execute & get response
            HttpResponse response = httpClient.execute(httpPost);
            return response.toString();

        }
        catch(Exception e)
        {
                return "Error: " + e.getMessage();
    }
    }

Original issue reported on code.google.com by billybat...@gmail.com on 18 Jan 2013 at 12:50