Jetzt verfügbar:  Berichte für DRACOONMehr Infos

DRACOON API: /nodes/files/uploads/{upload_id}

Kommentare

1 Kommentar

  • Offizieller Kommentar
    Avatar
    Michael Netter

    The following snippet should hopefully solve your problem:

     

    public static void uploadFile(File file, String uploadId) throws ClientProtocolException, IOException {
        HttpClient client = HttpClientBuilder.create().build();

        String postUrl = DRACOON_URL + "/api/v4/nodes/files/uploads/" + uploadId;
        HttpPut postReq = new HttpPut(postUrl);
        postReq.addHeader("Accept", "application/json");
        postReq.addHeader("X-Sds-Auth-Token", X_SDS_AUTH_TOKEN);

        InputStream fileContent = new FileInputStream(file);

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody("file", fileContent, ContentType.DEFAULT_BINARY, file.getName());
        HttpEntity entity = builder.build();
        postReq.setEntity(entity);

        client.execute(postReq);
    }

     

    Below you can find a full working example for uploading a file using Apache HttpClient and Google Gson:

    package com.dracoon.examples;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;

    import com.google.gson.Gson;
    import com.google.gson.JsonObject;

    public class UploadTest {

    public static final String DRACOON_URL = "https://dracoon.team";
    public static final long PARENT_ID = 141159;
    public static final String X_SDS_AUTH_TOKEN = "AAABBBCCCP8zIX6KPxZ9GpRMyRca0oJp";
    public static final File DUMMY_FILE = new File("c:\\file.txt");

    public static void main(String[] args) throws ClientProtocolException, IOException {

    String uploadId = createUploadChannel();
    uploadFile(DUMMY_FILE, uploadId);
    completeUpload(uploadId);

    }

    public static String createUploadChannel() throws ClientProtocolException, IOException {

    Gson gson = new Gson();
    HttpClient client = HttpClientBuilder.create().build();
    String url = DRACOON_URL + "/api/v4/nodes/files/uploads";
    String fileName = DUMMY_FILE.getName();
    HttpPost postreq = new HttpPost(url);
    postreq.addHeader("content-type", "application/json");
    postreq.addHeader("Accept", "application/json");
    postreq.addHeader("X-Sds-Auth-Token", X_SDS_AUTH_TOKEN);

    JsonObject expiration = new JsonObject();
    expiration.addProperty("enableExpiration", false);

    JsonObject jsonBody = new JsonObject();
    jsonBody.addProperty("parentId", PARENT_ID);
    jsonBody.addProperty("name", fileName);
    jsonBody.addProperty("classification", 1);
    jsonBody.add("expiration", expiration);

    String body = gson.toJson(jsonBody);

    postreq.setEntity(new StringEntity(body));
    HttpResponse response = client.execute(postreq);
    String json = EntityUtils.toString(response.getEntity(), "UTF-8");

    return gson.fromJson(json, FileUploadResponse.class).uploadId;
    }

    public static void uploadFile(File file, String uploadId) throws ClientProtocolException, IOException {
    HttpClient client = HttpClientBuilder.create().build();

    String postUrl = DRACOON_URL + "/api/v4/nodes/files/uploads/" + uploadId;
    HttpPut postReq = new HttpPut(postUrl);
    postReq.addHeader("Accept", "application/json");
    postReq.addHeader("X-Sds-Auth-Token", X_SDS_AUTH_TOKEN);

    InputStream fileContent = new FileInputStream(file);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();

    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addBinaryBody("file", fileContent, ContentType.DEFAULT_BINARY, file.getName());
    HttpEntity entity = builder.build();
    postReq.setEntity(entity);

    client.execute(postReq);
    }

    public static void completeUpload(String uploadId) throws ClientProtocolException, IOException {
    HttpClient client = HttpClientBuilder.create().build();

    String putUrl = DRACOON_URL + "/api/v4/nodes/files/uploads/" + uploadId;
    HttpPut putReq = new HttpPut(putUrl);
    putReq.addHeader("Accept", "application/json");
    putReq.addHeader("X-Sds-Auth-Token", X_SDS_AUTH_TOKEN);

    client.execute(putReq);
    }

    class CreateFileUploadRequest {
    public Long parentId;
    public String name;
    public Long size;
    public Expiration expiration;
    public Integer classification;
    public String notes;
    }

    class Expiration {
    public String expireAt;
    public Boolean enableExpiration;
    }

    class FileUploadResponse {
    public String uploadId;
    public String token;
    }

    }
     
    Aktionen für Kommentare Permalink

Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.