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>

时间: 2024-11-03 03:27:57

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

2. SpringMVC 上传文件操作

1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 1 com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 2 com.springsource.net.sf.cglib-2.2.0.jar 3 com.springsource.org.aopalliance-1.0.0.jar 4 com.springsource.org.apache.commo

SpringMVC上传文件的三种解析方式

springMVC上传文件后,在action解析file文件的三种方式. jsp页面的写法: <form action="parserUploadFile1" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit"

springmvc 上传文件时的错误

使用springmvc上传文件一直失败,文件参数一直为null, 原来是配置文件没写成功. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 最大上传尺寸 B单位 1M= 1024*1024 --> <property name="maxUploadSize&

springmvc上传文件,抄别人的

SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml [html] view plaincopyprint? <?xml version="1.0" encoding="UTF-8"?>

SpringMVC上传文件总结

如果是maven项目 需要在pom.xml文件里面引入下面两个jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com

springmvc 上传文件的问题

今天用springmvc 上传文件的时候 报错 org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest 网上查找原因 说的有如下几种: 1.表单form 上没有 enctype="multipart/form-data"   这个属性 2.配置文件: <bean id="multi

SpringMVC上传文件(图片)并保存到本地

SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize">

SpringMVC上传文件的三种方式(转载)

直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartresolver.java.html 前台: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <%@ page language="java" contentTy

springmvc上传文件方法及注意事项

本文基于注解的配置,敬请留意  基于注解整合 一.springmvc为我们提供两种上传方式配置: 1)org.springframework.web.multipart.commons.CommonsMultipartResolver (基于commons-fileupload.jar) 2) org.springframework.web.multipart.support.StandardServletMultipartResolver (基于servlet3.0+) 二.配置方式基于com