Read all files under static resource files in the project, such as all pictures

Source: Internet
Author: User

request requires Incoming folder path

This is the backstage code.
ImportJava.io.File;Importjava.io.IOException;Importjava.net.MalformedURLException;Importjava.util.ArrayList;Importjavax.servlet.ServletException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;/*** File Preview helper class *@authorzhy**/@Controller Public classFilebrowseutil {/*** Get an filelist array of files in the incoming file path via AJAX request *@paramreq *@paramRESP *@paramParams folder Path parameter *@return * @throwsservletexception *@throwsIOException *@throwsmalformedurlexception*/@RequestMapping ("/getfilelist") @ResponseBodyprotectedArraylist<string>Calculategeoservlet (httpservletrequest req, httpservletresponse resp,string params)throwsservletexception, IOException, malformedurlexception {ArrayList<String> filelist=NewArraylist<string>(); String dir=req.getsession (). Getservletcontext (). Getrealpath (params); FileList=GetFiles (dir); returnfileList; } /*** Recursively get all the directories and their files under a certain path *@paramfilePath File path *@return */ PublicArraylist<string>getFiles (String filePath) {ArrayList<String> fileList =NewArraylist<string>(); File Root=NewFile (FilePath); file[] Files=Root.listfiles (); for(File file:files) {if(File.isdirectory ()) {/** Recursive invocation*/GetFiles (File.getabsolutepath ()); Filelist.add (File.getabsolutepath ()); } Else{String Picpathstr=File.getabsolutepath ();//String picpathstr = File.getabsolutepath (). ReplaceAll ("\\\\", "//");Filelist.add (Getfilenamewithsuffix (PICPATHSTR)); } } /*For (String str:filelist) {System.out.println (str); }*/ returnfileList; } /*** reserved file name and suffix*/ Publicstring Getfilenamewithsuffix (String pathandname) {intStart = Pathandname.lastindexof ("\ \"); if(Start! =-1 ) { returnPathandname.substring (start + 1)); } Else { return NULL; } } /*** Reserved filenames only do not retain suffixes*/ Publicstring GetFileName (String pathandname) {intStart = Pathandname.lastindexof ("\ \"); intEnd = Pathandname.lastindexof ("."); if(Start! =-1 && End! =-1) { returnPathandname.substring (start + 1), end); } Else { return NULL; } } }

Read all files under static resource files in the project, such as all pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.