提交表单 附件上传 MultipartRequest接收

前台form提交,有附件,后台需要通过MultipartRequest对象接收,附加上传到服务器,数据库保存附件路径。

public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws ResourceException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        String usersoundDir = "userfiles" + File.separatorChar + "usersound"
                + File.separatorChar;// 创建服务器文件夹路径
        String path = ResourceContext.abs_path;// 获取tomcat服务器路径
        // 判断平台增加文件夹斜杠
        if (ResourceContext.app_os.equals("windows")) {
            path = path + usersoundDir;
        } else {
            path = path + File.separatorChar + usersoundDir;
        }

        // 判断有无文件夹,没有则创建
        File savePath = new File(path);
        if (!savePath.exists()) {
            savePath.mkdirs();
        }

        MultipartRequest multi = new MultipartRequest(request, path,
                20 * 1024 * 1024, "GBK");
        String fname = "";
        String url = "";
        Enumeration fnames = multi.getFileNames();
        while (fnames.hasMoreElements()) {
            String filename = (String) fnames.nextElement();
            File file = multi.getFile(filename);
            if (file != null && file.length() > 0) {
                fname = file.getName();
                int idx = fname.lastIndexOf(".");
                String extention = fname.substring(idx);// 文件后缀
                path = path + user.getFLogin() + extention;// 存入服务器的最终路径(音频路径+文件名)
                File newFile = new File(path);
                if (newFile.exists()) {// 如果服务器上有同名文件,先删除掉
                    newFile.delete();
                }
                file.renameTo(newFile);// 修改文件名
                url = "userfiles/usersound/" + user.getFLogin() + extention;// 存入数据库路径
            }
        }

        if ("".equals(url)) {
            url = multi.getParameter("f_soundfile_1");
        }

        org.oa.presentation.vojos.PopupUserDef pvud = new org.oa.presentation.vojos.PopupUserDef();
        pvud.setId(Long.parseLong(multi.getParameter("id")));// id
        pvud.setF_allowpop(Long.parseLong(multi.getParameter("f_allowpop")));// 允许弹出窗口
        pvud.setF_allowsound(Long.parseLong(multi.getParameter("f_allowsound")));// 允许播放声音
        pvud.setF_msec_hold(Long.parseLong(multi.getParameter("f_msec_hold")));// 窗口停留时间
        pvud.setF_msec_interval(Long.parseLong(multi
                .getParameter("f_msec_interval")));// 刷新间隔时间
        pvud.setF_soundfile(url);// 文件路径
        pvud.setF_userid(user.getId());// 用户id
        pvud.setF_allowintegrate(Long.parseLong(multi
                .getParameter("f_allowintegrate")));// 允许合并成一个窗口
        pvud.setF_allowmutipopup(Long.parseLong(multi
                .getParameter("f_allowmutipopup")));// 允许多次提醒

        // 存储用户定义信息
        resource.getCalenderListMonthService().saveObjectToDatabase(pvud,
                "T_HZOA_SYS_POPUP_USERDEF");
        request.setAttribute("rs", "1");
        return list(mapping, form, request, response);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值