Map<String, Object> map = Maps.newHashMap(); if (key.equals(SignUtils.getKey(url))) { try { String path = request.getSession().getServletContext().getRealPath(url); String[] fv = path.split("\\."); File newFile = new File(fv[0]+"_sign.png"); // The image generated with the following three lines of code is delayed, that is, it can only be accessed a few seconds after the generation is completed. The specific reason is not clear // String newUrl = fv[0]+"_sign.png"; // String newFilePath = request.getSession().getServletContext().getRealPath(newUrl);//fv[0]+"_sign.png"; // File newFile = new File(newFilePath); File oldFile = new File(request.getSession().getServletContext().getRealPath(url)); InputStream in = new FileInputStream(oldFile); byte[] b = IOUtils.toByteArray(in); b = ImageUtils.transferAlpha(ImageUtils.ByteToBufferedImage(b), 150, x, y, width, height, rotate); Files.write(b,newFile); map.put("result", true); map.put("url", SignUtils.getUrl()+newFile.getName()); } catch (Exception e) { e.printStackTrace (); map.put("result", false); map.put("message", "操作失败:"+e.getMessage()); } } else { map.put("result", false); map.put("message", "Illegal operation"); } response.flushBuffer(); return map;
This program is to cut out a picture in a web directory, and then save it with another name, and then return the path of the new picture. As a result, the three lines that are commented out are immediately accessible, but it cannot be accessed. Change the way of writing. .
Please see the comments. In fact, the absolute paths obtained by the two are exactly the same, but the second one (the commented out code) has a delay, and it will take three or four seconds to access it.