spring实现文件上传
来源:互联网 发布:网络教育是国民教育吗 编辑:程序博客网 时间:2024/06/13 09:36
1.jsp页面直接引入
<script type="text/javascript" src="js/jquery/jquery-1.9.1.js"></script>
<script type="text/javascript" src="js/jquery/ajaxfileupload.js"></script>
<script type="text/javascript" src="js/jquery/jquery.json-2.4.js"></script>
<script type="text/javascript">
//定时器对象
var uploadProcessTimer = null;
function ajaxFileUpload()
{
$.ajaxFileUpload
({
url:'/upload/mvc/upload.do',
secureuri:false,
fileElementId:'fileToUpload',
dataType: 'json',
data:{name: $('#name').val()},
success: function (data, status)
{
//清除定时器
if(uploadProcessTimer) {
window.clearInterval(uploadProcessTimer);
}
$('#loading').hide();
var message = data['message'];
var code = data['code'];
if(code != 200) {
$('#fileUploadProcess').html('0%');
}
if(message)
{
alert(data.message);
}
},
error: function (data, status, e)
{
//清除定时器
if(uploadProcessTimer) {
window.clearInterval(uploadProcessTimer);
}
$('#loading').hide();
//这里处理的是网络异常,返回参数解析异常,DOM操作异常
alert("上传发生异常");
}
})
return false;
}
</script>
附件:<input id="fileToUpload", name="file" type="file" class="input"> <br/>
<br><br>
<input type="button" onclick="return ajaxFileUpload();" value="上传"><br/>
java:
@RequestMapping(value = "upload", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> fileUpload(
@RequestParam("file") CommonsMultipartFile file,
HttpSession session, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// 清除上次上传进度信息
String saveDirectoryPath = Constants.UPLOAD_PLATFORM + "/" + uploadFolderName;
File saveDirectory = new File(saveDirectoryPath);
if (!saveDirectory.exists()) {
saveDirectory.mkdir();
}
// 判断文件是否存在
if (!file.isEmpty()) {
String fileName = file.getOriginalFilename();
file.transferTo(new File(saveDirectory, fileName));
}
return State.OK.toMap();
}
public enum State {
OK(200, "上传成功"),
ERROR(500, "上传失败"),
OVER_FILE_LIMIT(501, "超过上传大小限制"),
NO_SUPPORT_EXTENSION(502, "不支持的扩展名");
private int code;
private String message;
private State(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
public Map<String, Object> toMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("code", this.code);
map.put("message", this.message);
return map;
}
}
- spring实现文件上传
- Spring实现文件上传
- Spring实现文件上传
- spring实现文件上传
- spring实现上传文件
- spring实现文件上传
- 用spring实现文件上传
- Spring MVC 实现文件上传
- spring MVC实现文件上传
- Spring MVC实现文件上传
- Spring中实现文件上传
- Spring中实现文件上传
- spring 文件上传功能实现
- Spring mvc实现文件上传
- spring springmvc实现文件上传
- Spring mvc 实现文件上传
- Spring+Springmvc实现文件上传
- Spring Boot实现文件上传
- GRE阅读主题句理解方法
- unity——Rigidbody类
- php截取字符串,解决乱码问题与html标签问题
- No plugin found for prefix 'war' in the current project and in the plugin groups
- 渲云客户端怎么安装?
- spring实现文件上传
- 一步一步实现Periscope点赞效果
- Git版本控制工具与GitHub的使用
- gdb 调试相关
- Laravel5 安装和配置
- SQL Server中DateTime与DateTime2的区别
- 统计内网机器网络流量的方案
- JAVA代码规范
- 客户端的安装与功能介绍?