日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springmvc 文件上传和拦截器

發布時間:2025/3/20 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc 文件上传和拦截器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件上傳:

單文件:

@Controller public class FirstController {@RequestMapping("/first")public String doFrist(MultipartFile upload, HttpSession session){//文件不能為空if (upload.getSize()>0){String childpath=upload.getOriginalFilename();//限定類型/* if (childpath.endsWith("jsp")){}*/String fatherpath=session.getServletContext().getRealPath("/upload");System.out.print(fatherpath);//拼接路徑File file=new File(fatherpath,childpath);try {//上傳upload.transferTo(file);} catch (IOException e) {e.printStackTrace();}}else{System.out.print("文件為空");}return "/index.jsp";} }

  配置文件:

<context:component-scan base-package="day013shangchuan"></context:component-scan><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="50000000"></property> //上傳文件的總大小<property name="maxUploadSizePerFile" value="50000"></property>//單個文件的大小<property name="defaultEncoding" value="utf-8"></property> //編碼的字符集</bean><mvc:annotation-driven></mvc:annotation-driven>

  jsp頁面::

<form action="/first" method="post" enctype="multipart/form-data">文件:<input type="file" name="upload"><input type="submit"> </form>

  多文件上傳:

@Controller public class FirstControllers {@RequestMapping("/firsts")public String doFrist(@RequestParam MultipartFile [] upload, HttpSession session){for (MultipartFile item:upload){//文件不能為空if (item.getSize()>0){String childpath=item.getOriginalFilename();//限定類型/* if (childpath.endsWith("jsp")){}*/String fatherpath=session.getServletContext().getRealPath("/upload");System.out.print(fatherpath);//拼接路徑File file=new File(fatherpath,childpath);try {//上傳item.transferTo(file);} catch (IOException e) {e.printStackTrace();}}else{System.out.print("文件為空");}}return "/index.jsp";} }

 配置和上面的一樣

? jsp頁面:

<form action="/first" method="post" enctype="multipart/form-data">文件:<input type="file" name="upload">文件1:<input type="file" name="upload">文件2:<input type="file" name="upload"><input type="submit"> </form>

? ?

?

? ?攔截器:

? ? ??

public class FirstInter implements HandlerInterceptor {public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {System.out.println("111");return true;}public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("333");}public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("444");}}

  

配置文件:

??

<context:component-scan base-package="day013shangchuan"></context:component-scan><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="50000000"></property><property name="maxUploadSizePerFile" value="50000"></property><property name="defaultEncoding" value="utf-8"></property></bean><mvc:interceptors><mvc:interceptor><mvc:mapping path="/**"/><bean class="day014lanjie.FirstInter"></bean></mvc:interceptor></mvc:interceptors><mvc:annotation-driven></mvc:annotation-driven>

  

?

?

?

? ? ? ??

轉載于:https://www.cnblogs.com/xu06123/p/8694317.html

總結

以上是生活随笔為你收集整理的springmvc 文件上传和拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。