我的servlet从图像名称(前三个字符)计算路径。由于页面上可能有很多图像,因此还会有许多这样的请求。我想实现最高的性能。这种方法会起作用,还是有一种方法可以改善它?
.... @WebServlet(name = "ZeroCopyServlet", urlPatterns = {"/book"}) ...
char[] chars = requestedFile.toCharArray();
char[] firstChar = new char[1];
char[] firstThreeChars = new char[3];
System.arraycopy(chars, 0, firstThreeChars, 0, 3);
System.arraycopy(chars, 0, firstChar, 0, 1);
String result1 = new String(firstChar);
String result2 = new String(firstThreeChars);
requestedFile = result1 + "/" + result2 + "/" + requestedFile;
Path filePath = basePath.resolve(requestedFile).normalize();
... fileChannel.transferTo ...
所有用于计算目录的逻辑与将照片从服务器提供到客户端的逻辑(Zerocopy)结合使用。也许我在做错一切,将这种逻辑放在servlet中是不好的做法。谢谢您的建议。
你可以通过正直来做到这一点 https://regex101.com/r/yeakju