SSM文件上传(记录)

主要代码:
jsp:

 <form role="form" class="form-horizontal" method="post" action="${pageContext.request.contextPath}/file/addnewnews.do" enctype="multipart/form-data">
                    <div class="form-group">
                        <label for="newstitle" class="control-label col-lg-2">标题</label>
                        <div class="col-lg-8">
                            <input id="newstitle" name="newstitle" type="text" class="form-control" placeholder="请输入标题">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="typeid" class="control-label col-lg-2">频道</label>
                        <div class="col-lg-8">
                            <select id="typeid" name="typeid" class="form-control">
                                <c:forEach items="${types}" var="type" varStatus="i">
                                    <option value="${type.typeid}">${type.typename}</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="newsintro" class="control-label col-lg-2">导语</label>
                        <div class="col-lg-8">
                            <textarea id="newsintro" name="newsintro" class="form-control" style="resize:none;height: 100px;" placeholder="请输入说明"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-lg-2">具体内容</label>
                        <div class="col-lg-8">
                            <div id="div1">
                                <p>请输入新闻正文</p>
                            </div>
                            <textarea id="newstext" name="newstext" style="width:100%; height:200px;"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-lg-2" for="file">首图上传</label>
                        <div class="col-lg-8">
                            <input type="file" class="form-control" placeholder="请选择图片" name="file" id="file"/>
                            <p>
                                <span class="label label-warning">图片大小应小于5M</span>
                                <span class="label label-info">图片添加为可填项</span>
                            </p>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-lg-2 col-lg-offset-4">
                            <button type="submit" class="btn btn-success" >提交</button>
                        </div>
                        <div class="col-lg-2">
                            <a class="btn btn-warning" onclick="history.back(-1);">返回</a>
                        </div>
                    </div>
                </form>
           

controller层:

    //新编写新闻
    @RequestMapping("/addnewnews.do")
    public String addnewnews(NewNews newNews, HttpSession session,MultipartFile file,HttpServletRequest request){
        int adminid=(int)session.getAttribute("adminid");
        System.out.println(newNews);
        if(!file.isEmpty()){
            try {
                //文件上传路径
                String path = request.getSession().getServletContext().getRealPath("/upload");
                System.out.println(path);
                //设置图片名称
                //获取图片扩展名
                String picname=file.getOriginalFilename();
                String externname=picname.substring(picname.lastIndexOf(".")+1);
                int newsid=new Long(System.currentTimeMillis()/1000).intValue();
                newNews.setNewsid(newsid);
                String newpicname=String.valueOf(adminid)+String.valueOf(newsid)+"."+externname;
                //文件传输保存
                File f=new File(path,newpicname);
                if(!f.exists()){
                    f.mkdirs();
                }
                //如果图片存在就销毁再创建
                file.transferTo(f);
                //添加图片路径
                String url="upload/"+newpicname;
                newNews.setNewsfirstpic(url);

            }catch (Exception e) {
                e.printStackTrace();
            }
        }

重点:
1.如果不想写注释的话,画框两处参数要同名:
在这里插入图片描述
2.文件上传相关代码:

        if(!file.isEmpty()){
            try {
                //文件上传路径
                String path = request.getSession().getServletContext().getRealPath("/upload");
                System.out.println(path);
                //设置图片名称
                //获取图片扩展名
                String picname=file.getOriginalFilename();
                String externname=picname.substring(picname.lastIndexOf(".")+1);
                int newsid=new Long(System.currentTimeMillis()/1000).intValue();
                newNews.setNewsid(newsid);
                String newpicname=String.valueOf(adminid)+String.valueOf(newsid)+"."+externname;
                //文件传输保存
                File f=new File(path,newpicname);
                if(!f.exists()){
                    f.mkdirs();
                }
                //如果图片存在就销毁再创建
                file.transferTo(f);
                //添加图片路径
                String url="upload/"+newpicname;
                newNews.setNewsfirstpic(url);

            }catch (Exception e) {
                e.printStackTrace();
            }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值