Java implementation of Word frequency statistics--web migration

Source: Internet
Author: User
Tags save file

  This time the original console project was migrated to the Web project, still retains the original version of the console.

Demand:

1. Move the program to the Web platform and receive the file by uploading txt to the user;

2. On the page to give the link (if there is a cover, the author, the number of words, pages and other information better) or form, showing the classical English novel word frequency statistical results;

3. Support user to customize Word delimiter;

4. Vocabulary range Comparison (additional items).

Realize:

  1. Migrate to the Web project, support users to manually select TXT file upload;

2. After the user submits, the word frequency statistic function is run in the background, and the first 10 items of the statistic result are printed to the screen;

3. Statistical results File Result.txt can be downloaded (this feature is currently only available locally);

4. The user can customize the separator character. If you want to ",", "." Separate then enter ",." To

5. Due to time issues, the lexical range comparison has not yet been achieved.

Code:

  1.Servlet: used to process file uploads and to transmit the word frequency statistics, where the file upload part used apache.commons.

  Configuration of uploaded files

1 //Upload Configuration2     Private Static FinalString upload_directory = "UPLOAD";3 4     5     Private Static Final intMemory_threshold = 1024 * 1024 * 3; 6     Private Static Final intmax_file_size = 1024 * 1024 * 40; 7     Private Static Final intMax_request_size = 1024 * 1024 * 50;

    Uploading files and processing

1         //Configure upload Parameters2Diskfileitemfactory factory =Newdiskfileitemfactory ();3         //set the memory threshold-after which a temporary file is generated and stored in the temp directory4 Factory.setsizethreshold (memory_threshold);5         //Set temporary storage directory6Factory.setrepository (NewFile (System.getproperty ("Java.io.tmpdir")));7 8Servletfileupload upload =Newservletfileupload (factory);9 Ten         //set maximum file upload value One Upload.setfilesizemax (max_file_size); A  -         //Set maximum request value (include file and form data) - Upload.setsizemax (max_request_size); the  -  -String Uploadpath = "D:\\project\\workspace" + File.separator +upload_directory; -  +         //If the directory does not exist, create -File Uploaddir =NewFile (uploadpath); +         if(!uploaddir.exists ()) { A Uploaddir.mkdir (); at         } -          -  -         Try { -             //parse the requested content extract file data -List<fileitem> Formitems =upload.parserequest (request); in  -             if(Formitems! =NULL&& formitems.size () > 0) { to                 //iterate represents single data +String Path = ""; -                  for(Fileitem item:formitems) { the                     if(!Item.isformfield ()) { *String FileName =NewFile (Item.getname ()). GetName (); $String FilePath = Uploadpath + File.separator +FileName;Panax NotoginsengFile StoreFile =NewFile (filePath); -                         //save file to hard disk the Item.write (storefile); +Request.setattribute ("message", "File Upload succeeded!")); APath =FilePath; the}Else{ +                         if(item.getstring () = =NULL) { -                             Newfileproccessing (path); $}Else if(Item.getstring (). IsEmpty ()) { $                             Newfileproccessing (path); -}Else{ -                             NewFileproccessing (Path, 4096, item.getstring ()); the                         } -                     }Wuyi                 } the             } -}Catch(Exception ex) { WuRequest.setattribute ("message", "Error:" +ex.getmessage ()); -}

    Jump page

      Getservletcontext (). Getrequestdispatcher ("/filedownload.jsp"). Forward (request, response);

  2.fileupload.jsp: defines a form for submitting user-uploaded files and custom delimiters. If the user does not fill in the custom delimiter, the character is delimited by default.

1     <formMethod= "POST"Action= "Countweb/uploadservlet"2 enctype= "Multipart/form-data">3Select a file:<inputtype= "File"name= "UploadFile" />(Please select TXT file)<BR/>4         <BR/>5Custom delimiter:<inputtype= "text"name= "Synstr"value="" /> <BR/>6         <BR/> <inputtype= "Submit"value= "Upload" />7     </form>

  3.filedownload.jsp: used to output user upload results and Word frequency statistics results.

1     <Center>2         <H2>${message}</H2>3     </Center>4     <% Stringjuge= (String) Request.getattribute ("message");5     if(!juge.startswith ("Error")) {%>6Statistical results:<BR/>7     <%FileReader FilePrint= NewFileReader ("D:\\project\\workspace\\countweb\\result.txt");8 bufferedreader BR= NewBufferedReader (fileprint);9      for(intI= 0; I< -; I++){%>Ten         <%=Br.readline ()%><BR/> One     <% } A br.close (); - fileprint.close (); -     %> the     <ahref= "D:\Project\workspace\CountWeb\Result.txt">Download the result file</a> -     <%} %> -     <BR/>

Operation Result:

1. File Upload interface:

  

  

2. Upload Results page:

  

3. Custom delimiter ",. \ n" Output:

  

4. File Download:

  

Code Address:

HTTPS Https://coding.net/u/regretless/p/CountWeb/git

SSH [email protected]: Regretless/countweb.git

GIT Git://git.coding.net/regretless/countweb.git

  

  

Java implementation of Word frequency statistics--web migration

Related Article

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.