springMVC单个文件上传
2014-07-30 22:27
330 查看
<form id="uploadForm" action="upload.html" method="post" enctype="multipart/form-data"> <table> <tr> <td>文件</td> <td><input type="file" name="file"></td> </tr> <tr> <td colspan="2" align="center"> <button type="button" onclick="doUpload()">上传</button> <button type="button" onclick="hide('upload')">返回</button> </td> </tr> </table> </form>
function doUpload(){
if(uploadFormValidateor.form()){
var fileName = $("#uploadForm").find("input[type=file]")[0].value;
$.ajax({
url:"<%=request.getContextPath()%>/ajax/exists.html",
data:{
where:"ME_FILE where lower(FILE_NAME) = lower('" + fileName + "')"
},
success:function(num){
var cover;
if(num != "0") {
if(confirm("已经存在该文件,是否覆盖原文件?")){
cover = "true";
}else{
return false;
}
}
$("#uploadForm").ajaxSubmit({
data:{"cover":cover},
success:function(data){
if(data == "success") {
$("#searchForm").submit();
}else{
alert("上传失败");
}
}
});
}
});
}
}
@RequestMapping("upload") @ResponseBody public String upload(HttpServletRequest request){ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file"); String realFileName = file.getOriginalFilename(); String destDir = request.getServletContext().getRealPath("/") + Constant.FILE_PATH; File uploadFile; try { File dir = new File(destDir); if(!dir.exists()){ dir.mkdir(); } uploadFile = new File(destDir + realFileName); file.getFileItem().write(uploadFile); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return "failure"; } Map<String,Object> map = new HashMap<String,Object>(); String cover = request.getParameter("cover"); int i = 0; if(cover != null && cover.equals("true")){ //存在则更新文件信息 map.put("fileName", realFileName); map.put("fileSize", file.getSize()); map.put("uploadDate", DateUtils.getDate()); map.put("uploadTime", DateUtils.getTime()); i = MybatisUtils.update("file.mysql.updateFileByName", map); if(i == 0){ return "failure"; } }else{ map.put("fileId", UUID.randomUUID().toString()); map.put("fileName", realFileName); map.put("fileType", realFileName.substring(realFileName.lastIndexOf(".") + 1)); map.put("fileSize", file.getSize()); map.put("uploadDate", DateUtils.getDate()); map.put("uploadTime", DateUtils.getTime()); i = MybatisUtils.insert("file.mysql.insertFile", map); if(i == 0){ // 操作数据失败,把上传的文件删除 uploadFile.delete(); return "failure"; } } System.out.println(uploadFile.getAbsolutePath()); return "success"; }
根据文件名判断是否已存在文件,提示是否要覆盖,覆盖则更新文件信息;如果文件不存在则向数据插一条上传文件信息。
相关文章推荐
- springMVC easyUI filebox 单个文件上传
- SpringMVC实现单个文件上传
- springMVC easyUI filebox 单个文件上传
- springmvc 单个文件上传
- SpringMVC:用MultipartFile上传单个文件,多个文件
- JQuery与springmvc实现单个文件上传操作
- SpringMVC 单个文件上传及调用
- asp脚本及asp上传组件的单个文件大小局限
- 使用commons-fileupload实现单个和多个文件上传
- 利用MultipartFile实现文件上传(springMVC中)
- common-fileupload-1.1.1 && struts1.3.8 单个文件上传
- 修改boa对单个上传文件大小的限制
- FileUpload之单个文件上传
- 单个文件上传类(可以自定义配置)
- springmvc文件上传总结
- 文件上传下载——通过ahxu扩展后的组件DiskFileUploadEx上传单个excel
- common-fileupload 上传单个或者多个文件 示例
- js 上传单个文件(任意大小)
- Struts1.x系列教程(8):上传单个文件
- PHP——上传文件单个案例