Sorry I didn't realize this was the Java-specific discussion - so I
assume you're using Java!

To get you started:

I've used a library from 
http://forums.sun.com/thread.jspa?forumID=31&threadID=451245
to create the multi-part POST request

My code gets an image's data from a URL, uploads it to the Blobstore,
and returns the blobkey.  I'll pick it up from where I get the data

                byte[] data = new byte[contentLength];
                int bytesRead = 0;
                int offset = 0;
                while (offset < contentLength) {
                        bytesRead = in.read(data, offset, data.length - offset);
                        if (bytesRead == -1)
                                break;
                        offset += bytesRead;
                }
                in.close();

                //create POST to upload to blogstore
            BlobstoreService blobstoreService =
BlobstoreServiceFactory.getBlobstoreService();
            String uploadURL = blobstoreService.createUploadUrl("/
uploadblob");

            //add host if in dev mode
            if (uploadURL.indexOf("http") == -1)
                uploadURL = "http://127.0.0.1:8888"; + uploadURL;

                url = new URL(uploadURL);
                // create a boundary string
                String boundary = MultiPartFormOutputStream.createBoundary();
                URLConnection urlConn =
MultiPartFormOutputStream.createConnection(url);
                urlConn.setReadTimeout(15000);
                urlConn.setRequestProperty("Accept", "*/*");
                urlConn.setRequestProperty("Content-Type",
                        MultiPartFormOutputStream.getContentType(boundary));
                // set some other request headers...
                urlConn.setRequestProperty("Connection", "Keep-Alive");
                urlConn.setRequestProperty("Cache-Control", "no-cache");
                // no need to connect because getOutputStream() does it
                MultiPartFormOutputStream out =
                        new 
MultiPartFormOutputStream(urlConn.getOutputStream(), boundary);
                // write a text field element - This should be removed - left 
over
from multipart library demo code
                out.writeField("myText", "text field text");
                // write bytes directly
                out.writeFile("myFile", contentType, fileName, data);
                out.close();

                // read response from server
                BufferedReader responseIn = new BufferedReader(
                        new InputStreamReader(urlConn.getInputStream()));
                StringBuilder redirectResponse = new StringBuilder();
                String line="";
                while((line = responseIn.readLine()) != null) {
                        redirectResponse.append(line);
                }
                in.close();

                //extract the blobstore key from the response
                String blogKeyStr =
Utilities.findSubString(redirectResponse.toString(), "blogkey = '",
"'</H1>");

                if (blogKeyStr == null || blogKeyStr.equals("FAILED"))
                        throw new IOException("Failed to upload image blob");

                return blogKeyStr;

==========================================

To make it work you also need a couple servlets (make sure you add
them to web.xml):

public class UploadBlob extends HttpServlet {

    private BlobstoreService blobstoreService =
BlobstoreServiceFactory.getBlobstoreService();

    public void doPost(HttpServletRequest req, HttpServletResponse
res)
        throws ServletException, IOException {

        Map<String, BlobKey> blobs =
blobstoreService.getUploadedBlobs(req);
        BlobKey blobKey = blobs.get("myFile");
        String resultKey = "UPLOAD-FAILED";
        if (blobKey != null) {
                resultKey = blobKey.getKeyString();
        }

        res.sendRedirect("/blobuploadredirect?blobkey=" + resultKey);

    }
}

==========================================
public class Blobuploadredirect extends HttpServlet {
  public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
          String blobkey = request.getParameter("blobkey");
          if (blobkey == null)
                  blobkey = "FAILED";

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                                        "Transitional//EN\">\n" +
                "<HTML>\n" +
                "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
                "<BODY>\n");

    out.println("<H1>blogkey = '" + blobkey + "'</H1></BODY></HTML>");
  }
}

-- 
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