使用java将附件上载到zendesk

使用java将附件上载到zendesk,java,Java,我正在尝试将附件上载到。从API文档中,您必须使用 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&token= {optional_token}" 我

我正在尝试将附件上载到。从API文档中,您必须使用

 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&token=                       {optional_token}"
我正试图用java做同样的事情。我可以上传一个文件并收到正确的json响应。但是,如果在zendesk服务器上打开该文件,则无法识别该文件。如果我用curl从命令行上传同一个文件,一切正常。我做错了什么?这是我用来上传文件的java代码

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 && len < 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}

    }
}
publicstaticvoidmain(字符串[]args)引发IOException{
File File=新文件(“C:\\Users\\user\\Documents\\zendesk2\\Zendesk\\src\\main\\resources\\scrat.jpg”);
试一试{
MultipartEntity MultipartEntity=新的MultipartEntity(HttpMultipartMode.BROWSER_兼容);
addPart(“文件名”,新的StringBody(file.getName());
FileBody FileBody=新的FileBody(文件,“应用程序/八位字节流”);
multipatentity.addPart(“附件”,文件体);
//-u管理员:密码
凭证凭证=新用户名密码凭证(“用户名”、“密码”);
DefaultHttpClient httpClient=新的DefaultHttpClient();
httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY,credentials);
//-X柱
HttpPost HttpPost=新的HttpPost(“https://testserver.zendesk.com/api/v2/uploads.json");
//@-绝对路径
httpPost.setEntity(多协议实体);
//过程响应
HttpResponse response=httpClient.execute(httpPost);
HttpEntity当前性=response.getEntity();
if(最近性!=null){
long len=resEntity.getContentLength();
如果(len!=-1&&len<2048){
//正在使用gson…分析此结果。。。。
System.out.println(EntityUtils.toString(resEntity));
}否则{
//输出内容
}
}
httpClient.getConnectionManager().shutdown();
}捕获(例外e){
//-f、 静默失败}
}
}
我一直在从事一项研究

您可以使用代码的当前0.0.3快照版本上载附件。在不久的将来,我可能会添加更多功能

下面是一些API当前工作方式的示例代码:

ZenDesk zd = new ZenDesk.Builder("https://{{your domain}}.zendesk.com")
        .setUsername("...")
        .setToken("...") // or .setPassword("...")
        .build();

byte[] contents = new byte[file.length()];
FileInputStream fis = new FileInputStream(fis);
fis.read(contents);
fis.close();

Attachment.Upload upload = zd.createUpload(file.getName(), "application/binary", contents);

zd.close();

希望这能有所帮助(请注意,上面的示例代码并没有整理异常,但应该能为您提供API的基本工作方式)。

我知道这很旧,但我花了一段时间才弄明白如何做,我想与大家分享

  • 要将文件附加到票证,需要三个步骤。创建票据 如果一个不存在(我知道,这应该是显而易见的),并获得 车票号码
  • 为您的特定文件类型等创建上载,这将返回带有令牌属性的上载
  • 通过引用票证id和包含单个令牌列表的注释来更新票证
  • 
    票证=zd.createTicket(票证);
    附件.Upload Upload=zd.createUpload(文件名,“应用程序/二进制文件”,内容);
    List tokens=new ArrayList();
    add(upload.getToken());
    注释=新注释();
    comment.setBody(“我的上传”);
    注释.setTokenks(令牌);
    票证=新票证();
    ticket.setId(ticketId);
    票证设置注释(注释);
    zd.upateTicket(票);
    

    这不是一个完整的示例,对吗?合乎逻辑的下一步是以某种方式使上传在给定的票证上可用。