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


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

    我项目存放图片的文件夹路径 C:UsersmiaozworkspaceookWebContentimages

    然后再tomcat服务器上有个缓存空间C:Usersmiaozworkspace.metadata.pluginsorg.eclipse.wst.server.core mp0wtpwebappsookimages

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

    例如在我的项目中

    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();
            }
           
  • 相关阅读:
    Java守护线程Daemon
    在for循环中创建双向链表
    Java泛型-官方教程
    大自然搬运工
    转 curl命令
    HashMap扩容问题及了解散列均分
    mysql 分组查询并取出各个分组中时间最新的数据
    CNN 模型复杂度分析
    Attention机制
    深度学习之目标检测
  • 原文地址:https://www.cnblogs.com/miaos/p/11029530.html
Copyright © 2020-2023  润新知