用form表单上传
2016-04-08 09:30
99 查看
jsp页面:
controller java 页面
注意:此处应用到了上传工具。工具地址是:
https://github.com/hyberbin/J-Excel
<div id="file-window" class="easyui-window" style="display: none;"> <form action="${ctx}/office/up" method="post" enctype="multipart/form-data" id="uploadFile"><!--enctype="multipart/form-data" 上传必须写 controller必用 MultipartFile file 来接 --> <table> <tr> <td>第一步:选择要导入的excel文件</td> </tr> <tr> <td> <input type="file" name="fileOffice" id="fileOffice" class="easyui-validatebox"style="display: none;"/><!--name="fileOffice"必须与controller对应的参数名相同 @RequestParam(value = "fileOffice") MultipartFile file --> <input type="button" value="选择文件" onclick="choseFile();"/> </td> </tr> <tr> <td>第二步:开始上传</td> </tr> <tr> <td><input type="button" value="开始上传" onclick="up();"/></td> </tr> <tr> <td>第三步:导入数据</td> </tr> <tr> <td><input type="button" value="导入数据" onclick="refash();"/></td> </tr> </table> </form> </form> </div>
controller java 页面
@RequestMapping(value = "/office/up", method = RequestMethod.POST) public String uploadOffice(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "fileOffice") MultipartFile file) throws IllegalStateException, IOException { // User user = request.getParameter(user.getRealName()); String[] cols = { "seq", "office", "address", "postcode", "manager", "mobile", "telephone", "fax"}; if (!file.isEmpty()) { // 文件保存路径 String filePath = request.getSession().getServletContext() .getRealPath("/") + "upload/" + file.getOriginalFilename(); File targetFile = new File(filePath, file.getOriginalFilename()); if (!targetFile.exists()) { targetFile.mkdirs(); } // 保存 file.transferTo(targetFile); List<Office> officeList = ExcelUtil.importExcel(Office.class, FileUtil.getFileStream(targetFile), cols, FileUtil.getExcelFileType(targetFile)); if(officeList.size() == 0){ return "2"; }else{ if (officeService.uploadOffice(officeList)) { return "1"; } else { return "3"; } } } else { return "2"; } }
注意:此处应用到了上传工具。工具地址是:
https://github.com/hyberbin/J-Excel
相关文章推荐
- Linux chage --密码时效管理
- 自定义UICollectionView的cell01
- linux下用 fdisk把新硬盘 分区、格式化、挂载!
- 界面设计小作业
- 操作系统之作业调度
- Storm记录01-- 实时计算系统
- 页面缓存js问题解决
- 用js导出页面table到excel表中
- 让创建按钮的大小和图片一样大
- HTML 5中的文件处理之FileAPI(转载)
- Yeoman官网新手教程:第二章 下载Yeoman生成器
- spring框架整合使用定时任务框架java quartz的示例代码配置
- RxJava 官方文档中文翻译
- CSS3选择器(上)
- Linux cd --更改当前工作目录
- Linux cat --连接文件或标准输出并打印
- 创建一个不断循环切换的动画
- 如何将xib与一个控制器关联
- ceph源码解析--osd篇
- 【备忘】sp_helptext对有架构名schema的存储过程如何调用