新建文件file.html
用jquery需要引入路径
<script src="js/jquery-2.1.0.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script src="view/js/jquery-2.1.0.js"></script> <!-- 需要写全路径 -->
<body>
<form id="form">
<input type="file" name="uplaod">
<button onclick="tijioa()">提交</button>
</form>
</body>
<script type="text/javascript">
function tijiao(){
//创建formData对象 form0表示的是获取第一个form表单
var formData = new FormData($("form")[0]);
$.ajax({
type:"post",
url:"ssh_sign0506/dict!upload",
data:formData,
processData : false,
contentType : false,
dataType:"json",
success : function(data) {
}
});
}
</script>
</html>
在DictController类中添加文件上传的方法
上服务器中取一个请求路径:先获取服务器下的路径——》在工程下创建一个upload文件夹
通过接收到的文件——》在服务器下创建文件文件名:uploadFileName
复制,把传过来的文件复制到dest上:
在后台写三行就能完成文件的上传
给这两个属性加get、set方法
//在WebContent下新建一个upload的文件夹,获取其在服务器的绝对磁盘路径
String path = ServletActionContext.getServletContext().getRealPath("/upload");
//创建一个服务器端的文件
File dest = new File(path,uploadFileName);
//完成文件上传的操作
FileUtils.copyFile(upload, dest); //将upload文件复制到路径下 有异常先抛出
需要有两个属性:uploadFileName、upload
private File upload;//定义一个File ,变量名要与jsp中的input标签的name一致
private String uploadFileName;//上传文件的名称
这里的private File upload;要和file.html中<input type="file" name="upload"> 里的name一致
现在一个基本的文件上传功能就做完了。
在pojo包下创建一个ExcelEntity.java文件,
ExcelEntity.java中的属性就是需要读取表格的字段
package com.hxci.pojo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
public class ExcelEntity extends BaseRowModel {
private String id;
@ExcelProperty(index = 1 , value = "开课院系部")
private String yuanxi;
@ExcelProperty(index = 2 , value = "开课专业")
private String zhuanye;
@ExcelProperty(index = 3 , value = "课程代码")
private String daima;
@ExcelProperty(index = 4 , value = "课程名称")
private String mingcheng;
@ExcelProperty(index = 5 , value = "考试、考查")
private String xingzhi;
@ExcelProperty(inde