이 기사에서는 포스트를 모방하여 양식을 제출하여 Java의 이미지 업로드 기능을 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
모의 양식 HTML은 다음과 같습니다.
<form action = "up_result.jsp"method = "post"enctype = "multipart/form-data"name = "form1"id = "form1"> <label> <input type = "text"name = "name"value = "" /> < /label> <label> <input type = "file"name = "userfile" /> < /label> <label> <입력 유형 = "value ="upload " /> < /label >/양식>
Java 코드는 다음과 같습니다.
com.yanek.util; java.net.haststream; net .sf.json.jsonarray; import net.sf.json.jsonobject; ///0.jpg "; String urlstr ="http://127.0.0.1:8080/minicms/up/up/up/up_result.jsp "; map <string, string> textmap = new Hashmap <string, String> (); textmap .put ( "이름", "testName"); , filemap); System.out.println (ret)} * im String> textmap, map <string, string> filemap) {string res = ""; --------------------------------------------------------- --------------------------------------------------------- --------------------------------------------------------- ------------------------------123821742118716 "; // 경계는 요청 헤더 및 업로드 파일의 구분 기호입니다. url url = new URL (httpurlConnection). Conn.setEcaches (false); "Post"; Windows NT 6.1; ZH-CN; getoutputStream (); = (map.entry) iter.next (string) getkey (string inputValue = getValue (inputValue == null); ( "/r/n"). Append ( "-"). Append (경계). Append ( "/r/n"); + inputname + "/"/r/n/r/n "); strbuf.append (inputValue);} out.write (strbuf.toString () .getBytes ());} // 파일 if (filemap! = null ) {iterator iter = filemap.entryset (). iterator (); String inputValue = (string) getValue (inputValue == null) {inputValue (inputValue); .GetContentType (file); if (filename.endswith ( ". png")) {contenttype = "inconstype == null || contenttype.equals (")) {contenttype = "Application. /Octet-stream ";} StringBuffer strbuf = new StringBuffer (); strbuf.append ("/r/n "). Ap Pend ("-") .append (경계) .append ("/r/n ") strbuf.append ( "내용-데이터; 이름 =/" " + inputname +"/"; filename =/" + filename + "/"/r/n "); 유형 : " + contenttype +"/r/n/r/n "); out.write (strbuf.toString (). getBytes ()); datainputStream in = new DatainputStream (new FileInputStream (file)); int bytes = 0 BYTE [] 새로운 바이트 [1024]; } byte [] endData = ( "/r/n-"-/r/n ". getBytes (); // RETURNBUFFER STRBUF = 새로운 BufferedReader (conn.getInputStream ()); = null) {strbuf.append ( "오류는 사후 요청을 보내는 것이 발생했습니다. " + urlstr); e.printstacktrace ();} 마지막으로 {if (conn! = null) {conn.disconnect (); conn = null;}} return res;}}이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.