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

2023-02-16,,,,

一、界面效果

二、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 传统文件上传方法的相关教程结束。

《一篇关于SpringMVC 传统文件上传的方法.doc》

下载本文的Word格式文档,以方便收藏与打印。