一篇关于SpringMVC 传统文件上传的方法
一、界面效果

二、html代码
<legend>上传APK文件</legend>
<form action="<%=basePath%>/apks/commitApk" class="form-horizontal" method="post" enctype="multipart/form-data">
<input name="cityCode" value="${s0.paramValue}" type="hidden"/>
<input name="productCode" value="${s.paramValue}" type="hidden"/>
<div class="form-group">
<label for="ticket-message" class="col-sm-3 control-label col-md-1">版本号</label>
<div class="col-md-4">
<input type="text" class="form-control" name="versionCode" id="versionCode" placeholder="请输入整数">
</div>
</div>
<div class="form-group">
<label for="ticket-message" class="col-sm-3 control-label col-md-1">版本名</label>
<div class="col-md-4">
<input type="text" class="form-control" name="versionName" id="versionName" placeholder="0.0.0.1">
</div>
</div>
<div class="form-group">
<label for="ticket-message" class="col-sm-3 control-label col-md-1">描述</label>
<div class="col-md-4">
<textarea class="form-control" name="versionDesc" id="versionDesc" rows="5" cols="30" placeholder="版本描述"></textarea>
</div>
</div>
<div class="form-group">
<label for="ticket-message" class="col-sm-3 control-label col-md-1">文件</label>
<!-- <label for="ticket-attachment" class="col-sm-3 control-label">请选中apk文件</label> -->
<div class="col-md-4">
<input type="file" name="apkFile" id="apkFile">
<p class="help-block"><em>文件类型: .apk</em></p>
</div>
</div>
<div class="form-group">
<label for="ticket-message" class="col-sm-3 control-label col-md-1"></label>
<div class="col-md-4">
<input id="apkSubmitBtn" type="submit" class="btn btn-danger"></input>
</div>
</div>
</form>
三、后台java代码
@RequestMapping(value = "/commitApk", method = RequestMethod.POST)
public @ResponseBody ModelAndView commitApk(@RequestParam("cityCode") String cityCode,@RequestParam("productCode") String productCode,
@RequestParam("apkFile") CommonsMultipartFile[] apkFile,@RequestParam("versionCode") int versionCode,@RequestParam("versionName") String versionName,
@RequestParam("versionDesc") String versionDesc, HttpSession session) {
FileOutputStream out = null;
FileInputStream in = null;
try {
String appPath = System.getProperty("root");
appPath = appPath.substring(0, appPath.indexOf("ExceptionManageSystem"));
StringBuffer buffer = new StringBuffer(appPath + com.tongyan.ems.common.Constants.APKMANAGER_FILES_PAHT);
buffer.append(productCode).append("\\").append(cityCode).append("\\");
File fileDir = new File(buffer.toString());
if(!fileDir.exists()) {
fileDir.mkdirs();
}
buffer.append(apkFile[0].getFileItem().getName());
File file = new File(buffer.toString());
if(!file.exists()) {
file.createNewFile();
} else {
file.delete();//如果存在就删除重新上传
}
out = new FileOutputStream(file); in = (FileInputStream)apkFile[0].getInputStream();
int read = 0;
byte[] b = new byte[1024];
while((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
//数据入库
ApkManagerPo apkManagerPo = new ApkManagerPo();
apkManagerPo.setApkCode(UUID.randomUUID().toString());
apkManagerPo.setCreateDate(new SimpleDateFormat(Constants.DATE_FORMAT).format(new Date()));
apkManagerPo.setProductCode(productCode);
apkManagerPo.setCustomerCode(cityCode);
apkManagerPo.setVersionCode(versionCode);
apkManagerPo.setVersionName(versionName);
apkManagerPo.setVersionDesc(versionDesc);
if(session.getAttribute("User") != null) {
UserPo user = (UserPo)session.getAttribute("User");
apkManagerPo.setUserId(user.getUserId());
}else {
apkManagerPo.setUserId("");
}
apkManagerPo.setApkRoute(com.tongyan.ems.common.Constants.FEEDBACK_FILES_PAHT + productCode + "\\" + cityCode + "\\" + apkFile[0].getFileItem().getName());//文件夹放在webApp下面
apkManagerPo.setApkPath(com.tongyan.ems.common.Constants.FEEDBACK_FILES_URL + productCode + "/" + cityCode + "/" + apkFile[0].getFileItem().getName());
apkService.addApkVersion(apkManagerPo);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
此代码为form表单提交,现改需要改为js提交,故做记录
一个用于ajax上传的js插件
http://files.cnblogs.com/files/royi123/ajaxfileupload_JS_File.rar
一篇关于SpringMVC 传统文件上传的方法的更多相关文章
- TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式
		
1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...
 - 关于SpringMVC的文件上传
		
关于文件的上传,之前写过2篇文章,基于Struts2框架,下面给出文章链接: <关于Struts2的文件上传>:http://www.cnblogs.com/lichenwei/p/392 ...
 - 6.学习springmvc的文件上传
		
一.文件上传前提与原理分析 1.文件上传必要前提: 2.文件上传原理分析: 3.需要引入的jar包: 二.传统方式文件上传程序 1.pom.xml <dependency> <gro ...
 - 使用springmvc实现文件上传
		
该配置在javaweb上传文件篇中的基础上进行配置:https://www.cnblogs.com/flypig666/p/11745182.html 1.配置文件解析器,在springmvc.xml ...
 - 【SpringMVC】SpringMVC 实现文件上传
		
SpringMVC 实现文件上传 文章源码 文件上传回顾 查看 JavaWeb 阶段的文件上传下载 实现步骤: 客户端: 发送 post 请求,告诉服务器要上传什么文件 服务器: 要有一个 form ...
 - springmvc图片文件上传接口
		
springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...
 - SpringMVC学习--文件上传
		
简介 文件上传是web开发中常见的需求之一,springMVC将文件上传进行了集成,可以方便快捷的进行开发. springmvc中对多部件类型解析 在 页面form中提交enctype="m ...
 - Spring +SpringMVC  实现文件上传功能。。。
		
要实现Spring +SpringMVC 实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...
 - SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
		
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
 
随机推荐
- 三星在GPL下发布其exFAT文件系统实现源码
			
exFAT文件系统是微软的一个产品,设计让外置储存设备和PC之间实现无缝的TB级数据转移和数据交换,它只支持Windows和OS X,不支持Linux.作为一个含有大量专利的私有产品,没有人会预计它会 ...
 - iOS 通讯录操作
			
转载至:http://superuna.blog.51cto.com/4192682/982938 //新增联系人 -(void)AddPeople { //取得本地通信录名柄 ...
 - iwebshop 订单存库修改为下单件库存
			
//减少库存量 $orderGoodsDB = new IModel('order_goods'); $orderGoodsList = $orderGoodsDB->query('order_ ...
 - 复制过来的东西也不靠谱,微信公众号第三方平台的API
			
API:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&veri ...
 - 《Java中的包机制》
			
/* 包的机制:(1) */ package lee; public class PackageTest { public void Test(int num) { System.out.printl ...
 - CSS中相对定位与绝对定位
			
看了几个讲解定位的博客,觉得还不错,分享之: 博客一:http://blog.sina.com.cn/s/blog_4bcf4a5e010008o0.html 文章中,主要需要参考的有两点: 1,相对 ...
 - myawr :  mysql性能监控
			
myawr以mysql instance 为单位,每隔一段时间进行采样,然后把数据保存到数据库,以便分析.目前myawr脚本收集的信息包括5个部分: 1 系统方面的:负载.cpu.io.网络.swap ...
 - EC6 map 和 set
			
1.map 首先map是一个具有键值对的结构 给定一个名字,要查找对应的成绩,就先要在names中找到对应的位置,再从scores取出对应的成绩,Array越长,耗时越长. 如果用Map实现,只需要一 ...
 - BackTrack5-r3安装前需要的准备及说明
			
一. 配置创建一个虚拟机,本教程用的是VMware-workstation-full-10.0.0,BT5-r3-GNOME-64镜像. 这里是BT5-r3-GNOME-64位种子:http://pa ...
 - Smarty 分页
			
1 <div id="pagelist" class="clearfix">2 <a href="/canadian-sai ...