在CKAN中,我试图使用java客户端上传一个文件,但得到了错误代码“400”,但没有显示任何错误日志

在CKAN中,我试图使用java客户端上传一个文件,但得到了错误代码“400”,但没有显示任何错误日志,java,upload,ckan,Java,Upload,Ckan,在CKAN中,我试图使用java客户端上传一个文件,但得到了错误代码400,但没有显示任何错误日志。我已经在Centos7系统上本地完成了CKAN设置。如果有任何建议,请提供帮助,谢谢: protected String MultiPartPost(String path, String data) throws CKANException { String body = ""; String CKANrepos = "http://172.21.9.11

在CKAN中,我试图使用java客户端上传一个文件,但得到了错误代码400,但没有显示任何错误日志。我已经在Centos7系统上本地完成了CKAN设置。如果有任何建议,请提供帮助,谢谢:

    protected String MultiPartPost(String path, String data)
        throws CKANException {
    String body = "";
    String CKANrepos = "http://172.21.9.118:5000";
    String CKANapiHeader="X-CKAN-API-Key";
    String CKANapi = "api key";
    //1st part
    String generatedFilename=null;
    HttpClient httpclient = new DefaultHttpClient();
    String filename = "test.txt";
    try {
        // create new identifier for every file, use time
        SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyyMMMddHHmmss");
        dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
        String date=dateFormatGmt.format(new Date());
        generatedFilename=date +"/"+filename;
        HttpGet getRequest = new HttpGet(this.CKANrepos+ "/api/storage/auth/form/"+generatedFilename);
        getRequest.setHeader(CKANapiHeader, this.CKANapi);
        HttpResponse response = httpclient.execute(getRequest);
        int statusCode = response.getStatusLine().getStatusCode();

        if(statusCode!=200){
             throw new IllegalStateException("File reservation failed, server responded with code: "+statusCode+
              "\n\nThe message was: "+body);
        }
    }catch (IOException ioe) {
        System.out.println(ioe);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }

    //2nd part
    File file = new File("D:\\test.txt");
    httpclient = new DefaultHttpClient();
    try {

        FileBody bin = new FileBody(file,"text/html");

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("file", bin);

        reqEntity.addPart("key", new StringBody(generatedFilename));

        HttpPost postRequest = new HttpPost(this.CKANrepos+"/storage/upload_handle");
        postRequest.setEntity(reqEntity);
        postRequest.setHeader(CKANapiHeader, this.CKANapi);
        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) {
            body += line;
        }
        if(statusCode!=200){
           System.out.println("statusCode ==" +statusCode);
        }
    }catch (IOException ioe) {
        System.out.println(ioe);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return body;
}

}

您要将此文件上载到哪个版本的CKAN?如果您尝试的是一个相当新的CKAN版本,那么您使用的API是错误的。是的@Nigel我使用的是新的CKAN版本..而我使用的API是错误的,因此请替换为API/action/resource\u create API,它工作得很好..感谢您的评论。