Java 업로드 파일

13513 단어
파일 업로드
    @ResponseBody
    @RequestMapping("/doAdd")
    public Object doAdd(HttpServletRequest request, Advert advert, HttpSession session) {
        AjaxResult result = new AjaxResult();
        try {
            MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) request;

            MultipartFile mfile = mreq.getFile("advpic");

            String name = mfile.getOriginalFilename();//java.jpg
            String extname = name.substring(name.lastIndexOf(".")); // .jpg

            String iconpath = UUID.randomUUID().toString() + extname; //232243343.jpg

            ServletContext servletContext = session.getServletContext();
            String realpath = servletContext.getRealPath("/pics");

            String path = realpath + "\\adv\\" + iconpath;

            mfile.transferTo(new File(path));

            User user = (User) session.getAttribute(Const.LOGIN_USER);
            advert.setUserid(user.getId());
            advert.setStatus("1");
            advert.setIconpath(iconpath);

            int count = advertService.insertAdvert(advert);
            result.setSuccess(count == 1);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
            System.out.println(e.getStackTrace());
        }
        return result;
    }

html
                    <form id="advertForm" method="post" action="" enctype="multipart/form-data">
                        <div class="form-group">
                            <label for="name">    label>
                            <input type="text" class="form-control" id="name" name="name" placeholder="       ">
                        div>
                        <div class="form-group">
                            <label for="url">    label>
                            <input type="text" class="form-control" id="url" name="url" placeholder="       ">
                        div>
                        <div class="form-group">
                            <label for="advpic">    label>
                            <input type="file" class="form-control" id="advpic" name="advpic" placeholder="       ">
                        div>
                        <button id="saveBtn" type="button" class="btn btn-success"><i
                                class="glyphicon glyphicon-plus">i>   
                        button>
                        <button type="button" class="btn btn-danger"><i class="glyphicon glyphicon-refresh">i>   
                        button>
                    form>

javascript
<span style="color: #000000;">
    $(</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () {
        $(</span>".list-group-item").click(<span style="color: #0000ff;">function</span><span style="color: #000000;"> () {
            </span><span style="color: #0000ff;">if</span> ($(<span style="color: #0000ff;">this</span>).find("ul"<span style="color: #000000;">)) {
                $(</span><span style="color: #0000ff;">this</span>).toggleClass("tree-closed"<span style="color: #000000;">);
                </span><span style="color: #0000ff;">if</span> ($(<span style="color: #0000ff;">this</span>).hasClass("tree-closed"<span style="color: #000000;">)) {
                    $(</span>"ul", <span style="color: #0000ff;">this</span>).hide("fast"<span style="color: #000000;">);
                } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                    $(</span>"ul", <span style="color: #0000ff;">this</span>).show("fast"<span style="color: #000000;">);
                }
            }
        });
    });
    $(</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () {
        $(</span>"#saveBtn").click(<span style="color: #0000ff;">function</span><span style="color: #000000;"> () {
            </span><span style="color: #0000ff;">var</span> options =<span style="color: #000000;"> {
                url: </span>"${APP_PATH}/advert/doAdd.do"<span style="color: #000000;">,
                beforeSubmit: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () {
                    loadingIndex </span>= layer.msg('       ', {icon: 6<span style="color: #000000;">});
                    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>; <span style="color: #008000;">//</span><span style="color: #008000;">    true,  ,    .</span>
<span style="color: #000000;">                },
                success: </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> (result) {
                    layer.close(loadingIndex);
                    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (result.success) {
                        layer.msg(</span>"        ", {time: 1000, icon: 6<span style="color: #000000;">});
                        setTimeout(</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> () {
                            window.location.href </span>= "${APP_PATH}/advert/index.htm"<span style="color: #000000;">;
                        }, </span>1000<span style="color: #000000;">);
                    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
                        console.log(result.message);
                        layer.msg(</span>"        ", {time: 1000, icon: 5, shift: 6<span style="color: #000000;">});
                    }
                }
            };
            $(</span>"#advertForm").ajaxSubmit(options); <span style="color: #008000;">//</span><span style="color: #008000;">    </span>
            <span style="color: #0000ff;">return</span><span style="color: #000000;">;
            </span><%--$("#advertForm").attr("action", "${APP_PATH}/advert/doAdd.do");--%>
            <%--$("#advertForm").submit();--%><span style="color: #000000;">
        });
    });
</span>

좋은 웹페이지 즐겨찾기