使用Java将图像上载到imageshack

使用Java将图像上载到imageshack,java,base64,httpclient,image-uploading,Java,Base64,Httpclient,Image Uploading,我在使用Java将图像上传到imageshack时遇到了以下代码问题。我得到的反应是http拒绝了我的连接。我认为我的问题在于如何给出图像文件,因为我不确定它是否应该是base64、byteArray等 任何帮助都会很好,谢谢 public String ImageShack (String imageDir, String myKey) { //set file BufferedImage image = null; File file = new File(imag

我在使用Java将图像上传到imageshack时遇到了以下代码问题。我得到的反应是http拒绝了我的连接。我认为我的问题在于如何给出图像文件,因为我不确定它是否应该是base64、byteArray等

任何帮助都会很好,谢谢

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);
        byte[] byteImage = byteArray.toByteArray();
        String dataImage = new Base64().encodeAsString(byteImage);

        //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();
}
}
公共字符串ImageShack(字符串imageDir,字符串myKey){
//设置文件
BuffereImage图像=空;
文件文件=新文件(imageDir);
//设置名称值对
List params=new ArrayList();
尝试
{
image=ImageIO.read(文件);
ByteArrayOutputStream byteArray=新建ByteArrayOutputStream();
写入(图像,“png”,byteArray);
字节[]byteImage=byteArray.toByteArray();
字符串dataImage=newbase64().encodeAsString(byteImage);
//分配名称值PAR
参数add(新的BasicNameValuePair(“key”,myKey));
添加(新的BasicNameValuePair(“文件上传”,dataImage));
添加(新的BasicNameValuePair(“格式”、“json”);
//创建HTTPClient和客户端
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.imageshack.us/upload_api.php");
//集合实体
setEntity(新的UrlEncodedFormEntity(参数));
//执行并获取响应
HttpResponse response=httpClient.execute(httpPost);
返回response.toString();
}
捕获(例外e)
{
return“Error:+e.getMessage();
}
}

我发现了一个类似的问题,并不是专门针对imageshack的,只是一个网站,但下面是他们解决方案中的代码:

BufferedImage img = ImageIO.read(new File("temp.jpg"));             
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", baos);
baos.flush();
Base64 base = new Base64(false);
String encodedImage = base.encodeToString(baos.toByteArray());
baos.close();
encodedImage = java.net.URLEncoder.encode(encodedImage, "ISO-8859-1");
request.setRequestBody(encodedImage); 

以上就是。如果这是问题的根源,那么在Image/Base64上进行字符串转换也会很有帮助。

我更改了代码,使其与上面的代码类似,但仍然没有响应。我不确定该从这里走到哪里。然后我建议您查阅API文档或在他们的网站上发布。