Huploadify V2.1+ SpringMVC上传文件的实现

Posted Brook Legend

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Huploadify V2.1+ SpringMVC上传文件的实现相关的知识,希望对你有一定的参考价值。

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>学生成绩信息</title>
<link rel="stylesheet" type="text/css"
href="<%=basePath%>jquery/Huploadify-V2.1.2/Huploadify.css">
<script type="text/javascript"
src="<%=basePath%>jquery/Huploadify-V2.1.2/jquery.js"></script>
<script type="text/javascript"
src="<%=basePath%>jquery/Huploadify-V2.1.2/jquery.Huploadify.js"></script>
</head>
<script type="text/javascript">
$(function(){
var up = $(\'#upload\').Huploadify({
auto:false,
fileTypeExts:\'*.jpg;*.png;*.exe;*.mp3;*.mp4;*.zip;*.doc;*.docx;*.ppt;*.pptx;*.xls;*.xlsx;*.pdf\',
multi:true,
fileSizeLimit:99999999,
breakPoints:true,
saveInfoLocal:true,
showUploadedPercent:true,//是否实时显示上传的百分比,如20%
showUploadedSize:true,
removeTimeout:9999999,
uploader:\'<%=basePath%>\'+ \'fileUpload\',
onUploadStart:function(){
//up.settings(\'formData\', {aaaaa:\'1111111\',bb:\'2222\'});
up.Huploadify(\'settings\',\'formData\', {aaaaa:\'1111111\',bb:\'2222\'});
},
onUploadSuccess:function(file){
//alert(\'上传成功\');
},
onUploadComplete:function(){
//alert(\'上传完成\');
},
/*getUploadedSize:function(file){
var data = {
data : {
fileName : file.name,
lastModifiedDate : file.lastModifiedDate.getTime()
}
};
var url = \'http://localhost/uploadfile/\';
var uploadedSize = 0;
$.ajax({
url : url,
data : data,
async : false,
type : \'POST\',
success : function(returnData){
returnData = JSON.parse(returnData);
uploadedSize = returnData.uploadedSize;
}
});
return uploadedSize;
} */
});

$(\'#btn1\').click(function(){
up.stop();
});
$(\'#btn2\').click(function(){
up.upload(\'*\');
});
$(\'#btn3\').click(function(){
up.cancel(\'*\');
});
$(\'#btn4\').click(function(){
up.disable();
});
$(\'#btn5\').click(function(){
up.ennable();
});


});
</script>
<body>
<div id="upload"></div>
<button id="btn1">stop</button>
<button id="btn2">upload</button>
<button id="btn3">cancel</button>
<button id="btn4">disable</button>
<button id="btn5">ennable</button>
</body>
</html>

 

/**
* 查询学生信息列表
*
* @param name
* @return
* @throws NamingException
* @throws SQLException
*/
@ResponseBody
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public String fileUpload(@RequestParam Map<String, Object> map, HttpServletRequest request,
HttpServletResponse response, @RequestParam(value = "file", required = false) MultipartFile file) {
try {
String fileName = request.getParameter("fileName");
byte[] bytes = file.getBytes();
System.out.println(file.getOriginalFilename());
String uploadDir = request.getRealPath("/") + "upload";
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
String sep = System.getProperty("file.separator");
File uploadedFile = new File(uploadDir + sep + fileName);
FileCopyUtils.copy(bytes, uploadedFile);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return "ok";
}

 

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8" />
</beans>

 

 

以上是关于Huploadify V2.1+ SpringMVC上传文件的实现的主要内容,如果未能解决你的问题,请参考以下文章

SpringMV---params and headers

springmv的文件上传(多种类型)

spring,springmv,springboot解决跨域问题

Springmv 拦截器&异常处理

Spring学习10-SpringMV核心组件2及SpringMVC项目示例

第七次课:ssh的集成(SpringMV+Spring+Hibernate)