主要是 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();
- }
来源: http://www.bubuko.com/infodetail-3093894.html