• 一篇关于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

  • 相关阅读:
    cqyz oj | 单峰排列
    cqyz oj/uva 548 | 二叉树
    cqyz oj | 树网的核 | 树的直径
    cqyz oj | 树上的询问 | 最近公共祖先
    cqyz oj | 循环逆序对 | 逆序对 | 树状数组
    cqyz oj | 潜水比赛 | 贪心
    YOLO v3 & Pascal VOC数据集
    太阳爆发分类
    PPT制作
    anaconda
  • 原文地址:https://www.cnblogs.com/royi123/p/5920332.html
Copyright © 2020-2023  润新知