首页 > Web开发 > 详细

解决web网页改变起引用的图片,刷新页面仍然显示之前的图片

时间:2019-06-16 11:07:58      阅读:195      评论:0      收藏:0      [点我收藏+]

主要是tomcat服务器上的缓存引起的,只要在更新图片的时候同时给缓存更新即可

我项目存放图片的文件夹路径 C:\Users\miaoz\workspace\book\WebContent\images

然后再tomcat服务器上有个缓存空间C:\Users\miaoz\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\book\images

当刷新页面时图片还是从缓存空间中取,所以才会导致更新图片还是显示之前的,这里应该同时给两个路径的图片进行更新

例如在我的项目中

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        String bookinfo[] = {"","","","","","","",""};
        String imagid=new Connect(1).getimag_seq();
        int count=0;
        request.setCharacterEncoding("utf-8");
        DiskFileItemFactory factory = new DiskFileItemFactory();
        String path = request.getRealPath("/images");//这是缓存中图片的路径
        factory.setRepository(new File(path));
        //设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
        factory.setSizeThreshold(1024*1024) ;
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List<FileItem> list = (List<FileItem>)upload.parseRequest(request);
            for(FileItem item : list){
                String name = item.getFieldName();
                if(item.isFormField()){
                    String value = item.getString() ;
                    bookinfo[count++]=value;
                    System.out.println(name+"-->"+value);
                }else{
                    String value = item.getName() ;
                    int start = value.lastIndexOf("\\");
                    String filename = value.substring(start+1);
                    request.setAttribute(name, filename);
                    String path1=path.substring(0,path.length()-75)+"\\book\\WebContent\\images";//这是真实项目的图片路径
                    System.out.println(path);
                    OutputStream out = new FileOutputStream(new File(path,imagid+".jpg"));
                    OutputStream out1 = new FileOutputStream(new File(path1,imagid+".jpg"));//这里同时打开两个stream,分别是缓存与真实路径
                    InputStream in = item.getInputStream() ;
                    int length = 0 ;
                    byte [] buf = new byte[1024] ;
                    while( (length = in.read(buf) ) != -1){
                        //同时将图像写入两个路径中
                        out.write(buf, 0, length);
                        out1.write(buf, 0, length);
                    }
                    in.close();
                    out.close();
                    out1.close();
                }
            }
        }catch (FileUploadException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
       

 

解决web网页改变起引用的图片,刷新页面仍然显示之前的图片

原文:https://www.cnblogs.com/miaos/p/11029530.html

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