Chinaunix首页 | 论坛 | 博客
  • 博客访问: 813767
  • 博文数量: 247
  • 博客积分: 166
  • 博客等级: 入伍新兵
  • 技术积分: 2199
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-15 16:10
文章分类

全部博文(247)

文章存档

2017年(1)

2015年(63)

2014年(80)

2013年(94)

2012年(9)

分类: Java

2015-07-15 11:45:52

第一步:配置sping 

点击(此处)折叠或打开

  1. <bean id="multipartResolver"
  2.           class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  3.         <!--1024*200即200k-->
  4.         <property name="maxUploadSize" value="204800"/>
  5.         <!--resolveLazily属性启用是为了推迟文件解析,以便在UploadAction 中捕获文件大小异常-->
  6.         <property name="resolveLazily" value="true"/>
  7.     </bean>
第二步:在上传action中自己捕获异常 

点击(此处)折叠或打开

  1. @RequestMapping
  2.     public void execute(
  3.             @RequestParam(required = false) MultipartFile file,
  4.             @RequestParam(value = "file_info_id", required = false) Integer fileInfoId,
  5.             ModelMap model, HttpServletRequest request) throws Exception {
  6.             
  7.         if (file == null || file.isEmpty()) {
  8.             return;
  9.         }
  10.         byte[] bytes = file.getBytes();
  11.             ……………………
  12. ………………
  13. }
  14.   @ExceptionHandler(Exception.class)
  15.     public ModelAndView handleException(Exception ex,HttpServletRequest request) {
  16.          Map<Object, Object> model = new HashMap<Object, Object>();
  17.          if (ex instanceof MaxUploadSizeExceededException){
  18.              model.put("errors", "文件应不大于 "+
  19.              getFileKB(((MaxUploadSizeExceededException)ex).getMaxUploadSize()));
  20.              } else{
  21.              model.put("errors", "不知错误: " + ex.getMessage());
  22.              }
  23.          return new ModelAndView("/common/file/upload", (Map) model);
  24.                 
  25.     }
  26.     
  27.     private String getFileKB(long byteFile){
  28.         if(byteFile==0)
  29.          return "0KB";
  30.         long kb=1024;
  31.         return ""+byteFile/kb+"KB";
  32.     }
  33.     private String getFileMB(long byteFile){
  34.         if(byteFile==0)
  35.           return "0MB";
  36.          long mb=1024*1024;
  37.          return ""+byteFile/mb+"MB";
  38.     }
第三步:界面 

点击(此处)折叠或打开

  1. <script type="text/javascript">
  2. $(function() {
  3.     $('#frmupload1').submit(function() {
  4.         if ($('#file1').val() == '') {
  5.             alert('请选择上传导入文件!');
  6.             $('#file1').focus();
  7.             return false;
  8.         }else{
  9.             if(!isvalidatefile($('#file1').val()))
  10.                  return false;
  11.                 
  12.         }
  13.     });
  14.     $('#frmupload2').submit(function() {
  15.         if ($('#file2').val() == '') {
  16.             alert('请选择上传导入文件!');
  17.             $('#file2').focus();
  18.             return false;
  19.         }else{
  20.             if(!isvalidatefile($('#file2').val()))
  21.                  return false;
  22.                 
  23.         }
  24.     });
  25. });

  26. function isvalidatefile(obj) {
  27.     
  28.     var extend = obj.substring(obj.lastIndexOf(".") + 1);
  29.     //alert(extend);
  30.     if (extend == "") {
  31.     } else {
  32.         if (!(extend == "xls" )) {
  33.             alert("请上传后缀名为xls(Excel2003)或xlsx(Excel2007)的文件!");
  34.             
  35.             return false;
  36.         }
  37.     }
  38.     return true;
  39. }
  40. <body>
  41. <h1>上传文件</h1>
  42. <form action="" method="post" enctype="multipart/form-data" onsubmit="return checkSubmit();">

  43.     <p>请选择文件:</p>
  44.     
  45.     <p ${not empty errors ?"style='color : red;'":""}>${errors}</p>
  46.     <input type="file" name="file" id="file"/>&nbsp;<input type="submit" value="确定"/>
  47. </form>
  48. </body>
转载自:http://913.iteye.com/blog/1521186


阅读(1794) | 评论(0) | 转发(0) |
0

上一篇:div 显示与隐藏

下一篇:spring mvc整合json

给主人留下些什么吧!~~