0

When i hit the URL, i have many files and new file is added sometimes. I want to read the content of newly added file in the URL. Currently i can able to read the file content when given the file name. I could not able to read the latest file without giving any filename. Any suggestions on how to get the latest file and read the content..

Code to read the content from a file when given the filename in the URL:

URL url = new URL("https://xyz.abc.com/filesPath/myFile.pptx");
HttpsURLConnection connection = ((HttpsURLConnection) url.openConnection());
        InputStream input;
        input = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String msg = null;
        while ((msg = reader.readLine()) != null) {
System.out.println(msg);
        }

In the above code, the url given has the file path which opens the file and read the content. How can i read the content of the latest file from the available files list(https://xyz.abc.com/filesPath/).

The url path https://xyz.abc.com/filesPath/ has multiple files uploaded.

--EDITED--

Below is the html response when tried with the suggested code mentioned in How to get list of files/directories of an directory url in java?

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
    <html dir="ltr" class="ms-isBot" lang="en-US">
     <head>
      <meta name="GENERATOR" content="AllAccess" />
      <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
      <meta http-equiv="X-UA-Compatible" content="IE=10" />
      <meta http-equiv="Expires" content="0" />
      <!-- === Page Title loaded from page or page layout ==================================================================== -->
      <title>
        Raw Data - All Documents
    </title>
      <!-- === Favicon / Windows Tile ==================================================================== -->
      <link rel="shortcut icon" href="/_layouts/15/xxinfo.icon" id="favicon" />
       <script type="text/javascript".....>
      //..script tags
    <body>
      ...
 <tr class="ms-alternating ms-itmhover" iid="39,1428,0">
                    <td class="ms-vb-itmcbx ms-vb-firstCell"><input type="checkbox" class="s4-itm-cbx" /></td>
                    <td class="ms-vb-icon"><img border="0" alt="spreadTestReport.xls" title="spreadTestReport.xls" src="/_layouts/15/images/icxls.png?rev=23" /></td>
                    <td height="100%" onmouseover="OnChildItem(this)" class="ms-vb-title">
                     <div class="ms-vb itx" onmouseover="OnItem(this)" ctxname="ctx39" id="1428" field="LinkFilename" perm="0x1b03c4312ef" eventtype="">
                      <a onfocus="OnLink(this)" href="/MyDocuments/Report/spreadTestReport.xls" onmousedown="return VerifyHref(this,event,'1','SharePoint.OpenDocuments','1https://my20infosw.share.gm.com/_layouts/15/WopiFrame.aspx?sourcedoc=/Shared%20Documents/08.Test%20Report/MY20%20Test%20Reports/Sanity/Raw%20Data/spreadTestReport.xls&amp;action=default')" onclick="return DispEx(this,event,'TRUE','FALSE','{$thisNode/@File_x0020_Type.url}','{$thisNode/@File_x0020_Type.progid}','{$XmlDefinition/List/@DefaultItemOpen}','{$thisNode/@HTML_x0020_File_x0020_Type.File_x0020_Type.mapcon}','{$thisNode/@HTML_x0020_File_x0020_Type}','{$thisNode/@serverurl.progid}','{$thisNode/@CheckoutUser.id}','{$Userid}','{$XmlDefinition/List/@ForceCheckout}','{$thisNode/@IsCheckedoutToLocal}','{$thisNode/@PermMask}')">spreadTestReport</a>
                     </div>
                     <div class="s4-ctx" onmouseover="OnChildItem(this.parentNode); return false;">
                      <span>&nbsp;</span>
                      <a onfocus="OnChildItem(this.parentNode.parentNode); return false;" onclick="PopMenuFromChevron(event); return false;" href="javascript:;" title="Open Menu"></a>
                      <span>&nbsp;</span>
                     </div></td>
                    <td class="ms-vb2">
                     <nobr>
                      7/17/2016 10:52 PM
                     </nobr></td>
                    <td class="ms-vb-user"><span class="ms-noWrap"><span class="ms-imnSpan"><a href="#" onclick="IMNImageOnClick(event);return false;" class="ms-imnlink ms-spimn-presenceLink"><span class="ms-spimn-presenceWrapper ms-imnImg ms-spimn-imgSize-10x10"><img name="imnmark" class="ms-spimn-img ms-spimn-presence-disconnected-10x10x32" title="" showofflinepawn="1" src="/_layouts/15/images/spimn.png?rev=23" alt="No presence information" sip="rawaa.kashat@gm.com" id="imn_108985,type=sip" /></span></a></span><span class="ms-noWrap ms-imnSpan"><a href="#" onclick="IMNImageOnClick(event);return false;" class="ms-imnlink" tabindex="-1"><img name="imnmark" class="ms-hide" title="" showofflinepawn="1" src="/_layouts/15/images/blank.gif?rev=23" alt="" id="imn" /></a><a class="ms-subtleLink" onclick="GoToLinkOrDialogNewWindow(this);return false;" href="/_layouts/15/userdisp.aspx?ID=113">Spread</a></span></span></td>
                   </tr>

Filename is spreadTestReport.xls in the above mentioned response.

sss
  • 5
  • 1
  • 5
  • Do you have access to the list of files? If the server doesn't provide a directory listing, you will probably not be able to do it. – Compass Jun 19 '17 at 20:19
  • @Compass- yes i have access to the list of files. I can able to click on any file and open it.Once the file is opened , if i gave the entire path in my code, i can able to read the content too.. – sss Jun 19 '17 at 20:23

1 Answers1

0

Assuming invocation of this URL: https://xyz.abc.com/filesPath/ gives you a list of files. The response could be form of a html/text/json response.

Take this response parse it and create a initial List with each entry as a file.

Then subsequent querying of the same URL will allow you to diff it against the previous list, see if a new file name was added and then read the new file name https://xyz.abc.com/filesPath/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Farooq Khan
  • 570
  • 4
  • 11
  • Could you provide me any example link, as a newbie it would be good to check an example.@Farooq Khan – sss Jun 19 '17 at 20:29
  • See the accepted answer at: https://stackoverflow.com/questions/11561608/how-to-get-list-of-files-directories-of-an-directory-url-in-java – Farooq Khan Jun 19 '17 at 20:33
  • -Thanks.but with the accepted answer in the link https://stackoverflow.com/questions/11561608/how-to-get-list-of-files-directories-of-an-directory-url-in-java , for me its displaying entire html content instead of file names as shown in the link. – sss Jun 19 '17 at 20:44
  • Can you paste/show me atleast some of that html content you are seeing – Farooq Khan Jun 19 '17 at 20:48
  • You missed the important part, I needed to see the body part which you have truncated. Anyway, the trick is to tell Jsoup the HTML Element to pick. Like in the accepted stackoverflow answer the code statement (Element file : doc.select("td.right td a")) { you need to find equivalent in your HTML response – Farooq Khan Jun 19 '17 at 20:58
  • sorry, just edited. File name is embedded in tags spreadTestReport.xls. The code mentioned in the URL also suggested with td. – sss Jun 19 '17 at 21:08
  • Use a CSS selector like this maybe `Elements e1s = doc.select("tr[class^=ms-alternating]");` and then iterate the e1s in a for loop and then maybe for each element you can call `Elements tds = sectd.select("td");` and then get to the value by using the correct index within the tds[1] and then select the image and get the altname – Farooq Khan Jun 19 '17 at 21:18
  • Basically try to study the JSoup documentation to understand how to use the CSS selectors that should do the trick – Farooq Khan Jun 19 '17 at 21:22
  • Please see my post edited section. I have added one section. For each file name there are separate . Sure, I wil try and update you. Thanks. – sss Jun 19 '17 at 21:24