用form表单上传

jsp页面:

    <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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值