鍒涘缓鏂囦欢涓婁紶琛ㄥ崟

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>

鎶€鏈垎浜浘鐗? src=

鍚庡彴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锛岀晫闈㈠涓嬫墍绀猴紝骞朵笂浼犳枃浠讹細

鎶€鏈垎浜浘鐗? src=
濡傛灉浣犵殑JSP鑴氭湰杩愯姝e父锛屾枃浠跺皢琚笂浼犺嚦 c:apache-tomcat-5.5.29webappsdata 锛屼綘鍙互鎵撳紑鏂囦欢澶圭湅鐪嬫槸鍚︿笂浼犳垚鍔熴€?/div>

以上是关于鍒涘缓鏂囦欢涓婁紶琛ㄥ崟的主要内容,如果未能解决你的问题,请参考以下文章

python鎺ュ彛鑷姩鍖?-post鏂囦欢涓婁紶

闆嗚绗叚澶╋細鏂囦欢涓婁紶婕忔礊

閰嶇疆php.ini 淇敼php鏂囦欢涓婁紶澶у皬

閫夋嫨鏂囦欢杩涜涓婁紶

閫氳繃`RestTemplate`涓婁紶鏂囦欢(InputStreamResource璇﹁В)

Jquery.form寮傛涓婁紶鏂囦欢甯歌闂瑙e喅