我创建了一个java程序,可以将文件上传到ckan安装,(目前我的本地测试安装,没有在现场测试)。
我测试我的应用程序的文本文件正确上传到ckan。我确实有一些zip文件有问题
经过一些测试和失败尝试后,我意识到问题在于文件的大小。对于低于4Kb的zip文件,它可以工作,但是对于较大的文件,它会因错误“服务器错误”而失败。
文件无法通过我的java应用程序上传,在使用ckan前端时上传得很好,所以我猜测问题出在我的java应用程序而不是ckan安装上。这是我正在使用的代码:
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;
try {
ContentType zipType=ContentType.create("zip");
ContentBody cbFile = new FileBody(file,zipType);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("file", cbFile)
.addPart("url",new StringBody(HOST+"/files/"+date+"/"+uploadFileName,ContentType.TEXT_PLAIN))
.addPart("upload",cbFile)
.addPart("title",new StringBody(TITLE,ContentType.TEXT_PLAIN))
.addPart("description",new StringBody(DESCRIPTION+date,ContentType.TEXT_PLAIN))
.addPart("comment",new StringBody(COMMENT,ContentType.TEXT_PLAIN))
.addPart("key", new StringBody(uploadFileName+date,ContentType.TEXT_PLAIN))
.addPart("package_id",new StringBody("test2",ContentType.TEXT_PLAIN))
.addPart("notes", new StringBody("notes",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())));
}
catch (IOException ioe) {
System.out.println(ioe);
}
finally {
httpclient.getConnectionManager().shutdown();
}