2013-02-05 26 views
0

我試圖上傳一個附件到zendesk。從API文檔你必須使用用java上傳一個附件到zendesk

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} 

    } 
} 

回答

1

我一直在java client for zendesk

您可以使用代碼當前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的作品)

+0

這不是一個完整的例子,雖然對不對?合乎邏輯的下一步是以某種方式使上載在給定票證上可用。 –

0

我知道這是舊的,但它花了我一段時間來解決如何做到這一點,我想分享。

  1. 要將文件附加到故障單,它是一個3步過程。創建一個票 如果不存在(我知道,它應該很明顯),並獲得 票證編號。
  2. 爲您的特定文件類型等創建一個上傳,它將返回帶有令牌屬性的上傳。
  3. 通過引用包含單個令牌列表的註釋的票證ID更新票證。

Ticket ticket = zd.createTicket(ticket); Attachment.Upload upload = zd.createUpload(fileName, "application/binary", content); List tokens = new ArrayList<>(); tokens.add(upload.getToken()); Comment comment = new Comment(); comment.setBody("My upload"); comment.setTokenks(tokens); Ticket ticket = new Ticket(); ticket.setId(ticketId); ticket.setComment(comment); zd.upateTicket(ticket);