html:
<!--upload html-->
<input type="file" id="file" name="file" value="选择文件" />
<input type="button" value="上传" οnclick="upload();"/>
<!--upload js--->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
<script type ="text/javascript">
var upload = function(){
$.ajaxFileUpload({
url: 'map/upload',
secureuri: false,
fileElementId: 'file',
dataType: 'json',
success: function (data, status){
alert("上传成功");
},
error: function (data, status, e){
alert("上传失败");
}
});
}
</script>
java:
/**
* 所用jar
这里用的sping mvc 当然用其他的上传得到流也是可以的
* jxl-2.4.3.jar
* org.apache.common.io.jar
* org.apache.common.fileupload.jar
*/
//上传接收
@RequestMapping("/upload")
public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List<MultipartFile> fileList = multipartRequest.getFiles("file");
for (MultipartFile mf : fileList) {
if(!mf.isEmpty()){
parseExcel(mf);
break;
}
}
//返回json
toJson("{'status', 1}");
}
//解析表格
public void parseExcel(MultipartFile mf){
Workbook workbook = null ;
try {
workbook = Workbook.getWorkbook(mf.getInputStream());
Sheet sheet = workbook.getSheet(0);
int count = sheet.getRows();
for (int i= 1 ;i < count;i ++){
Cell[] cells = sheet.getRow(i);
System.out.println(cells);
}
}catch (Exception e){
e.printStackTrace();
} finally {
if (workbook != null ){
workbook.close();
}
}
}
原创:blog.csdn.net/qilin001cs