2015-07-01 83 views
0

我想上傳一個文件到我的本地Ckan安裝。這是我所ckan使用java客戶端上傳

static String myApiKey="fa0499d1-ffda-4590-82b3-4afdb9c91576"; 
static String uploadFileName="/home/ilias/log.txt"; 

public static void uploadFile() 
{ 
    HttpClient httpclient = new DefaultHttpClient(); 
    Date now=new Date(); 
    File file = new File(uploadFileName); 
    httpclient = new DefaultHttpClient(); 
    FileBody bin = new FileBody(new File(uploadFileName),"text"); 
    SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
    String date=dateFormatGmt.format(new Date());  

    file = new File(uploadFileName); 
    httpclient = new DefaultHttpClient(); 
    try { 
      HttpEntity reqEntity = MultipartEntityBuilder.create() 
      .addPart("file", bin) 
      .addPart("key", new StringBody(uploadFileName+date)) 
      .addPart("package_id",new StringBody("test2")) 
      .addPart("url",new StringBody(HOST+"/files/"+date+"/test.txt"))   
      .build(); 

      HttpPost postRequest = new HttpPost(HOST+"/api/action/resource_create"); 
      postRequest.setEntity(reqEntity); 
      postRequest.setHeader("X-CKAN-API-Key", myApiKey); 

      HttpResponse response = httpclient.execute(postRequest); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      BufferedReader br = new BufferedReader(
        new InputStreamReader((response.getEntity().getContent()))); 

      String line; 
      while ((line = br.readLine()) != null) { 
      System.out.println("+"+line); 
      } 
      if(statusCode!=200){ 
       System.out.println("statusCode =!=" +statusCode); 
      } 
    }catch (IOException ioe) { 
    System.out.println(ioe); 
    } finally { 
    httpclient.getConnectionManager().shutdown(); 
    } 

}

這是從CKAN

{"help": 
"http://192.168.1.2:5000/api/3/action/help_show?name=resource_create", 
"success": true, 
"result": { 
    "url_type": null, 
    "cache_last_updated": null, 
    "package_id": "97d53eb6-60af-4c21-91b9-13a354e3ede4", 
    "webstore_last_updated": null, 
    "file": "FieldStorage('file', u'log.txt')", 
    "id": "20fb8e22-6c89-4e3d-9c56-7991ecbc3cfc", 
    "size": null, 
    "state": "active", 
    "hash": "", 
    "description": "", 
    "format": "TXT", 
    "tracking_summary": 
     {"total": 0, "recent": 0}, 
    "mimetype_inner": null, 
    "key": "/home/ilias/log.txt20150701_002641", 
    "mimetype": null, 
    "cache_url": null, 
    "name": null, 
    "created": "2015-07-01T00:26:44.410200", 
    "url": "http://192.168.1.2:5000/files/20150701_002641/test.txt", 
    "webstore_url": null, 
    "last_modified": null, 
    "position": 23, 
    "revision_id": "45d45b31-7177-4d88-8694-2f0f33c358ec", 
    "resource_type": null} 
} 

這將創建正確的數據集下的一個項目,但出來的文件中。我得到的文件的信息,但實際的文件沒有上傳,我收到404錯誤,當我點擊文件鏈接。我試圖在ckan安裝的物理驅動器上尋找它,但是我也可以在那裏找到它。這意味着該文件根本沒有上傳。

回答

0

明白了,我不得不使用「上傳」標籤如下

ContentBody cbFile = new FileBody(new File("/path/to/file.ext"), ContentType.TEXT_HTML); 
HttpEntity reqEntity = MultipartEntityBuilder.create() 
     .addPart("upload",cbFile) 

,並在情況下,這可能是有幫助別人的完整代碼使用Java是一個文件上傳到CKAN安裝:

public static String uploadFile() 
    { 
     String myApiKey="apikey"; 
     String uploadFileName="/path/to/file.ext"; 
     String HOST="http://ckan.host.com"; 
     String line; 
     StringBuilder sb = new StringBuilder(); 
     CloseableHttpClient httpclient = HttpClientBuilder.create().build(); 
     File file = new File(uploadFileName); 
     SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
     String date=dateFormatGmt.format(new Date()); 

     HttpPost postRequest; 
     file = new File(uploadFileName); 
     try { 

      ContentBody cbFile = new FileBody(file, ContentType.TEXT_HTML); 
      HttpEntity reqEntity = MultipartEntityBuilder.create() 
      .addPart("file", cbFile) 
      .addPart("key", new StringBody(uploadFileName+date,ContentType.TEXT_PLAIN)) 
      .addPart("package_id",new StringBody("dataSetName",ContentType.TEXT_PLAIN)) 
      .addPart("url",new StringBody("path/to/save/dir",ContentType.TEXT_PLAIN)) 
      .addPart("upload",cbFile) 
      .addPart("comment",new StringBody("comments",ContentType.TEXT_PLAIN)) 
      .addPart("notes", new StringBody("notes",ContentType.TEXT_PLAIN)) 
      .addPart("author",new StringBody("AuthorName",ContentType.TEXT_PLAIN)) 
      .addPart("author_email",new StringBody("AuthorEmail",ContentType.TEXT_PLAIN)) 
      .addPart("title",new StringBody("title",ContentType.TEXT_PLAIN)) 
      .addPart("description",new StringBody("file Desc"+date,ContentType.TEXT_PLAIN))  
      .build(); 

      postRequest = new HttpPost(HOST+"/api/action/resource_create"); 
       postRequest.setEntity(reqEntity); 
       postRequest.setHeader("X-CKAN-API-Key", myApiKey); 

      HttpResponse response = httpclient.execute(postRequest); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      BufferedReader br = new BufferedReader(
        new InputStreamReader((response.getEntity().getContent()))); 

      sb.append(statusCode+"\n"); 
      if(statusCode!=200){ 
       System.out.println("statusCode =!=" +statusCode);   
      } 
      else System.out.println("OK"); 

      while ((line = br.readLine()) != null) { 
       sb.append(line+"\n"); 
      System.out.println("+"+line); 
      } 

      httpclient.close(); 
      return sb.toString(); 
    }catch (IOException ioe) { 
    System.out.println(ioe); 
    return "error"+ioe; 
    } finally { 
    httpclient.getConnectionManager().shutdown(); 

    } 
}