Adding Link file using APACHE POI XSSF is not accepting directory address and showing java.net.URISyntaxException

ぐ巨炮叔叔 提交于 2019-11-29 02:46:48

Finally I have done this ...Thanks to Gagravarr... Given me a good Idea.

boolean isDirCreated = false;//to create Screenshot directory just once

//Create Screenshot Directory.
public static void createDir(String ScreenshotDirAddress){
    if(!isDirCreated){
       File file= new File(ScreenshotDirAddress);
       if (!file.exists())
            file.mkdirs();
    isDirCreated=true;
    }
}


//hyperlink screenshot
public static void hyperlinkScreenshot(XSSFCell cell, String FileAddress){
    XSSFWorkbook wb=cell.getRow().getSheet().getWorkbook();
    CreationHelper createHelper = wb.getCreationHelper();
    CellStyle hlink_style = wb.createCellStyle();
    Font hlink_font = wb.createFont();
    hlink_font.setUnderline(Font.U_SINGLE);
    hlink_font.setColor(IndexedColors.BLUE.getIndex());
    hlink_style.setFont(hlink_font);
    Hyperlink hp = createHelper.createHyperlink(Hyperlink.LINK_FILE);
    FileAddress=FileAddress.replace("\\", "/");
    hp.setAddress(FileAddress);
    cell.setHyperlink(hp);
    cell.setCellStyle(hlink_style);
}

//take screenshot
public static void takeScreenShot(WebDriver driver, String screenshotName, XSSFCell cell){
    createDir();
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
        String FullAddress=System.getProperty("user.dir")+"/"+ScreenshotDirAddress+"/"+screenshotName+".png";
        FileUtils.copyFile(scrFile, new File(FullAddress));
        hyperlinkScreenshot(cell, FullAddress);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The Main problem that I have faced was related to the URL. If I used "\" it is showing illegalArgumentException, while replacing it with "/" is working fine. I don't know the exact reason but if

        FileAddress=FileAddress.replace("\\", "/");

is commented... it is showing illegal argument exception. So if the URL is

D:\eclipse\workspace\TestApp\Screenshot\TestResult\Test.png

will show the illegalArgumentException. However if "\" is replaced by "/" to make it D:/eclipse/workspace/TestApp/Screenshot/TestResult/Test.png

it is working properly. Both the URL is correct and opening the same file in browser or even using manually in excel file for hyperlink the same file its working fine. I don't know why it is showing exception in Apache POI, may be a possible Bug.

Anwar Jamil Khan
public static String takeScreenShot(XSSFRow wrow, XSSFCell cell,String fileName)
    {
        try {

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
            LocalDateTime now = LocalDateTime.now();    
            fileName = fileName + dtf.format(now);
            fileName=fileName.replace(":","");

            File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            String screen_path;
            ReadConfigProperty file = new ReadConfigProperty();
            FileUtils.copyFile(src, new File("Screenshot\\" + fileName + ".jpg"));
            screen_path= file.reportFilePath() + fileName + ".jpg";


            screen_path= "file:///"+screen_path;

            cell = wrow.createCell(7);
            //cell.setCellValue(screen_path);


            XSSFWorkbook wb=cell.getRow().getSheet().getWorkbook();
            CreationHelper createHelper = wb.getCreationHelper();
            XSSFCellStyle hlink_style = wb.createCellStyle();
            XSSFFont hlink_font = wb.createFont();
            hlink_font.setUnderline(Font.U_SINGLE);
            hlink_font.setColor(IndexedColors.BLUE.getIndex());
            hlink_style.setFont(hlink_font);
            Hyperlink hp = createHelper.createHyperlink(Hyperlink.LINK_FILE);
            screen_path=screen_path.replace("\\", "/");
            hp.setAddress(screen_path);
            cell.setHyperlink((org.apache.poi.ss.usermodel.Hyperlink) hp);
            cell.setCellStyle(hlink_style);

            return screen_path;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!