Upload an attachment to zendesk with java
<p>I'm trying to upload an attachement to <a href="http://developer.zendesk.com/documentation/rest_api/attachments.html" rel="nofollow">zendesk</a>. From the API documentation you have to use</p>

<pre><code> curl -u username:password -H "Content-Type: application/binary" \
--data-binary @file.dat -X POST \
"https://helpdesk.zendesk.com/api/v2/uploads.json?filename=myfile.dat&amp;token=                       {optional_token}"
</code></pre>

<p>I am trying to do the same thing with java. I can upload a file and receive the correct json response. However if I open the file on the zendesk server, the file is not recognized. If I upload the same file with curl from the command line everything is working OK. What am I doing wrong here ? This is my java code I use to upload the file.</p>

<pre><code>public static void main(String[] args) throws IOException {
    File file = new File("C:\\Users\\user\\Documents\\zendesk2\\Zendesk\\src\\main\\resources\\scrat.jpg");
    try {
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("filename", new StringBody(file.getName()));
        FileBody fileBody = new FileBody(file, "application/octet-stream");
        multipartEntity.addPart("attachment", fileBody);

        // -u admin:password
        Credentials credentials = new UsernamePasswordCredentials("username", "passw");
        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, credentials);

        // -X POST
        HttpPost httpPost = new HttpPost("https://testserver.zendesk.com/api/v2/uploads.json");

        // @ - absolute path
        httpPost.setEntity(multipartEntity);

        // process response
        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            long len = resEntity.getContentLength();
            if (len != -1 &amp;&amp; len &lt; 2048) {
// this result is being parsed with gson....
                System.out.println(EntityUtils.toString(resEntity));
            } else {
                // Stream content out
            }
        }

        httpClient.getConnectionManager().shutdown();

    } catch (Exception e) {
        //-f, fail silently}

    }
}
</code></pre>
