Wow, so far I got it working. So it is possible to do it on the server side. 
I had to fix request url to be absolute instead of relative, or at least I 
thought I had it fixed, and wasn't. 

Here is what I am doing so far:

   private void upload2_A(long fileThingId, long stuffId, String fileName, 
String contentType, byte[] filebytes) {


    String base = sp.getUrl(true);

    

    String surl = 
BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/upload");

    surl = base + surl;


    URL url = null;

    try {

      url = new URL(surl);

    } catch (MalformedURLException e) {

      log.warning("BlobJdo.upload2_A() Error, could not setup url" + 
e.toString());

      e.printStackTrace();

    }


    String boundary = createBoundary();

    

    URLFetchService urlFetch = URLFetchServiceFactory.getURLFetchService();

    FetchOptions fetchOptions = 
FetchOptions.Builder.withDefaults().setDeadline(1000.00);

    HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST, 
fetchOptions);

    request.addHeader(new HTTPHeader("Content-Type", "multipart/form-data; 
boundary=" + boundary));

    request.addHeader(new HTTPHeader("Cookie", sp.getHeader("Cookie")));

    //request.addHeader(new HTTPHeader("Host", "127.0.0.1:8888"));

    //request.addHeader(new HTTPHeader("Origin", "http://127.0.0.1:8888";));

    //request.addHeader(new HTTPHeader("User-Agent", "Mozilla/5.0 (Macintosh; 
U; Intel Mac OS X 10_6_6; en-us) AppleWebKit/533.19.4 (KHTML, like Gecko
)"));

    //request.addHeader(new HTTPHeader("Referer", 
"http://127.0.0.1:8888/index.html?gwt.codesvr=127.0.0.1:9997";));

    //request.addHeader(new HTTPHeader("Accept", "application/xml
,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png
,*/*;q=0.5"));

    //request.addHeader(new HTTPHeader("Accept-Language", "en-us"));

    //request.addHeader(new HTTPHeader("Accept-Encoding", "gzip, deflate"));

    //request.addHeader(new HTTPHeader("Connection", "keep-alive"));

    

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    write(out, "--" + boundary + "\r\n");

    writeParameter(out, "oid", Long.toString(sp.getUserThingId()));

    write(out, "--" + boundary + "\r\n");

    writeParameter(out, "tid", Long.toString(fileThingId));

    write(out, "--" + boundary + "\r\n");

    writeImage(out, fileName, contentType, filebytes);

    write(out, "--" + boundary + "--\r\n"); // end boundary!!!!


    int len = out.toByteArray().length;

    request.addHeader(new HTTPHeader("Content-Length", "" + len));

    request.setPayload(out.toByteArray());

    

    // debug

    echo(request.getHeaders());

    echo(request.getPayload());


    // send request

    HTTPResponse res = null;

    try {

      res = urlFetch.fetch(request);

    } catch (IOException e) {

      e.printStackTrace();

    }

    

    if (res == null) {

      log.warning("BlobJdo.Upload2_A() could not setup urlfetch");

      return;

    }


    InputStream in = new ByteArrayInputStream(res.getContent());

    int data;

    try {

      data = in.read();

      while(data != -1) {

        data = in.read();

      }

      in.close();

    } catch (IOException e) {

      log.warning("BlobJdo.Upload2_A() data write out error" + 
e.toString());

      e.printStackTrace();

    }

  }

  

  private String getRandomStr() {

    return Long.toString(random.nextLong(), 36);

  }


  private String createBoundary() {

    return "----GoneVerticalBoundary" + getRandomStr() + getRandomStr();

  }        


  private void write(OutputStream os, String s) {

    try {

      os.write(s.getBytes());

    } catch (IOException e) {

      e.printStackTrace();

    }

  }


  private void writeParameter(OutputStream os, String key, String value) {

    write(os, "Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n"+ 
value + 
"\r\n");

  }


  private void writeImage(OutputStream os, String fileName, String 
contentType, byte[] image) {

    write(os, "Content-Disposition: form-data; name=\"File\"; filename=\"" + 
fileName + "\"\r\n");

    write(os, "Content-Type: " + contentType + "\r\n\r\n");

    try {

      os.write(image);

    } catch (IOException e) {

      e.printStackTrace();

    }

    write(os, "\r\n");

  }

  

  


  private void echo(List<HTTPHeader> headers) {

    if (headers == null) {

      return;

    }


    Iterator<HTTPHeader> it = headers.iterator();

    while (it.hasNext()) {

      HTTPHeader h = it.next();

      String hh = h.getName();

      String vv = h.getValue();

      System.out.println("***Header: hh=" + hh + " vv=" + vv);

    }


  }


  private void echo(byte[] b) {

    if (b == null) {

      System.out.println("Why is this null");

      return;

    }


    System.out.println("******PAYLOAD START******");


    //byte[] b = baos.toByteArray();

    InputStream in = new ByteArrayInputStream(b);

    int data;

    try {

      data = in.read();


      while(data != -1) {

        System.out.print((char)data);

        data = in.read();

      }

      in.close();

    } catch (IOException e) {

      // TODO Auto-generated catch block

      e.printStackTrace();

    }

    System.out.println("******END PAYLOAD******");

  }

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to