一篇关于SpringMVC 传统文件上传的方法

一、界面效果

二、html代码

 1 <legend>上传APK文件</legend>
 2   <form action="<%=basePath%>/apks/commitApk" class="form-horizontal" method="post" enctype="multipart/form-data">
 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( 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 = xxx;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()));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(xx);//文件夹放在webApp下面
49             apkManagerPo.setApkPath(xx);
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插件

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

 

posted on 2016-09-29 16:23  洛易  阅读(1448)  评论(0编辑  收藏  举报