4

I am using this code to read from an URL and the code is giving me this error:

Exception in thread "main" java.io.FileNotFoundException: C:\Work\Projects\WikipediaTemplate\codes\Sweble\WikipediaTemplateGenerator\tempFiles\htmlunit2229583992082609160.tmp (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.(FileInputStream.java:138)
    at com.gargoylesoftware.htmlunit.DownloadedContent$OnFile.getInputStream(DownloadedContent.java:81)
    at com.gargoylesoftware.htmlunit.WebResponseData.getStream(WebResponseData.java:91)
    at com.gargoylesoftware.htmlunit.WebResponseData.getInputStream(WebResponseData.java:156)
    at com.gargoylesoftware.htmlunit.WebResponse.getContentAsStream(WebResponse.java:241)
    at com.gargoylesoftware.htmlunit.util.WebResponseWrapper.getContentAsStream(WebResponseWrapper.java:58)
    at com.gargoylesoftware.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:150)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:468)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:342)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:407)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:392)
    at etc.TestWebClient.main(TestWebClient.java:64)

I am using HTMLUnit 2.15.

The code is:

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    File file=new File("tempFiles");
    file.mkdir();
    System.setProperty("java.io.tmpdir", file.getCanonicalPath());;
    String url="http://www.nhs.uk/conditions/Norovirus/Pages/Introduction.aspx";

    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(false);
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setThrowExceptionOnScriptError(false);

    webClient.getOptions().setTimeout(120000);
    webClient.getOptions().setUseInsecureSSL(true);
    List<String> urlVisited=new ArrayList<String>();
    System.err.println("URL Link:"+url);

    int status =0;
    status=webClient.getPage(url).getWebResponse()
            .getStatusCode();

    System.out.println("Web response status:"+status);
    //webClient.getOptions().setJavaScriptEnabled(false);
    String directory="";

    LinkOption options = null;
    //Files.getOwner(file.toPath(), options);
    System.out.println(file.isDirectory()+"/"+file.canWrite());
   // boolean bval = file.setWritable(true,false);
    String currentDirectory = System.getProperty("user.dir");
    System.out.println(currentDirectory);
    //System.out.println(bval);
    Page rawpage = webClient.getPage(url);
}
Makyen
  • 31,849
  • 12
  • 86
  • 121
SidML
  • 183
  • 1
  • 7
  • Have you checked whether the mkdir function returns successfully and whether it actually creates that folder? – Ashley Frieze Jan 04 '15 at 22:04
  • Yes, the directory exists as I checked with the line: System.out.println(file.isDirectory()+"/"+file.canWrite()); – SidML Jan 04 '15 at 22:05

1 Answers1

0

I know this question is years ago but, I faced a similar problem and resolved, so I thought I could answer it to help somebody.

First of all, if using HtmlUnit, there is this github thread which may help. I was using HtmlUnit version 2.67, and the solution mentioned there, didn´t work for me. So, I installed Selenium 4.7.2 dependency in my project.

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.7.2</version>
</dependency>

Then, i did this (this code is specific for my purposes, but it has some similarities with the one in the question):

URL url;
    try {
        url = new URL("URL that I needed to get Input Stream From...");
    } catch (Exception e) {
        return null;
    }

    HttpRequest req = new HttpRequest(org.openqa.selenium.remote.http.HttpMethod.GET, url.toString());
    req.setHeader("Authorization", "Bearer " + token);
    HttpResponse responseSel = null;
    
    HttpClient client = HttpClient.Factory.createDefault().createClient(url);
    responseSel = client.execute(req);
    if (responseSel != null && responseSel.getStatus() == 200)
        return responseSel.getContent().get();
    return null;

That's it. Hope it helps.

gbossa
  • 377
  • 1
  • 3
  • 18