sei*_*cle 8 java post apache-commons-httpclient ckan
我正在测试通过API的Java客户端将文件上传到CKAN/datahub.io上的数据集.
public String uploadFile()
throws CKANException {
String returned_json = this._connection.MultiPartPost("", "");
System.out.println("r: " + returned_json);
return returned_json;
}
Run Code Online (Sandbox Code Playgroud)
和
protected String MultiPartPost(String path, String data)
throws CKANException {
URL url = null;
try {
url = new URL(this.m_host + ":" + this.m_port + path);
} catch (MalformedURLException mue) {
System.err.println(mue);
return null;
}
String body = "";
HttpClient httpclient = new DefaultHttpClient();
try {
String fileName = "D:\\test.jpg";
FileBody bin = new FileBody(new File(fileName),"image/jpeg");
StringBody comment = new StringBody("Filename: " + fileName);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
HttpPost postRequest = new HttpPost("http://datahub.io/api/storage/auth/form/2013-01-24T130158/test.jpg");
postRequest.setEntity(reqEntity);
postRequest.setHeader("X-CKAN-API-Key", this._apikey);
HttpResponse response = httpclient.execute(postRequest);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("status code: " + statusCode);
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String line;
while ((line = br.readLine()) != null) {
body += line;
}
System.out.println("body: " + body);
} catch (IOException ioe) {
System.out.println(ioe);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
Run Code Online (Sandbox Code Playgroud)
2个回复我收到我的POST请求:
当我尝试上传的jpeg为2.83 Mb时出现413错误("请求实体太大").当我将文件缩小到较小的尺寸时,这会消失.文件大小上传是否有限制?
500错误("内部服务器错误").这是我被困的地方.这可能与datahub.io上的数据集不是"启用数据存储"这一事实有关吗?(我在数据集中的资源文件旁边看到一个禁用的"Data API"按钮,其工具提示说:"DataStore被禁用时,此资源不可用Data API"
=>这是500错误的可能原因吗?如果是这样,我怎么能从客户端启用它?(指向Python代码会很有用!)
谢谢!
PS:我用于测试目的的数据集:http://datahub.io/dataset/testapi
只有有权访问异常日志的人才能告诉您500正在发生的原因.
不过,我会检查你的要求是一样的,你会从被写入旁边的数据存储中的Python客户端得到什么:https://github.com/okfn/ckanclient/blob/master/ckanclient/ 初始化的.py# L546
您正在多部分请求中发送"bin"图像缓冲区和"comment"file_key.请注意,每次上传都必须更改file_key,因此请添加时间戳或其他内容.也许你需要Content-Type:为二进制文件添加一个.
| 归档时间: |
|
| 查看次数: |
1778 次 |
| 最近记录: |