多檔案上傳與攔截器

來源:互聯網
上載者:User

標籤:class   檔案   ons   origin   rac   路徑   多檔案上傳   nsf   enc   

@Controllerpublic class MostFileController {                      //多檔案上傳@RequestMapping("/first")    public String doFlrat(@RequestParam MultipartFile[] upload, HttpSession session) {        System.out.println("*******************");        for (MultipartFile item:upload) {            if(item.getSize()>0){                //使用者是否選擇了檔案                //擷取到使用者上傳的檔案名稱String chilPath=item.getOriginalFilename(); //檔案段名稱if(chilPath.endsWith(".jpg")||chilPath.endsWith("gif")||chilPath.endsWith("png")){                    //將行對路徑轉換成絕對路徑String paraPath=session.getServletContext().getRealPath("/uplode");                    //將file寫入指定的路徑File  filePath=new File(paraPath,chilPath);                    try {                        //將檔案記憶體運輸到指定的檔案中item.transferTo(filePath);                    } catch (IOException e) {                        e.printStackTrace();                        return "/Fileuplode.jsp";                    }                }else {                    return "/Fileuplode.jsp";                }            }else {                return "/Fileuplode.jsp";            }        }        return "/index.jsp";    }
@RequestMapping("/first2")public String doFirst2(MultipartFile upload,HttpSession session){    System.out.println("****************************88");    if(upload.getSize()>0){        //使用者是否選擇了檔案        //擷取到使用者上傳的檔案名稱String chilPath=upload.getOriginalFilename();  //檔案簡短名稱if(chilPath.endsWith(".jpg")||chilPath.endsWith("gif")||chilPath.endsWith("png")){            //將相對路徑轉化成絕對路徑String paratPath=session.getServletContext().getRealPath("/uplode");            //將file寫入指定的路徑File filePath=new File(paratPath,chilPath);            try {                //將檔案記憶體運輸到指定的檔案中upload.transferTo(filePath);            } catch (IOException e) {                e.printStackTrace();                return "/index.jsp";            }        }else {            return "/Fileuplode.jsp";        }    }else {        return "/Fileuplode.jsp";    }    return "/Fileuplode.jsp"; }
}

MostFileupdlo.xml配置:
  <!--配置包掃描器--><context:component-scan base-package="cn.mostFileupload"></context:component-scan>         <!--設定檔上傳的專用類--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <property name="defaultEncoding" value="utf-8"></property>    <property name="maxUploadSize" value="5000000"></property></bean><mvc:annotation-driven/>
Fileuplode.jsp頁面:   
</head><body><h1>檔案上傳</h1><form action="/first" method="post" enctype="multipart/form-data">    檔案1   <input type="file" name="upload"/>    檔案2   <input type="file" name="upload"/>    檔案3   <input type="file" name="upload"/>    <input type="submit"/></form></body>

Struts2攔截器:  exception 異常攔截器     
                params 參數攔截器
                il8n 國際化攔截器
                fileupload 檔案上傳攔截器
                validation 校正攔截器
Struts2中處理的請求的組件是:Action
SpringMVC中處理請求的組件是:Controller
JSP中處理請求的組件是: servlet
攔截器HandlerInterceptor的三種方法:(1)perHandle() (2)postHandle() (3)afterCompletion()
註冊攔截器:   **匹配0或者更多的目錄
                *匹配0或者任意的字串

  攔截器:
建立Myhanderinter類並整合HandlerInterceptor介面中的方法:
public class Myhanderinter implements HandlerInterceptor {    @Override    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {        System.out.println("perHandle+=========================================");        return true;    }    @Override    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {        System.out.println("posthandle-------------------------------------");    }    @Override    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {        System.out.println("afterHandle====================================");    }}
//在建立一個intercfeption類
@Controllerpublic class intercfeption {  @RequestMapping("/first")    public String doInter(){      System.out.println("Handle=====================================");        return "index.jsp";    }}
  HandleInter.xml配置:
   <!--配置包掃描器--><context:component-scan base-package="cn.Handerinter"></context:component-scan>         <!--註冊攔截器--><mvc:interceptors>    <mvc:interceptor>        <mvc:mapping path="/**"/>        <bean class="cn.Handerinter.Myhanderinter"></bean>    </mvc:interceptor></mvc:interceptors><mvc:annotation-driven/>

多檔案上傳與攔截器

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.