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安裝的物理驅動器上尋找它,但是我也可以在那裏找到它。這意味着該文件根本沒有上傳。