Dieser Artikel beschreibt die Bild -Upload -Funktion von Java durch Nachahmung von Posts, um Formulare einzureichen. Teilen Sie es für Ihre Referenz wie folgt weiter:
Die Scheinform HTML lautet wie folgt:
<form action = "up_result.jsp" methode = "post" engType = "multiPart/form-data" name = "Form1" id = "Form1"> <Label> <Eingabe type = "text" name = "name" Wert = " /> < /label> <label> <Eingabe type =" Datei "name =" userFile " /> < /label> <label> <Eingabe type =" value = "upload" /> < /label >/Form>
Der Java -Code lautet wie folgt:
Paket Com.anek.util; Java.io.outputStream; net .sf.json.jsonArray; //0.jpg "; String urlstr =" http://127.0.0.1:8080/minicms/up/up/up_result.jsp "; map <string, String> textmap = new Hashmap <String> (); te xtmap .put ("Name", "TestName"); , fileMap); String> textmap, map <String, String> filemap) {String res = ""; -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------123821742118716 "; // Grenze ist der Trennzeichen des Anforderungsheaders und Upload-Datei Inhaltsantrieb {URL = Neue URL (URLSTR); ; ; GetOutputStream ()); = (MAP.Entry) iter.Next (); ("/r/n"). Anhänge ("-"). Anhänge (Grenze) .Append ("/r/n"); + inputname + "/"/r/n/r/n "); strbuf.append (inputValue);} out.write (strbuf.toString () .getBytes ());} // Datei if (filemap! = null ) {Iterator iter = fileMap.EntrySet (). Iterator (); ); .GETCONTTYPE (Datei); /Oktett-Stream ";} StringBuffer strbuf = new StringBuffer (); strbuf.append ("/r/n "). AP pend ("-") .Append (Grenze) .Append ("/r/n ") ; Typ: " + contentType +"/r/n/r/n "); out.write (strbuf.toString (). GetBytes ()); DataNputStream in = new DataNputStream (neuer FileInputStream (Datei)); ; } byte [] enddata = ("/r/n--" + bode + "-/r/n"). getBytes (); ; = null) {strbuf.Append (Zeile) .Append ("/n"); ("Es ist ein Fehler beim Senden einer Postanforderung. " + urlstr); e.printstacktrace ();} endlich {if (conn! = null) {conn.disconnect (); conn = null;}} return Res;}}Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.