Este artículo describe la función de carga de imágenes de Java imitando la publicación para enviar formularios. Compártelo para su referencia, como sigue:
El formulario simulado HTML es el siguiente:
<form de acción = "up_result.jsp" método = "post" enctype = "multipart/form-data" name = "form1" id = "form1"> <label> <input type = "text" name = "name" valor = "" /> < /etiqueta> <label> <input type = "file" name = "userfile" /> </label> <label> <input type = "envíe" value = "cargar" /> < /etiqueta >/Forma>
El código Java es el siguiente:
paquete com.yanek.util; java.io.outputstream; net .sf.json.jsonArray; /0.jpg "; string urlstr =" http://127.0.0.1:8080/minicms/up/up_result.jsp "; map <string, string> textMap = nuevo hashmap <string, string> (); te xtMap .put ("name", "testName"); , fileMap); String> TextMap, MAP <String, String> FilEmap) {String Res = ""; -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------- contento {url url = nuevo url (urlstr); ; ; Windows NT 6.1; GetOutputStream ()); = (Map.entry) iter.next (); ("/r/n"). append ("-"). append (límite) .append ("/r/n"); + inputName + "/"/r/n/r/n "); strbuf.append (inputValue);} out.write (strbuf.ToString () .getBytes ());} // archivo if (fileMap! = null ) {Iterator iteri = fileMap.entryset (). Iterator (); ); .getContentType (archivo); /Octet-stream ";} StringBuffer strBuf = new StringBuffer (); strbuf.append ("/r/n "). AP Pend ("-") .Append (límite) .Append ("/r/n ") ; Tipo: " + ContentType +"/r/n/r/n "); out.write (strbuf.ToString (). GetBytes ()); DataInputStream in = new DataInputStream (new FileInputStream (archivo)); int bytes ; } byte [] enddata = ("/r/n--" + límite + "-/r/n"). GetBytes (); ; / Lea los datos de retorno strbuffer = new StringBuffer (); = NULL) {strBUf.append (línea) .Append ("/n"); ("Se produjo un error enviando una solicitud posterior. " + urlstr); E.PrintStackTrace ();} finalmente {if (conn! = null) {conn.disconnect (); conn = null;}} return Res;}}Espero que este artículo sea útil para la programación Java de todos.