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>"); } }
  • 相关阅读:
    JavaSE 学习笔记04丨异常
    Codeforces Round #677 (Div. 3) E、G题解
    JavaSE 学习笔记03丨继承、接口、多态、内部类
    ftp通过了用户验证但是连接超时
    实型变量
    3dmax放样
    画直线算法
    VAE变分自动编码器
    RNN 、LSTM长短期记忆网络
    java比较字符串
  • 原文地址:https://www.cnblogs.com/Crysta1/p/6178980.html
Copyright © 2011-2022 走看看