首页 > Web开发 > 详细

关于文件上传的坑,tomcat一重启图片就消失

时间:2019-10-16 10:07:29      阅读:202      评论:0      收藏:0      [点我收藏+]

首先在tomcat的conf的server.xml中配置一个配置

 <Context path ="/File" docBase ="D:/File" debug ="0" privileged ="true">
        </Context>

在后台用的时候就可以这样写

String productName = req.getParameter("productName");
        String productDesc = req.getParameter("productDesc");
        String productPrice = req.getParameter("productPrice");
        Part cover = req.getPart("productImage");
        Long da = new Date().getTime()/1000;
        String filePath = "/"+da+"_"+cover.getSubmittedFileName();
        File file = new File("/File"); //这里是重点
        String destPath = file.getAbsolutePath()+filePath;
        new File(destPath).getParentFile().mkdir();
        cover.write(destPath);
        int categoryId = Integer.parseInt(req.getParameter("categoryId"));
        Product product = new Product(productName,productDesc,new BigDecimal(productPrice),filePath,categoryId);
        ProductService service = new ProductService();
        service.insertProduct(product);
        resp.sendRedirect(req.getContextPath()+"/index");

上面我标记的地方就会去找D://File

idea里面在配置一个东西

 

技术分享图片

在上面的HTTP port 右侧点上勾

就可以用在jsp使用了

 

  <img id="image" src="/File${product.productImage}" style="width: 100px" >

 

关于文件上传的坑,tomcat一重启图片就消失

原文:https://www.cnblogs.com/LixiaoFeng1650062546/p/11683297.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!