java - Why is this POST request through HttpURLConnection throwing a FileNotFoundException? -
update 3: tried execute request against requestb.in insteaad of blobstore url , same exception occured, looks problem upload method using httpurlconnection , not blobstore specific issue.
update 2: added code creating upload url method @ bottom
update 1: added code , exact exception below question. hope situation clearer now.
files getting uploaded blobstore using post request url provided createuploadurl()
method. however, app engine still responds filenotfoundexception upload requests complaining file not found @ https://<app-id>.appspot.com/_ah/upload/<long-random-string>
what going on here?
upload code:
try { multipartutility multipartutility = new multipartutility(blobuploadurl, "utf-8",writelistener); multipartutility.addformfield("key_name", keyname); multipartutility.addfilepart("file", filename, is); multipartutility.finish(); } catch (ioexception e) { }
multipartutility class:
public class multipartutility { private final string boundary; private static final string line_feed = "\r\n"; private httpurlconnection httpconn; private string charset; private outputstream outputstream; private printwriter writer; private writelistener listener; public static interface writelistener{ void transferred(long num); } public multipartutility(string requesturl, string charset,writelistener listener) throws ioexception { this.charset = charset; // creates unique boundary based on time stamp boundary = "===" + system.currenttimemillis() + "==="; url url = new url(requesturl); httpconn = (httpurlconnection) url.openconnection(); httpconn.setusecaches(false); httpconn.setdooutput(true); // indicates post method httpconn.setdoinput(true); httpconn.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); httpconn.setchunkedstreamingmode(-1); outputstream=httpconn.getoutputstream(); this.listener=listener; writer = new printwriter(new outputstreamwriter(outputstream, charset), true); } public void addformfield(string name, string value) { writer.append("--" + boundary).append(line_feed); writer.append("content-disposition: form-data; name=\"" + name + "\"") .append(line_feed); writer.append("content-type: text/plain; charset=" + charset).append( line_feed); writer.append(line_feed); writer.append(value).append(line_feed); writer.flush(); } public void addfilepart(string fieldname, string filename, inputstream is) throws ioexception { //string filename = uploadfile.getname(); writer.append("--" + boundary).append(line_feed); writer.append( "content-disposition: form-data; name=\"" + fieldname + "\"; filename=\"" + filename + "\"") .append(line_feed); writer.append( "content-type: " + urlconnection.guesscontenttypefromname(filename)) .append(line_feed); writer.append("content-transfer-encoding: binary").append(line_feed); writer.append(line_feed); writer.flush(); byte[] buffer = new byte[4096]; int bytesread = -1; while ((bytesread = is.read(buffer)) != -1) { outputstream.write(buffer, 0, bytesread); } outputstream.flush(); writer.append(line_feed); writer.flush(); } public void addheaderfield(string name, string value) { writer.append(name + ": " + value).append(line_feed); writer.flush(); } public list<string> finish() throws ioexception { list<string> response = new arraylist<string>(); writer.append(line_feed).flush(); writer.append("--" + boundary + "--").append(line_feed); writer.close(); bufferedreader reader = new bufferedreader(new inputstreamreader( httpconn.getinputstream())); string line = null; while ((line = reader.readline()) != null) { response.add(line); } // checks server's status code first int status = httpconn.getresponsecode(); if (status != httpurlconnection.http_ok) { throw new ioexception("server returned non-ok status: " + status); } reader.close(); httpconn.disconnect(); return response; } }
io exception caught when upload request executed:
java.io.filenotfoundexception: https://<app-id>.appspot.com/_ah/upload/<random-long-string>
code creating blobstore upload url: (note can see uploaded files in blobstore , confirm blob handler servlet getting key of uploaded blob despite exception.)
blobstoreservice bs = blobstoreservicefactory.getblobstoreservice(); string blobuploadurl = bs.createuploadurl("/save/blob");
Comments
Post a Comment