SpringMVC實(shí)現(xiàn)文件上傳與下載、攔截器、異常處理器等功能_第1頁(yè)
SpringMVC實(shí)現(xiàn)文件上傳與下載、攔截器、異常處理器等功能_第2頁(yè)
SpringMVC實(shí)現(xiàn)文件上傳與下載、攔截器、異常處理器等功能_第3頁(yè)
SpringMVC實(shí)現(xiàn)文件上傳與下載、攔截器、異常處理器等功能_第4頁(yè)
SpringMVC實(shí)現(xiàn)文件上傳與下載、攔截器、異常處理器等功能_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第SpringMVC實(shí)現(xiàn)文件上傳與下載、攔截器、異常處理器等功能@RequestMapping("/testDown")

publicResponseEntitybyte[]testResponseEntity(HttpSessionsession)throwsIOException{

//獲取ServletContext對(duì)象

ServletContextservletContext=session.getServletContext();

//獲取服務(wù)器中文件的真實(shí)路徑

StringrealPath=servletContext.getRealPath("/static/img/1.jpg");

//創(chuàng)建輸入流

InputStreamis=newFileInputStream(realPath);

//創(chuàng)建字節(jié)數(shù)組

byte[]bytes=newbyte[is.available()];

//將流讀到字節(jié)數(shù)組中

is.read(bytes);

//創(chuàng)建HttpHeaders對(duì)象設(shè)置響應(yīng)頭信息

MultiValueMapString,Stringheaders=newHttpHeaders();

//設(shè)置要下載方式以及下載文件的名字

headers.add("Content-Disposition","attachment;filename=1.jpg");

//設(shè)置響應(yīng)狀態(tài)碼

HttpStatusstatusCode=HttpStatus.OK;

//創(chuàng)建ResponseEntity對(duì)象

ResponseEntitybyte[]responseEntity=newResponseEntity(bytes,headers,statusCode);

//關(guān)閉輸入流

is.close();

returnresponseEntity;

注意:如果報(bào)500錯(cuò)誤,可能是項(xiàng)目中無法找到靜態(tài)資源文件,需要對(duì)項(xiàng)目重新打包。

文件上傳

文件上傳要求form表單的請(qǐng)求方式必須為post,并且添加屬性enctype="multipart/form-data"以二進(jìn)制方式上傳

SpringMVC中將上傳的文件封裝到MultipartFile對(duì)象中,通過此對(duì)象可以獲取文件相關(guān)信息

上傳步驟:

添加依賴

!--/artifact/commons-fileupload/commons-fileupload--

dependency

groupIdcommons-fileupload/groupId

artifactIdcommons-fileupload/artifactId

version1.3.1/version

/dependency

在SpringMVC的配置文件springMVC.xml中添加配置

!--必須通過文件解析器的解析才能將文件轉(zhuǎn)換為MultipartFile對(duì)象--

!--必須設(shè)置id屬性,springMVC是根據(jù)id獲取,且id必須設(shè)置為multipartResolver--

beanid="multipartResolver"/bean

index.html

formmethod="post"th:action="@{/testUp}"enctype="multipart/form-data"

inputtype="file"name="photo"

inputtype="submit"value="上傳"

/form

控制器

@RequestMapping("/testUp")

//MultipartFile的形參名必須與index.html中的file標(biāo)簽的name一致

publicStringtestUp(MultipartFilephoto,HttpSessionsession)throwsIOException{

//獲取上傳的文件的文件名

StringfileName=photo.getOriginalFilename();

//處理文件重名問題

StringhzName=fileName.substring(fileName.lastIndexOf("."));

fileName=UUID.randomUUID().toString()+hzName;

//獲取服務(wù)器中photo目錄的路徑

ServletContextservletContext=session.getServletContext();

StringphotoPath=servletContext.getRealPath("photo");

Filefile=newFile(photoPath);

if(!file.exists()){

file.mkdir();

StringfinalPath=photoPath+File.separator+fileName;

//實(shí)現(xiàn)上傳功能

photo.transferTo(newFile(finalPath));

return"success";

攔截器

攔截器的配置

SpringMVC中的攔截器用于攔截控制器方法的執(zhí)行

SpringMVC中的攔截器需要實(shí)現(xiàn)HandlerInterceptor接口

HandlerInterceptor源碼

publicinterfaceHandlerInterceptor{

defaultbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{

returntrue;

defaultvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,@NullableModelAndViewmodelAndView)throwsException{

defaultvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,@NullableExceptionex)throwsException{

HandlerInterceptor接口有三個(gè)默認(rèn)方法

preHandle:控制器方法執(zhí)行之前執(zhí)行preHandle(),其boolean類型的返回值表示是否攔截或放行,返回true為放行,即調(diào)用控制器方法;返回false表示攔截,即不調(diào)用控制器方法

postHandle:控制器方法執(zhí)行之后執(zhí)行postHandle()

afterCompletion:處理完視圖和模型數(shù)據(jù),渲染視圖完畢之后執(zhí)行afterCompletion()

控制器

FirstInterceptor.java

publicclassFirstInterceptorimplementsHandlerInterceptor{

publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{

System.out.println("FirstInterceptor--preHandle");

returnfalse;

publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{

System.out.println("FirstInterceptor--postHandle");

publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{

System.out.println("FirstInterceptor--afterCompletion");

SpringMVC的攔截器必須在SpringMVC的配置文件中進(jìn)行配置:

方式一

mvc:interceptors

bean/bean

/mvc:interceptors

輸出:

FirstInterceptor--preHandle

方式二

mvc:interceptors

refbean="firstInterceptor"/ref

/mvc:interceptors

注意提前開啟注解掃描,并把攔截器放入Ioc容器

輸出:

FirstInterceptor--preHandle

注意:以上兩種配置方式都是對(duì)DispatcherServlet所處理的所有的請(qǐng)求進(jìn)行攔截。

方式三

mvc:interceptors

mvc:interceptor

mvc:mappingpath="/**"/!--攔截所有請(qǐng)求--

mvc:exclude-mappingpath="/"/!--不攔截主頁(yè)--

refbean="firstInterceptor"/ref

/mvc:interceptor

/mvc:interceptors

可以進(jìn)入首頁(yè)

發(fā)送任意請(qǐng)求都會(huì)被攔截

輸出:

FirstInterceptor--preHandle

以上配置方式可以通過ref或bean標(biāo)簽設(shè)置攔截器,通過mvc:mapping設(shè)置需要攔截的請(qǐng)求,通過mvc:exclude-mapping設(shè)置需要排除的請(qǐng)求,即不需要攔截的請(qǐng)求

/**:攔截所有請(qǐng)求

/*:攔截一級(jí)目錄的請(qǐng)求

多個(gè)攔截器的執(zhí)行順序

第一個(gè)攔截器

@Component

publicclassFirstInterceptorimplementsHandlerInterceptor{

publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{

System.out.println("FirstInterceptor--preHandle");

returntrue;

publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{

System.out.println("FirstInterceptor--postHandle");

publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{

System.out.println("FirstInterceptor--afterCompletion");

第二個(gè)攔截器

@Component

publicclassSecondInterceptorimplementsHandlerInterceptor{

publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{

System.out.println("SecondInterceptor--preHandle");

returntrue;

publicvoidpostHan

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論