zoukankan      html  css  js  c++  java
  • 一篇关于SpringMVC 传统文件上传的方法

    一、界面效果

    二、html代码

     1 <legend>上传APK文件</legend>
     2   <form action="<%=basePath%>/apks/commitApk" class="form-horizontal" method="post" enctype="multipart/form-data">
     3     <input name="cityCode" value="${s0.paramValue}" type="hidden"/>
     4     <input name="productCode" value="${s.paramValue}" type="hidden"/>
     5     <div class="form-group">
     6          <label for="ticket-message" class="col-sm-3 control-label col-md-1">版本号</label>
     7          <div class="col-md-4">
     8             <input type="text" class="form-control" name="versionCode" id="versionCode" placeholder="请输入整数">
     9          </div>
    10     </div>
    11     <div class="form-group">
    12          <label for="ticket-message" class="col-sm-3 control-label col-md-1">版本名</label>
    13          <div class="col-md-4">
    14               <input type="text" class="form-control" name="versionName" id="versionName" placeholder="0.0.0.1">
    15          </div>
    16     </div>
    17     <div class="form-group">
    18        <label for="ticket-message" class="col-sm-3 control-label col-md-1">描述</label>
    19        <div class="col-md-4">
    20            <textarea class="form-control" name="versionDesc" id="versionDesc" rows="5" cols="30" placeholder="版本描述"></textarea>
    21        </div>
    22     </div>    
    23     <div class="form-group">
    24        <label for="ticket-message" class="col-sm-3 control-label col-md-1">文件</label>
    25        <!-- <label for="ticket-attachment" class="col-sm-3 control-label">请选中apk文件</label> -->
    26        <div class="col-md-4">
    27             <input type="file" name="apkFile" id="apkFile">
    28             <p class="help-block"><em>文件类型: .apk</em></p>
    29        </div>
    30    </div>
    31    <div class="form-group">
    32         <label for="ticket-message" class="col-sm-3 control-label col-md-1"></label>
    33         <div class="col-md-4">
    34              <input id="apkSubmitBtn" type="submit" class="btn btn-danger"></input>
    35        </div>
    36    </div>
    37 </form>

    三、后台java代码

     1 @RequestMapping(value = "/commitApk", method = RequestMethod.POST)
     2     public @ResponseBody ModelAndView commitApk(@RequestParam("cityCode") String cityCode,@RequestParam("productCode") String productCode,
     3             @RequestParam("apkFile") CommonsMultipartFile[] apkFile,@RequestParam("versionCode") int versionCode,@RequestParam("versionName") String versionName, 
     4             @RequestParam("versionDesc") String versionDesc, HttpSession session) {
     5         FileOutputStream out = null;
     6         FileInputStream in = null;
     7         try {
     8             String appPath = System.getProperty("root");
     9             appPath = appPath.substring(0, appPath.indexOf("ExceptionManageSystem"));
    10             11             StringBuffer buffer = new StringBuffer(appPath + com.tongyan.ems.common.Constants.APKMANAGER_FILES_PAHT);
    12             13             buffer.append(productCode).append("\").append(cityCode).append("\");
    14             File fileDir = new File(buffer.toString());
    15             if(!fileDir.exists()) {
    16                 fileDir.mkdirs();
    17             }
    18             buffer.append(apkFile[0].getFileItem().getName());
    19             File file = new File(buffer.toString());
    20             if(!file.exists()) {
    21                 file.createNewFile();
    22             } else {
    23                 file.delete();//如果存在就删除重新上传
    24             }
    25             out = new FileOutputStream(file);
    26             
    27             in = (FileInputStream)apkFile[0].getInputStream();
    28             int read = 0;
    29             byte[] b = new byte[1024];
    30             while((read = in.read(b)) != -1) {
    31                 out.write(b, 0, read);
    32             }
    33             //数据入库
    34             ApkManagerPo apkManagerPo = new ApkManagerPo();
    35             apkManagerPo.setApkCode(UUID.randomUUID().toString());
    36             apkManagerPo.setCreateDate(new SimpleDateFormat(Constants.DATE_FORMAT).format(new Date()));
    37             apkManagerPo.setProductCode(productCode);
    38             apkManagerPo.setCustomerCode(cityCode);
    39             apkManagerPo.setVersionCode(versionCode);
    40             apkManagerPo.setVersionName(versionName);
    41             apkManagerPo.setVersionDesc(versionDesc);
    42             if(session.getAttribute("User") != null) {
    43                 UserPo user = (UserPo)session.getAttribute("User");
    44                 apkManagerPo.setUserId(user.getUserId());
    45             }else {
    46                 apkManagerPo.setUserId("");
    47             }
    48             apkManagerPo.setApkRoute(com.tongyan.ems.common.Constants.FEEDBACK_FILES_PAHT + productCode + "\" + cityCode + "\" + apkFile[0].getFileItem().getName());//文件夹放在webApp下面
    49             apkManagerPo.setApkPath(com.tongyan.ems.common.Constants.FEEDBACK_FILES_URL + productCode + "/" + cityCode + "/" + apkFile[0].getFileItem().getName());
    50             apkService.addApkVersion(apkManagerPo);
    51         } catch (Exception e) {
    52             e.printStackTrace();
    53         } finally {
    54              try {
    55                  if(out != null) {
    56                      out.close();  
    57                  }
    58                  if(in != null) {
    59                      in.close();
    60                  }
    61             } catch (IOException e) {
    62                 e.printStackTrace();
    63             }     
    64         }
    65         return null;
    66     }

    此代码为form表单提交,现改需要改为js提交,故做记录

    一个用于ajax上传的js插件

    http://files.cnblogs.com/files/royi123/ajaxfileupload_JS_File.rar

  • 相关阅读:
    matlab中关于使用length导致的不稳定状况。
    matlab 批量读入文件夹中的指定文件类型 (目录级数不限)
    matlab中的图像裁剪,图像抽取,反转,镜像。
    反锐化掩模 unsharp masking【转载】
    matlab 将图像切分为N*N像素的小块
    Python2.7.3 Tkinter Entry(文本框) 说明
    基于JQuery的列表拖动排序
    MAC如何删除开机自启动程序
    MAC配置SVN服务器
    关于MAC清倒废纸篓,项目正在使用
  • 原文地址:https://www.cnblogs.com/royi123/p/5920332.html
Copyright © 2011-2022 走看看