生活随笔
收集整理的這篇文章主要介紹了
Sring3MVC页面无刷新上传文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Sring3MVC頁面無刷新上傳文件
??? 首先,這個(gè)框架是利用Ajax與Ctroller進(jìn)行表單提交和數(shù)據(jù)交互的,但是Ajax是無法支持File上傳的,所以在這里我使用了隱藏的Iframe來做文件上傳的數(shù)據(jù)交互。(嘿嘿,是新手哦,有什么沒說正確的,請大蝦們指點(diǎn)指點(diǎn))
1.配置Spring
支持web應(yīng)用程序的文件上傳功能,是由spring內(nèi)置的即插即用的MultipartResolver來實(shí)現(xiàn)的,這些解析器都定義在org.springframework.web.multipart包里。下面將使用 CommonsMultipartResolver解析器來實(shí)現(xiàn)簡單的文件上傳功能。
基于項(xiàng)目的要求,我這里是在app-context.xml配置的:
?
?????<bean?id="multipartResolver"?class="org.springframework.web.multipart.commons.CommonsMultipartResolver">??????????????????<property?name="maxUploadSize"?value="9999999999999"?/>?????</bean>? ?
?
2.創(chuàng)建一個(gè)controller(控制器)來處理文件上傳請求,FileUploadController.java:
?
@Controller??public?class?FileUploadController?implements?ServletContextAware{????private?ServletContext?servletContext; ???public?void?setServletContext(ServletContext?servletContext)?{????this.servletContext?=?servletContext; ??}? ?@RequestMapping("uploadTransferFile.do") ???public?void?uploadTransferFile(@RequestParam("fileUpload")?CommonsMultipartFile?mFile, ??????????????????????????????????@RequestParam(value="file_ID",?required?=?true,?defaultValue?=?"")?String?file_ID, ??????????????????????????????????Writer?writer ??????????????????????????????????)?throws?Exception?{ ?BaseModel?outModel?=?new?BaseModel(); ??????????SerUser?serUser?=?GlobalThreadLocal.getSerUserInfo(); ?????if?(serUser?==?null)?{ ???????writer.write(showFileMsg("您未登錄或登錄過期,請重新登錄!")); ???????return; ?????} ??????????long?size?=?mFile.getSize(); ?????if(size?/?10?>?1024*1024)?{ ???????writer.write(showFileMsg("對不起,您上傳的文件太大,請重新上傳")); ???????return; ?????} ??????????String?fileType?=?mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf("."),?mFile.getOriginalFilename().length()).toLowerCase(); ??????????DateFormat?df?=?new?SimpleDateFormat("yyyyMMddHHmmssSSSS"); ?????String?fileName?=??file_ID?+?"_"?+?df.format(new?Date()); ??????????if(mFile.isEmpty()){ ???????writer.write(showFileMsg("上傳失敗!")); ???????return; ?????} ???????????String?path?=?merchantConfig.getFileUploadPath()?+?"/TransferBill/"; ?????File?filePath?=?new?File(path); ??????????if(!filePath.isDirectory())?????? ?????{??????? ???????filePath.mkdir();???? ?????}?? ?????File?file?=?new?File(path?+?fileName?+?fileType);??????try?{ ??????mFile.getFileItem().write(file);??????}?catch?(Exception?e)?{ ??????logger.debug("exception:",e); ??????writer.write(showFileMsg("上傳失敗!發(fā)生異常:"+e.getMessage())); ??????return; ?????} ??????????String?fileUrl?=?merchantConfig.getFileUploadPath().substring(11,merchantConfig.getFileUploadPath().length())?+?"/TransferBill/"?+?fileName?+?fileType; ?????riskManager.uploadTransferFileUrl(file_ID,?fileUrl); ??????????writer.write(showFileMsg("文件上傳成功!")); ?????outModel.setRetCode(BaseModel.RET_OK); ?????return;? 這里是寫一個(gè)Writer來調(diào)用JSP頁面的一個(gè)JS函數(shù):
?
?????public?static?String?showFileMsg(String?msg)?{? ???String?result?=?"<html><head><meta?http-equiv=\"Content-Type\"?content=\"text/html;?charset=UTF-8\"?/></head><body>"?+ ???????"<script?language=JavaScript>window.parent.fileUploadSuccessCallBack('"+?msg?+"');</script>"?+ ???????????"<body></html>"; ???return?result; ?}? ?
3.JSP頁面(這里是重點(diǎn))
主要是在這個(gè)JSP頁面加一個(gè)隱藏的Iframe:
?
<iframe?name="hideIframe"?style="display:none"></iframe>? 然后在文件上傳的表單里使用這個(gè)隱藏的Iframe來做表單返回的頁面:
?
<form?id="fileForm"?action="${ctx}/risksys/uploadWaybillFile.do"?enctype="multipart/form-data"?method="post"?target="hideIframe">?????????????????<input?type="hidden"?id="file_waybillId"?name="file_waybillId"?value="'+waybillId+'"?/>?????????????????<table?width="350"?border="0"?cellpadding="0"?cellspacing="0">?????????????????<tr><td?width="300"?class="dingdan_shahu"></td>?????????????????<td?class="order_xx"></td></tr>?????????????????<tr><td?class="dingdan_shahu">上傳文件:</td>?????????????????<td?class="order_xx"><span?id="showFile"><input?type="file"?name="fileUpload"?id="fileUpload"?/></span></td></tr>?????????????????<tr><td?class="dingdan_shahu"></td>'+ ?????????????????<td?class="order_xx"><span?style="color:red;">*注:文件大小請控制在10M以內(nèi)</span>?????????????????</td></tr>?????????????????</table></form>? 4.JS代碼
這里是被調(diào)用的函數(shù):
?
?????function?fileUploadSuccessCallBack(msg)?{ ?????????removeLoading(true); ?????????getInfo(msg); ?????????findPage(currentPageNo); ?????}? ?
?
?
?
?
轉(zhuǎn)載于:https://blog.51cto.com/peterz2011/929183
總結(jié)
以上是生活随笔為你收集整理的Sring3MVC页面无刷新上传文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。