bootstrap fileinput + springMVC 简单示例

在开发的时候需要实现一个class文件的上传存储功能,所以用到了bootstrap fileinput 插件,后台是用的springMVC。

提前准备

在开始贴代码前把一些必要的jar,css,js等文件准备好。

bootstrap fileinput

需要加载jQuery、bootstrap、fileinput

<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <!-- 加载jQuery -->
<script src="http://cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <!-- 加载bootstrap的js -->
<script src="<%=webContext %>/resources/bootstrap/js/fileinput.js"></script> <!-- 加载fileinput的js -->

<link rel="stylesheet"
	href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" /> <!-- 加载bootstrap的css样式 -->
<link rel="stylesheet"
	href="<%=webContext %>/resources/bootstrap/css/fileinput.css" /> <!-- 加载fileinput的css样式 -->


springMVC

需要配置文件解析器,下面是配置在spring-bean.xml中。

<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="maxUploadSize" value="-1" />  
        <property name="defaultEncoding" value="UTF-8" />  
        <property name="maxInMemorySize" value="40960"></property>  
        <property name="uploadTempDir" value="fileUpload/temp"></property>
    </bean>

jar包

在配置springMVC的文件解析器时可能会抛出ClassNotFound的异常。原因很可能是缺少Apache的commons-io和commons-fileupload两个jar包,下面是maven添加。

<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.2.1</version>
		</dependency>


代码片段

jsp页面代码

jsp页面部分

<button class="btn btn-primary" data-toggle="modal" data-target="#myModal">上传</button>
			<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					×
				</button>
				<h2 class="modal-title" id="myModalLabel">
					上传class文件
				</h2>
			</div>
			<div class="modal-body">
			<form enctype="multipart/form-data" id="uploadForm">
				<input id="fileUpload" name="classFile" type="file" multiple class="file-loading">
				</form>
			</div>
			<div class="modal-footer">
			<h6>class文件的包名必须是customfuction。</h6>
			<h6>class名作为表达式名,包含一个公开方法。</h6>
			<h6>方法名为class名+Template,无继承,无实现接口。</h6>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>

js代码

$("#myModal").on("hidden.bs.modal",function(){
		location.reload();
	}); //关闭模态框时刷新页面

$("#fileUpload").fileinput({
	    uploadUrl: "<%=webContext %>/craw/customFunctionUpload.do", // server upload action
	    allowedFileExtensions: ['class'],
	    uploadAsync: true,
	    maxFileCount: 5,
	    uploadExtraData: {state:"0"}
	});


后台控制代码

@RequestMapping("craw/customFunctionUpload.do")
	public void fileUpload(@RequestParam(value = "classFile") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) throws IOException, JBOException {
		if (file != null) {
			//注意:返回格式必须是json(可查看官方文档)
			//=========================具体功能可忽略===========================
			String globalDir = request.getSession().getServletContext().getRealPath("/");
			String fileName = file.getOriginalFilename();
			String state = request.getParameter("state");
			
			BizObjectManager bm = JBOFactory.getBizObjectManager("jbo.service.custom_function");
			String version = "1";
			int count = bm.createQuery("select count(*) from O where funcName=:funcName")
					.setParameter("funcName", fileName.substring(0, fileName.length() - 6))
					.getSingleResult(false).getAttribute("count(*)").getInt();
			if("0".equals(state)){
				if(count >= 1){
					response.getWriter().print("{\"error\":\"已存在同名class!\"}");
					return;
				}
			}
			else{
				if(!fileName.substring(0, fileName.length() - 6).equals(state)){
					response.getWriter().print("{\"error\":\"上传的class文件名与要修改的表达式名不匹配!\"}");
					return;
				}
				if(count >= 1){
					version =  "" + (Integer.valueOf(bm.createQuery("select version form O where funcName=:funcName")
							.setParameter("funcName", fileName.substring(0, fileName.length() - 6))
							.getSingleResult(false).getAttribute("version").getString()) + 1);					
				}
			}
			
			System.out.println(globalDir+"cfClasses/customfunction");
			
			File write = new File(globalDir+"cfClasses/customfunction", fileName);
			if (write.getParentFile().exists() == false) {
				System.out.println("dir not exist");
				write.getParentFile().mkdirs();
			}
			write.createNewFile();

			InputStream ins = file.getInputStream();
			OutputStream ous = new FileOutputStream(write);
			try {
				byte[] buffer = new byte[1024];
				ARE.getLog().debug("开始写文件:" + fileName);
				int len = 0;
				while ((len = ins.read(buffer)) > -1)
					ous.write(buffer, 0, len);
				ARE.getLog().debug("已保存的文件:" + fileName);
			} catch (Exception e) {
				response.getWriter().print("{\"error\":\"文件保存失败\"}");
			} finally {
				ous.close();
				ins.close();
			}

			System.out.println("fileupload success!  name=" + fileName);

			MyFileClassLoader fileClsLoader = new MyFileClassLoader();
			fileClsLoader.setClassPath(globalDir + "cfClasses");
			boolean flag = false;
			int paramCount = 0;
			String funcTemplateName = fileName.substring(0,fileName.length()-6)+"Template";
			String funcTemplate = funcTemplateName + "(";
			try {
				Class cls = fileClsLoader.loadClass("customfunction."+fileName.substring(0,fileName.length()-6));
				Method[] mthds = cls.getMethods();
				 for(Method mthd : mthds){
				 String methodName = mthd.getName();
				 System.out.println("mthd.name="+methodName);
				 if(methodName.equals(fileName.substring(0,fileName.length()-6)+"Template")){
					 flag = true;
					 paramCount = mthd.getGenericParameterTypes().length;
					 for (int i = 1; i <= paramCount; i++) {
						 funcTemplate += "${"+ i + "},";
					}
					 if(paramCount > 0){
						 funcTemplate = funcTemplate.substring(0,funcTemplate.length() - 1);
					 }
					 funcTemplate += ")";
				 	}
				 }
			} catch (ClassNotFoundException | IllegalArgumentException e) {
				ARE.getLog().error("解析class文件模板函数失败!", e);
				write.delete();
				response.getWriter().print("{\"error\":\"文件保存失败\"}");
			}
			
			if(!flag){
				write.delete();
				response.getWriter().print("{\"error\":\"class文件中不存在方法名为"+funcTemplateName+"的方法\"}");
				return;
			}
			
			BizObject obj = bm.newObject();
			obj.setAttributeValue("funcName", fileName.substring(0, fileName.length() - 6));
			obj.setAttributeValue("funcTemplate", funcTemplate);
			obj.setAttributeValue("version", version);
			obj.setAttributeValue("inputTime", StringFunction.getTodayNow());
			obj.setAttributeValue("updateTime", StringFunction.getTodayNow());
			bm.saveObject(obj);
			
			//=========================具体功能可忽略===========================			

			response.getWriter().print("{}");  //成功时返回一个空json表示成功
		} else {
			System.out.println("file empty");
			response.getWriter().print("{\"error\":\"上传失败\"}");  //出错时返回一个包含error属性的json,前端控件会显示错误状态并输出对应信息

		}
	}
}


图片展示

按钮
按钮1

弹出模态框
111

成功
111

报错
111
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值