@Test void getOriginalFileName() { when(part.getHeader("content-disposition")) .thenReturn("form-data; name=\"upload\"; filename=\"example.xls\""); String filename = FileUploadUtils.getOriginalFileName(part); assertEquals("example.xls", filename); }
public static String getOriginalFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); if (contentDisposition != null) { for (String cd : contentDisposition.split(";")) { if (cd.trim().startsWith("filename")) { String path = cd.substring(cd.indexOf('=') + 1).replaceAll("\"", "").trim(); Path filename = Paths.get(path).getFileName(); return StringUtils.hasText(filename.toString()) ? filename.toString() : null; } } } return null; }
FileUploadUtils { public static String getOriginalFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); if (contentDisposition != null) { for (String cd : contentDisposition.split(";")) { if (cd.trim().startsWith("filename")) { String path = cd.substring(cd.indexOf('=') + 1).replaceAll("\"", "").trim(); Path filename = Paths.get(path).getFileName(); return StringUtils.hasText(filename.toString()) ? filename.toString() : null; } } } return null; } }
FileUploadUtils { public static String getOriginalFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); if (contentDisposition != null) { for (String cd : contentDisposition.split(";")) { if (cd.trim().startsWith("filename")) { String path = cd.substring(cd.indexOf('=') + 1).replaceAll("\"", "").trim(); Path filename = Paths.get(path).getFileName(); return StringUtils.hasText(filename.toString()) ? filename.toString() : null; } } } return null; } private  FileUploadUtils(); }
FileUploadUtils { public static String getOriginalFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); if (contentDisposition != null) { for (String cd : contentDisposition.split(";")) { if (cd.trim().startsWith("filename")) { String path = cd.substring(cd.indexOf('=') + 1).replaceAll("\"", "").trim(); Path filename = Paths.get(path).getFileName(); return StringUtils.hasText(filename.toString()) ? filename.toString() : null; } } } return null; } private  FileUploadUtils(); static File saveToTempFile(Part part); static File saveToTempFolder(Part part); static String getOriginalFileName(Part part); }
FileUploadUtils { public static String getOriginalFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); if (contentDisposition != null) { for (String cd : contentDisposition.split(";")) { if (cd.trim().startsWith("filename")) { String path = cd.substring(cd.indexOf('=') + 1).replaceAll("\"", "").trim(); Path filename = Paths.get(path).getFileName(); return StringUtils.hasText(filename.toString()) ? filename.toString() : null; } } } return null; } private  FileUploadUtils(); static File saveToTempFile(Part part); static File saveToTempFolder(Part part); static String getOriginalFileName(Part part);  }