1

The image file is attahced as a parameter through the Webservice Ws.url("controller action").files(imageFile) in play framework 1.2.4. How to receive that image file in that controller? Can anyone please help me on this.

1 Answers1

0

Let us post two files:

WS.url("http://127.0.0.1:9000/process")
  .files(new File("kitten.jpg"), new File("dog.jpg"))
  .post();

The controller method looks like this:

public static void process(File dummy) {
  List<Upload> uploads = (List<Upload>) request.args.get("__UPLOADS");

  for (Upload upload : uploads) {
    System.out.println("Uploaded file name:         " + upload.getFileName());
    System.out.println("Uploaded file is stored as: " + upload.asFile());
  }
}

Very ugly, but this is the only way that I got it to work. The dummy parameter must be there, otherwise the uploads request argument will be null.

The images are accessible in different ways through the Upload objects.

Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50
  • Vesteras: Thanks a lot dude :) How can i pass an Object as a parameter in the same way and receive it in a controller? I want to pass the Mvc Model object as the parameter. – user1737507 Oct 29 '12 at 10:27
  • Why would you use WS.url to do that? – Werner Kvalem Vesterås Oct 29 '12 at 10:55
  • Actually i have created two play applications. I want to make communication between these two applications. Iam using Ws.url to make the communication by calling the other application's controller along with the ip address from one application. In this context, i want to pass a file and an object(MVC model object) to other application's controller. If object cannot be passed using WS.url, what is the better way to send an object from one application's controller to another application's controller? I explored on this, but couldn't able to get proper solution for this. please help me on this. – user1737507 Oct 30 '12 at 05:25
  • One approach would be to serialize the MVC model object to JSON, and then use WS.url().body().post() to send it. On the receiving end, you would deserialize the JSON structure in the body parameter back to a MVC model object. – Werner Kvalem Vesterås Oct 30 '12 at 07:14
  • How can i deserialize an json string to an object? – user1737507 Oct 31 '12 at 08:52
  • @ Werner Vesterås: How can i deserialize an json string to an object? In the first application's controller i serailized an object to json string using JsonWriter like "String serializedObject = JsonWriter.toJson(MyModelObject);" and passing the json string to another application's controller. In that controller, iam trying to deserialize the json string back to object using JsonReader like "Object deserializedObj=JsonReader.toJava(serializedAsset );". The deserializedObj is coming as null after deserailizing and but the serialized string has all the object data.Can u pls give me any solution? – user1737507 Oct 31 '12 at 09:00
  • Please create a new question. – Werner Kvalem Vesterås Oct 31 '12 at 09:06