2

So I searched quite a bit now for sample codes but the only thing I found was on examples for the server side, meaning the receiving part.

I want to create an application, that uses restlet to upload a file, content type: multipart/form-data. So I need the sending part

How do I create the form for this?

What I tried is the following, but it's not working:

public void UploadFile(File f){
    Form fileForm = new Form(); 
    fileForm.add(Disposition.NAME_FILENAME, "test.jpg");
    Disposition disposition = new Disposition(Disposition.TYPE_INLINE, fileForm); 
    FileRepresentation entity = new FileRepresentation(f, MediaType.IMAGE_ALL);  
    entity.setDisposition(disposition);

    FormData fd = new FormData("photo", entity);        
    FormDataSet fds = new FormDataSet();
    fds.setMultipart(true);
    fds.setMediaType(MediaType.MULTIPART_FORM_DATA);
    fds.getEntries().add(fd);

    String url = "http://localhost/uploadFile";
    Optional<JsonRepresentation> opJrep = m_RestClient.postJson(url,fds,MediaType.MULTIPART_FORM_DATA, Optional.empty());

}

using the following Method to post the Form and Receive a JSON Representation (postJson meaning post, get json back)

public Optional<JsonRepresentation> postJson(String url, Object sendObject,MediaType mediaType,Optional<Collection<? extends Header>> headers){
    //build resource
    ClientResource resource = new ClientResource(url);

    //build request with headers
    Request request = new Request(Method.POST,url);

    headers.ifPresent(col->{
        request.getHeaders().addAll(col);
    });

    //set request
    resource.setRequest(request);

    //get response
    resource.post(sendObject,mediaType);
    Representation responseEntity = resource.getResponseEntity();

    System.out.println(responseEntity.toString());

    try {
        //get json representation
        JsonRepresentation json = new JsonRepresentation(responseEntity);
        return Optional.of(json);
    } catch (Exception e) {
    }

    return Optional.empty();
}

The receiving server should return a JSON string, when everything is fine.

The Endpoint actually is not my localhost, but a Telegram Bot with the SendPhoto Method. There you can post a File as an image using multipart/form-data

SendPhoto Documentation

What could I do wrong? How to upload a file (image in my case) using restlet as a multipart/form-data?

Loki
  • 3,747
  • 4
  • 24
  • 47

1 Answers1

3

I did some try and error programming with the FormDataSet and got the result.

To upload a file (in this case picture) using restlet you have to do the following:

    FileRepresentation entity = new FileRepresentation(file, mediaType); //create the fileRepresentation  

    FormDataSet fds = new FormDataSet(); //create the FormDataSet
    FormData fd = new FormData(key, entity); //create the Formdata using a key and a value (file)       
    fds.getEntries().add(fd); //add the form data to the set
    fds.setMultipart(true); //set the multipart value to true

    String url = "http://localhost/uploadPhoto";
    Optional<JsonRepresentation> opJrep = m_RestClient.postJson(url,fds,MediaType.MULTIPART_FORM_DATA, Optional.empty());

This example uses the same postJson method as described in the question.

Loki
  • 3,747
  • 4
  • 24
  • 47