0

So I'm trying to make a multipart-form POST and I want to attach a org.apache.http.entity.mime.content.FileBody to the MultipartEntity that I'm going to be posting. Now I've got the raw string file data that I want to populate the FileBody with already. However, this project is using Google App Engine which prohibits every way I've seen of generating the FileBody. Anyone know how to create a FileBody object and populate it in GAE?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Dangerbunny
  • 81
  • 1
  • 3
  • 10
  • Which ways have you seen it prohibit, and with which errors/warnings? – nanofarad Jul 25 '13 at 22:11
  • It restricts you from using classes that can write to files, like FileWriter and FileOutputStream. You need to make a java.io.File object in order to make a FileBody object – Dangerbunny Jul 25 '13 at 22:12
  • Basically it won't let you write to the user's local machine. I'm wondering if there's a way to create and populate a java.io.File object without writing to the user's local machine – Dangerbunny Jul 25 '13 at 22:16

1 Answers1

0

So just ignore FileBody. You want to use the method MultipartEntity.addPart(ContentBody content). This works with FileBody, because FileBody's parent class implements ContentBody.

ContentBody is a super simple interface with just two methods. Create a class that implements it, create an instance of your class, and pass it in to the addPart method.

public ByteContentBody implements ContentBody {
  private String name;
  private byte[] data;

  public ByteContentBody(String name, byte[] data) {
    this.name = name;
    this.data = data;
  }

  public String getFilename(){
    returns name;
  }

  public void writeTo(OutputStream out) throws IOException {
    out.write(data);
  }
}

http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html

http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ContentBody.html

Robert Parker
  • 605
  • 4
  • 7
  • Thanks, this is what I ended up using, but with the apache HttpClient and HttpPost object to facilitate the post as well – Dangerbunny Aug 01 '13 at 22:38