ckan 使用 java 客户端上传

ckan uploading using java client

我正在尝试将文件上传到我的本地 Ckan 安装。这就是我的

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

这会在正确的数据集下创建一个项目,但没有文件。我得到了文件的信息,但实际文件没有上传,当我点击文件 link 时出现 404 错误。我试图在 ckan 安装的物理驱动器上寻找它,但我也能在那里找到它。意思是根本没有上传文件。

明白了,我不得不使用"upload"标签如下

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();

    }
}