gpt4 book ai didi

java - 使用java客户端ckan上传

转载 作者:行者123 更新时间:2023-11-30 03:21:48 25 4
gpt4 key购买 nike

我正在尝试将文件上传到我的本地 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}
}

这会在右侧数据集下创建一个项目,但不包含文件。我获取了文件的信息,但实际文件未上传,当我单击文件链接时收到 404 错误。我尝试在 ckan 安装的物理驱动器上查找它,但我也可以在那里找到它。这意味着该文件根本没有上传。

最佳答案

明白了,我必须使用“上传”标签,如下

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

}
}

关于java - 使用java客户端ckan上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31155127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com