<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 17,  comments - 12,  trackbacks - 0

    package com.test.servlet;

     

    import java.io.File;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.io.InputStream;  
    import java.io.OutputStream;  
    import java.util.List;  
     
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
     
    import org.apache.commons.fileupload.FileItem;  
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
    import org.apache.commons.fileupload.servlet.ServletFileUpload;  
     

    //


    public class UploadServlet extends HttpServlet {


     
     
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      

             DiskFileItemFactory factory = new DiskFileItemFactory();
            
      String path= request.getRealPath("/upload");
      

      
      factory.setRepository(new File(path));
      
      factory.setSizeThreshold(1024*1024);
      
      ServletFileUpload upload=new ServletFileUpload(factory);
      
      try
      {
       List<FileItem> list = upload.parseRequest(request);
       for(FileItem item : list)
       {
        if(item.isFormField())
        {
         String name=item.getFieldName();
         
         String value=item.getString("gbk");
         
         System.out.println(name);
         
         request.setAttribute(name, value);
         
        }
        else
        {
         String name=item.getFieldName();
         
         String value=item.getName();
         
         int start=value.lastIndexOf("\\");
         
         String fileName=value.substring(start+1);
         
         request.setAttribute(name, fileName);
         
         OutputStream os=new FileOutputStream(new File(path,fileName));
         
         InputStream is =item.getInputStream();
         
         byte[] buffer=new byte[400];
         
         int length=0;
         
         while((length=is.read(buffer))>0){
          
          
          os.write(buffer, 0, length);
          
         }
         os.close();
         is.close();
        
            
        } 
        
       }
       
       
      }
     
      
      catch(Exception ex){
       
       ex.printStackTrace();
       
      }
      request.getRequestDispatcher("upload/result2.jsp").forward(request,response);
      
      

     }

    }

    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
       
        <title>My JSP 'result2.jsp' starting page</title>
       
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">   
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

      </head>
     
      <body>
        username:${requestScope.username}<br>
        password:${requestScope.password}<br>
       
        file1:${requestScope.file1}<br>
       
        file2:${requestScope.file2}
         
       
       
      </body>
    </html>


    <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
       
        <title>My JSP 'upload2.jsp' starting page</title>
       
     <meta http-equiv="pragma" content="no-cache">
     <meta http-equiv="cache-control" content="no-cache">
     <meta http-equiv="expires" content="0">   
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     <meta http-equiv="description" content="This is my page">
     <!--
     <link rel="stylesheet" type="text/css" href="styles.css">
     -->

      </head>
     
      <body>
      <form action="/struts2/UploadServlet"  method="post" enctype="multipart/form-data">
     
      username:<input type="text" name="username" ><br>
      password:<input type="password" name="password"><br>
     
      file1:<input type="file" name="file1">
      file2:<input type="file" name="file2">
     
      <input type="submit" value=" submit ">
     
     
     
      </form>
     
     
      </body>
    </html>

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4"
     xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
     
    <!--   
     <filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
     </filter>
     
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
        
     </filter-mapping>
     -->
     
     

      <servlet>
        <servlet-name>UploadServlet</servlet-name>
        <servlet-class>com.test.servlet.UploadServlet</servlet-class>
      </servlet>

      <servlet-mapping>
        <servlet-name>UploadServlet</servlet-name>
        <url-pattern>/UploadServlet</url-pattern>
      </servlet-mapping>
    </web-app>

    posted on 2009-03-18 22:07 asiawang 閱讀(449) 評論(1)  編輯  收藏

    FeedBack:
    # re: servlet實現文件上傳操作
    2012-05-12 13:36 | 薩達發撒旦法
    薩達發撒旦法地方  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2012年5月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章檔案

    友情連接

    搜索

    •  

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 永久在线观看免费视频| 免费人成动漫在线播放r18| 亚洲国产精品无码一线岛国| 亚洲日韩一页精品发布| 亚洲AV无码成人网站久久精品大| 亚洲国产精品yw在线观看| 亚洲人成网站日本片| 免费成人福利视频| 永久免费视频v片www| 亚洲精品无码久久久| 亚洲成人动漫在线| 亚洲AV无码专区亚洲AV桃| 大地资源网高清在线观看免费 | 永久亚洲成a人片777777| 西西人体44rt高清亚洲| 午夜网站在线观看免费完整高清观看| 99久久99久久精品免费看蜜桃| 亚洲精品国产精品乱码不卡| h片在线播放免费高清| 日本黄网站动漫视频免费| 亚洲一级视频在线观看| 国产色爽免费无码视频| 亚洲欧洲日产国码久在线观看| 免费国产在线精品一区| 亚洲色大成网站www永久一区| 黄色片免费在线观看| 亚洲精品亚洲人成在线观看下载| 一级毛片高清免费播放| 日韩免费观看视频| 亚洲精品国产国语| 鲁大师在线影院免费观看| 亚洲人成人77777网站| 免费毛片a在线观看67194 | 国产一区二区免费在线| 亚洲白嫩在线观看| 日本免费电影一区二区| 免费国产小视频在线观看| 亚洲欧洲专线一区| 日韩视频在线精品视频免费观看| 国产精品日本亚洲777| 人妻视频一区二区三区免费|