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>"); } }
  • 相关阅读:
    CentOS上手动配置nginx.services
    Mac安装软件时,提示文件已损坏,需要移动到废纸篓的解决方法
    Jumpserver安装部署
    Linux服务器测试带宽
    Zabbix_server执行window脚本出现中文乱码如何解决
    四行shell脚本实现Zabbix_server 的高可用
    Kubernetes Pod故障归类与排查方法
    Nginx配置location与rewrite规则教程
    ipa文件信息检查工具
    申请免费SSL证书
  • 原文地址:https://www.cnblogs.com/Crysta1/p/6178980.html
Copyright © 2011-2022 走看看