zoukankan      html  css  js  c++  java
  • “图片”--上传(文件上传一致,单文件)

    html代码:

    <form action="GoodsAction_modify.do" method="post" enctype="multipart/form-data">
        <li><span>图片</span><input type="file" name="image" id="image" tabindex="5" onchange="save()"/></li>
    ...
    </form>

    注意:enctype="multipart/form-data"

    java代码:

        //注意,image并不是指前端jsp上传过来的图片文件本身,而是文件上传过来存放在临时文件夹下面的文件
        private File image;
        //提交过来的image的名字
        private String imageFileName;
        get...set...
      
    public void modify() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=utf-8"); String realPath = ServletActionContext.getServletContext().getRealPath("/images"); //绝对路径 //imageFileName = new Date().getTime()+".jpg"; //修改文件名称 File saveDir=new File(realPath); //按照realPath构建一个File实例 //if(!saveDir.exists()) saveDir.mkdirs(); //判断File实例对应的文件是否存在,如果不存在,则按照路径构建一个文件夹。 if (image != null) { File saveFile = new File(saveDir, imageFileName); //按照父目录saveDir路径和文件名构建一个File实例 if (!saveFile.getParentFile().exists()) { //判断saveFile的父目录文件是否存在,如果不存在,则按照路径构建一个文件夹。 saveFile.getParentFile().mkdirs(); } try { FileUtils.copyFile(image, saveFile); //根据saveFile所包含路径信息,把image写入文件系统(硬盘) } catch (Exception e) { e.printStackTrace(); } } Goods goods = goodsService.getEntity(id); id = goods.getId(); if(goods != null){ goods.setName(name); goods.setDescs(descs); goods.setCategoryid(categoryid); goods.setPrice(price); goods.setUserid(userid); goods.setImage(imageFileName); goodsService.saveOrUpdateEntity(goods); response.getWriter().write("<script>alert('修改成功');window.location.href='GoodsAction_list.do'</script>"); }else{ response.getWriter().write("<script>alert('修改失败');history.go(-1);</script>"); } }
  • 相关阅读:
    机器学习笔记(四)---- 逻辑回归的多分类
    在modelarts上部署backend为TensorFlow的keras模型
    深度学习在其他领域的应用1:密码破解
    Reactive(2) 响应式流与制奶厂业务
    如何把图片变得炫酷多彩,Python教你这样实现!
    漫谈边缘计算(三):5G的好拍档
    机器学习笔记(三)---- 逻辑回归(二分类)
    华为云数据库携新品惊艳亮相2019华为全联接大会
    100 个网络基础知识普及,看完成半个网络高手
    最大流
  • 原文地址:https://www.cnblogs.com/Crysta1/p/6178980.html
Copyright © 2011-2022 走看看