鍒涘缓鏂囦欢涓婁紶琛ㄥ崟
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍒涘缓鏂囦欢涓婁紶琛ㄥ崟相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/ons' title='ons'>ons
ast cal 鍚﹀垯 cep etc 鍐欏叆 琛ㄥ崟 澶勭悊绋嬪簭JSP鍙互閫氳繃HTML鐨刦orm琛ㄥ崟涓婁紶鏂囦欢鍒版湇鍔″櫒銆?鏂囦欢绫诲瀷鍙互鏄枃鏈枃浠躲€佷簩杩涘埗鏂囦欢銆佸浘鍍忔枃浠剁瓑鍏朵粬浠讳綍鏂囨。銆?/p>
鍒涘缓鏂囦欢涓婁紶琛ㄥ崟
鎺ヤ笅鏉ユ垜浠娇鐢℉TML鏍囩鏉ュ垱寤烘枃浠朵笂浼犺〃鍗曪紝浠ヤ笅涓鸿娉ㄦ剰鐨勭偣锛?/p>
- form琛ㄥ崟 method 灞炴€у繀椤昏缃负 POST 鏂规硶 锛屼笉鑳戒娇鐢?GET 鏂规硶銆?/li>
- form琛ㄥ崟 enctype 灞炴€ч渶瑕佽缃负 multipart/form-data銆?/li>
- form琛ㄥ崟 action 灞炴€ч渶瑕佽缃负鎻愪氦鍒板悗鍙板鐞嗘枃浠朵笂浼犵殑jsp鏂囦欢鍦板潃銆備緥濡?nbsp;uploadFile.jsp 绋嬪簭鏂囦欢鐢ㄦ潵澶勭悊涓婁紶鐨勬枃浠躲€?/li>
- 涓婁紶鏂囦欢鍏冪礌闇€瑕佷娇鐢?<input .../> 鏍囩锛屽睘鎬ц缃负 type="file"銆傚鏋滈渶瑕佷笂浼犲涓枃浠讹紝鍙互鍦?<input .../>鏍囩涓缃笉鍚岀殑鍚嶇О銆?/li>
浠ヤ笅鏄竴涓笂浼犳枃浠剁殑琛ㄥ崟锛屽疄渚嬪涓嬶細
<html> <head> <title>File Uploading Form</title> </head> <body> <h3>File Upload:</h3> Select a file to upload: <br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="file" name="file" size="50" /> <br /> <input type="submit" value="Upload File" /> </form> </body> </html>
鍦ㄤ綘鏈湴娴忚鍣ㄨ闂鏂囦欢锛屾樉绀虹晫闈㈠涓嬫墍绀猴紝鍦ㄤ綘鐐瑰嚮"Upload File"浼氬脊鍑轰竴涓獥鍙h浣犻€夋嫨瑕佷笂浼犵殑鏂囦欢锛?/p>
鍚庡彴JSP澶勭悊鑴氭湰
棣栧厛鎴戜滑鍏堝畾涔夋枃浠朵笂浼犲悗瀛樺偍鍦ㄦ湇鍔′笂鐨勪綅缃紝浣犲彲浠ュ皢璺緞鍐欏湪浣犵殑绋嬪簭褰撲腑锛屾垨鑰呮垜浠彲浠ュ湪web.xml閰嶇疆鏂囦欢涓€氳繃璁剧疆 context-param 鍏冪礌鏉ヨ缃枃浠跺瓨鍌ㄧ殑鐩綍锛屽涓嬫墍绀猴細
<web-app> .... <context-param> <description>鏂囦欢涓婁紶鍦板潃</description> <param-name>file-upload</param-name> <param-value> c:apache-tomcat-5.5.29webappsdata </param-value> </context-param> .... </web-app>
浠ヤ笅鑴氭湰鏂囦欢UploadFile.jsp鍙互澶勭悊澶氫釜涓婁紶鐨勬枃浠讹紝鍦ㄤ娇鐢ㄨ鑴氭湰鍓嶏紝鎴戜滑闇€瑕佹敞鎰忎互涓嬪嚑鐐癸細
- 浠ヤ笅瀹炰緥渚濊禆 FileUpload, 鎵€浠ヤ綘闇€瑕佸湪浣犵殑classpath涓紩鍏ユ渶鏂扮殑 commons-fileupload.x.x.jar 鍖呮枃浠躲€?涓嬭浇鍦板潃涓猴細http://commons.apache.org/fileupload/銆?/li>
- FileUpload 渚濊禆 Commons IO, 鎵€浠ヤ綘闇€瑕佸湪浣犵殑classpath涓紩鍏ユ渶鏂扮殑 commons-io-x.x.jar 銆?涓嬭浇鍦板潃涓猴細http://commons.apache.org/io/銆?/li>
- 鍦ㄦ祴璇曚互涓嬪疄渚嬫椂锛屼綘闇€瑕佷笂浼犵‘璁や笂浼犵殑鏂囦欢澶у皬灏忎簬 maxFileSize 鍙橀噺璁剧疆鐨勫ぇ灏?,鍚﹀垯鏂囦欢鏃犳硶涓婁紶鎴愬姛銆?/li>
- 纭繚浣犲凡缁忓垱寤轰簡鐩綍 c: emp 鍜?c:apache-tomcat-5.5.29webappsdata 銆?/li>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %> <%@ page import="javax.servlet.http.*" %> <%@ page import="org.apache.commons.fileupload.*" %> <%@ page import="org.apache.commons.fileupload.disk.*" %> <%@ page import="org.apache.commons.fileupload.servlet.*" %> <%@ page import="org.apache.commons.io.output.*" %> <% File file ; int maxFileSize = 5000 * 1024; int maxMemSize = 5000 * 1024; ServletContext context = pageContext.getServletContext(); String filePath = context.getInitParameter("file-upload"); // 楠岃瘉涓婁紶鍐呭浜嗙被鍨? String contentType = request.getContentType(); if ((contentType.indexOf("multipart/form-data") >= 0)) { DiskFileItemFactory factory = new DiskFileItemFactory(); // 璁剧疆鍐呭瓨涓瓨鍌ㄦ枃浠剁殑鏈€澶у€? factory.setSizeThreshold(maxMemSize); // 鏈湴瀛樺偍鐨勬暟鎹ぇ浜?maxMemSize. factory.setRepository(new File("c:\temp")); // 鍒涘缓涓€涓柊鐨勬枃浠朵笂浼犲鐞嗙▼搴? ServletFileUpload upload = new ServletFileUpload(factory); // 璁剧疆鏈€澶т笂浼犵殑鏂囦欢澶у皬 upload.setSizeMax( maxFileSize ); try{ // 瑙f瀽鑾峰彇鐨勬枃浠? List fileItems = upload.parseRequest(request); // 澶勭悊涓婁紶鐨勬枃浠? Iterator i = fileItems.iterator(); out.println("<html>"); out.println("<head>"); out.println("<title>JSP File upload</title>"); out.println("</head>"); out.println("<body>"); while ( i.hasNext () ) { FileItem fi = (FileItem)i.next(); if ( !fi.isFormField () ) { // 鑾峰彇涓婁紶鏂囦欢鐨勫弬鏁? String fieldName = fi.getFieldName(); String fileName = fi.getName(); boolean isInMemory = fi.isInMemory(); long sizeInBytes = fi.getSize(); // 鍐欏叆鏂囦欢 if( fileName.lastIndexOf("\") >= 0 ){ file = new File( filePath , fileName.substring( fileName.lastIndexOf("\"))) ; }else{ file = new File( filePath , fileName.substring(fileName.lastIndexOf("\")+1)) ; } fi.write( file ) ; out.println("Uploaded Filename: " + filePath + fileName + "<br>"); } } out.println("</body>"); out.println("</html>"); }catch(Exception ex) { System.out.println(ex); } }else{ out.println("<html>"); out.println("<head>"); out.println("<title>Servlet upload</title>"); out.println("</head>"); out.println("<body>"); out.println("<p>No file uploaded</p>"); out.println("</body>"); out.println("</html>"); } %>
鎺ヤ笅鏉ヨ鎴戜滑閫氳繃娴忚鍣ㄨ闂?nbsp;http://localhost:8080/UploadFile.htm锛岀晫闈㈠涓嬫墍绀猴紝骞朵笂浼犳枃浠讹細
濡傛灉浣犵殑JSP鑴氭湰杩愯姝e父锛屾枃浠跺皢琚笂浼犺嚦 c:apache-tomcat-5.5.29webappsdata 锛屼綘鍙互鎵撳紑鏂囦欢澶圭湅鐪嬫槸鍚︿笂浼犳垚鍔熴€?/div>
以上是关于鍒涘缓鏂囦欢涓婁紶琛ㄥ崟的主要内容,如果未能解决你的问题,请参考以下文章