Problem description
Server Interface:
HTTP request method: Post/form
Http://www.iyouchai.com:8888/upload
Input parameters:
File: Upload file name
Username: User Name
return value:
{
Fileid: "85635dee-d326-11e3-a001-00163e0202e9.jpg",
Createtime: "2014-05-03 20:13:48"
}
HTML Test format:
<form action= "/upload/" enctype= "Multipart/form-data" method= "POST" > <input name= "file" type= "file" ; <input type= "Submit" value= "Submit" > <input type= "text" name= "username" value= "[email protected]" > </form>
Upload the image code:
/** * @Description: todo (with reference, file upload) * @param actionUrl * @param params * @param files * @return result * @throws ioexception */public static string post (string actionurl, map<string, string> params,map<string, file> files) throws ioexception {string boundary = java.util.uuid.randomuuid (). toString (); string prefix = "--", linend = "\ r \ n"; string multipart_from_data = "Multipart/form-data"; string charset = "UTF-8"; Url uri = new url (ActionURL); httpurlconnection conn = (HttpURLConnection) uri.openconnection (); Conn.setReadTimeout (5 * 1000) Conn.setdoinput (true);// allow input conn.setdooutput (true);// allow output conn.setusecaches ( False), Conn.setrequestmethod ("POST"), // post mode Conn.setrequestproperty ("CoNnection ", " keep-alive ") Conn.setrequestproperty (" Charsert ", " UTF-8 "); Conn.setrequestproperty (" Content-type ", multipart_from_data+ "; boundary= " + boundary);// First, set the parameter Stringbuilder sb = new stringbuilder ();for (map.entry<string, ) for the type of the spelling text String> entry : params.entryset ()) {sb.append (PREFIX); sb.append (boundary); Sb.append ( Linend); Sb.append ("Content-disposition: form-data; name=\" "+ entry.getkey () + " \ "" + linend); Sb.append ("content-type: text/plain; charset=" + CHARSET + Linend); Sb.append ("Content-transfer-encoding: 8bit" + linend); Sb.append (LINEND); Sb.append ( Entry.getvalue ()); Sb.append (linend);} Dataoutputstream outstream = new dataoutputstream (Conn.getoutputstream ()); OutStream.write ( Sb.tostring (). GetBytes ());// send file Data if (files != null) for (map.entry<string, File> fIle : files.entryset ()) {stringbuilder sb1 = new stringbuilder (); sb1.append (PREFIX); sb1.append (boundary); Sb1.append (linend); Sb1.append ("Content-disposition: form-data; name=\" File\ "; filename=\" "+ file.getkey () + " \ "" + linend) Sb1.append ("Content-type: application/octet-stream; charset= "+ charset + linend); Sb1.append (LINEND); o Utstream.write (Sb1.tostring (). GetBytes ()); Inputstream is = new fileinputstream ( File.getvalue ()); LOG.I ("File Size", file.getvalue (). Length () + ""); byte[] buffer = new byte[1024] ;int len = 0;while ((len = is.read (buffer)) != -1) { Outstream.write (Buffer, 0, len);} Is.close (); Outstream.write (Linend.getbytes ());} Request End Flag byte[] end_data = (prefix + boundary + prefix + linend). GetBytes (); Outstream.write (end_data); outstrEam.flush ();// Get Response code Int res = conn.getresponsecode (); LOG.I ("Image upload result", res + ""); Inputstream in = conn.getinputstream (); I Nputstreamreader isreader = new inputstreamreader (in); Bufferedreader bufreader = new bufferedreader (Isreader); string line = null; string data = "OK";while ((Line = bufreader.readline ()) == null) data += line;if (res == 200) {int ch; Stringbuilder sb2 = new stringbuilder ();while ((Ch = in.read ()) != -1) {sb2.append ((char) ch);}} Outstream.close (); Conn.disconnect (); return in.tostring ();}
But each return is 404,not found
Do not use the browser to test the interface directly, the interface only supports the Post solution 1
Request address error, please add "/"; http://www.iyouchai.com:8888/upload/
Your method does not provide a GET method. If the path is right, it can be accessed directly using the URL. The result is a blank page, and if the Get method is provided, the display does not allow this method to be used.
HTTP POST request error FileNotFoundException 404