ajax上传表单数据,包含文件

前端代码:

<script>
    $(function () {
        $("#btn").click(function () {
            var formData=new FormData($("#Form")[0]);
            $.ajax({
                async : false,
                cache : false,
                type : "post",
                data : formData,
                url:"FilmServlet?method=addFilm",
                dataType:"text",
                contentType: false, //必须
                processData: false, //必须
                success:function (ip) {
                    if (ip=="1"){
                        alert("添加成功!");
                    } else if (ip=="0"){
                        alert("添加失败!")
                    }else {
                        alert("其他!")
                    }
                }
            })
        })
    })
</script>
<body>
<div class="container">

    <!-- <form class="form-inline"> -->
    <form class="form-horizontal" id="Form" method="post" enctype="multipart/form-data">
        <!-- 水平列表表单 -->
        <div class="form-group">
            <label for="movename" class="col-sm-2 control-label">电影名:</label>
            <div class="col-sm-2">
                <input class="form-control" type="text" name="movename" id="movename">
            </div>
        </div>

        <div class="form-group">
            <label for="introduction" class="col-sm-2 control-label">简介:</label>
            <div class="col-sm-8">
                <textarea class="form-control" id="introduction" name="introduction"></textarea>
            </div>

        </div>

        <div class="form-group">
            <label for="duration" class="col-sm-2 control-label">电影时长:</label>
            <div class="col-sm-2">
                <input class="form-control" type="text" name="duration" id="duration">
            </div>
        </div>

        <div class="form-group">
            <label for="filmcover" class="col-sm-2 control-label">电影封面:</label>
            <div class="col-sm-2">
                <input type="file" name="filmcover" id="filmcover">
            </div>
        </div>

        <div class="form-group">
            <label for="filmcover" class="col-sm-2 control-label">电影彩蛋:</label>
            <div class="col-sm-2">
                <input type="file" name="eggs" id="eggs">
            </div>
        </div>
        <div class="form-group">
            <label for="price" class="col-sm-2 control-label">电影价格:</label>
            <div class="col-sm-2">
                <input type="text" name="price" id="price">
            </div>
        </div>

        <div class="form-group">
            <label for="shelftime " class="col-sm-2 control-label">上架时间:</label>
            <div class="col-sm-2">
                <input type="date" name="shelftime " id="shelftime ">
            </div>
        </div>

        <div class="form-group">
            <label for="undertime" class="col-sm-2 control-label">下架时间:</label>
            <div class="col-sm-2">
                <input type="date" name="undertime" id="undertime">
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-2 col-md-offset-2">
            <input type="button" value="添加" class="btn" id="btn">
            </div>
        </div>
    </form>
</div>

后端代码:

 //获取工厂
            FileItemFactory factory = new DiskFileItemFactory();
            //获取上传文件工具类
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setHeaderEncoding("utf-8");
            String newFileName = null;
            String fName = null;//电影名
            String introduction = null;//介绍
            float filmTime = 0;//电影时长
            String filmCover = null;//电影封面
            String filmEggs = null;//电影彩蛋
            String actorName = null;//明星名
            String actorphoto = null;//明星头像
            String paraName = null;//参数名称
            float price = 0;//票价
            //上架时间
            String shelftime=null;
            //下架时间
            String undertime=null;
            try {
                List<FileItem> list = upload.parseRequest(request);
                for (FileItem item : list) {
                    paraName = item.getFieldName();
                    if (!item.isFormField()) {
                        //存储到服务器的web工程本地img子目录中
                        String realPath = request.getServletContext().getRealPath("");
                        int pos = item.getName().lastIndexOf(".");
                        newFileName = UUID.randomUUID().toString().replaceAll("-", "")
                                + item.getName().substring(pos);
                        System.out.println(newFileName);
                        if ("filmcover".equals(paraName)) {
                            filmCover = newFileName;
                        } else if ("eggs".equals(paraName)) {
                            filmEggs = newFileName;
                        } else if ("icon".equals(paraName)) {
                            actorphoto = newFileName;
                        }
                        String filePath = realPath + "images/" + newFileName;
                        System.out.println("存储路径 :" + filePath);
                        File file = new File(filePath);
                        item.write(file);
                    } else {
                        //非文件项
                        if ("movename".equals(paraName)) {
                            fName = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");
                        } else if ("introduction".equals(paraName)) {
                            introduction = new String(item.getString().getBytes("ISO-8859-1"), "UTF-8");
                        } else if ("duration".equals(paraName)) {
                            filmTime = Float.parseFloat(item.getString());
                        } else if ("price".equals(paraName)) {
                            price = Float.parseFloat(item.getString());
                        }else if("shelftime".equals(paraName)){
                            shelftime=item.getString();
                        }else if ("undertime".equals(paraName)){
                            undertime=item.getString();
                        }
                    }

                }
                Film film = new Film(1, fName, filmCover, filmEggs, introduction, 1, filmTime, price,shelftime,undertime);
                boolean flag = service.addFilm(film);
                if (flag) {
                    jsonStr = "1";
                } else {
                    jsonStr = "0";
                }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java后端指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值