要求:把程序迁移到web平台,通过用户上传TXT的方式接收文件。建议(但不强制要求)保留并维护Console版本,有利于测试。

在页面上设置上传的控件,然后在servlet中接受,得到的是一个字节流,然后转化为字符型在原有代码中进行统计。

jsp页面的代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>
<form action="server/CountWordServlet" method="post" enctype="multipart/form-data">
请上传要统计的文件<input type="file" name="sourceFile"/>
<input type="submit" value="上传">
</form>
</td>
</tr>
</table>
</body>
</html>

展示结果的页面如下

<%@page import="com.server.servlet.Word"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%ArrayList<Word> list=(ArrayList<Word>)request.getAttribute("list"); %>
<title>Insert title here</title>
</head>
<body>
<table> <%
if(list!=null&&list.size()!=0){
%>
<tr> <td>单词</td><td>数量</td> </tr>
<%
for(int i=0;i<list.size();i++){
String word=((Word)list.get(i)).getWord();
int num=((Word)list.get(i)).getNum();
%><tr>
<td><%=word%></td>
<td><%=num%></td>
</tr>
<%
}
}else{ %>
<td>此文件没有单词或者文件不存在</td>
<% }
%> </table>
</body>
</html>

servle中的代码如下

public class CountWordServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("utf-8");
ArrayList<Word> list=new ArrayList<>();
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
FileItemIterator iterator=upload.getItemIterator(request);
while(iterator.hasNext()){
InputStream input=iterator.next().openStream(); WordCountFreq wcf=new WordCountFreq();
list=(ArrayList<Word>) wcf.sortAndOutput(input);
request.setAttribute("list", list);
}
} catch (FileUploadException e) {
e.printStackTrace();
}
System.out.println("成功了!");
response.setContentType("text/html;charset=utf-8"); request.getRequestDispatcher("/show.jsp").forward(request, response);
} }

然后将统计过程的关键方法sortAndOutput()展示如下

public List<Word> sortAndOutput(InputStream input) throws IOException {
BufferedInputStream bis=new BufferedInputStream(input);
byte [] buf = new byte[1024];
int len = -1;
String temp = "";
String lastWord = "";
while((len = bis.read(buf)) != -1) {
//将读取到的字节数据转化为字符串打印出来
String str = new String(buf,0,len);
temp = "";
temp += lastWord;
for (int i = 0; i < str.length(); i++) {
temp += str.charAt(i);
}
lastWord = "";
if (Character.isLetter(str.charAt(str.length()-1))) {
int j, t;
for (j = str.length() - 1, t = 0; Character.isLetter(str.charAt(j)); j--, t++);
temp = temp.substring(0, temp.length() - t);
for (int k = j + 1; k < str.length(); k++) {
lastWord += str.charAt(k);
}
}
root = generateCharTree(temp);
}

示例如下

在没做web版本之前,只是传入文件的路径进行处理。改为web版本之后将遇见的一点小困难是要将字节流转化为字符进行处理,经过查询也很快就解决了。

ssh:git@git.coding.net:muziliquan/GUIVersion.git

git:git://git.coding.net/muziliquan/GUIVersion.git

词频统计-------------web版本的更多相关文章

  1. Java实现的词频统计——Web迁移

    本次将原本控制台工程迁移到了web工程上,依旧保留原本控制台的版本. 需求: 1.把程序迁移到web平台,通过用户上传TXT的方式接收文件: 2.在页面上给出链接 (如果有封皮.作者.字数.页数等信息 ...

  2. java词频统计——web版支持

    需求概要: 1.把程序迁移到web平台,通过用户上传TXT的方式接收文件. 2.用户直接输入要统计的文本,服务器返回结果 3.在页面上给出链接 (如果有封皮.作者.字数.页数等信息更佳)或表格,展示经 ...

  3. 词频统计Web工程

    本次将原本控制台工程迁移到了web工程上.. 需求: 1.把程序迁移到web平台,通过用户上传TXT的方式接收文件: 2.在页面上给出链接 (如果有封皮.作者.字数.页数等信息更佳)或表格,展示经典英 ...

  4. 个人项目----词频统计WEB(部分功能)

    需求分析 1.使用web上传txt文件,对上传的txt进行词频统计. 2.将统计后的结果输出到web页面,力求界面优美. 3.在界面上展示所给url的文章词频统计,力求界面优美. 3.将每个单词同四. ...

  5. 词频统计web

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  6. Hadoop之词频统计小实验

    声明:    1)本文由我原创撰写,转载时请注明出处,侵权必究. 2)本小实验工作环境为Ubuntu操作系统,hadoop1-2-1,jdk1.8.0. 3)统计词频工作在单节点的伪分布上,至于真正实 ...

  7. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  8. c#词频统计命令行程序

    这里将用c#写一个关于词频统计的命令行程序. 预计时间分配:输入处理3h.词条排序打印2h.测试3h. 实际时间分配:输入处理1h.词条排序打印2h.测试3h.程序改进优化6h. 下面将讲解程序的完成 ...

  9. java词频统计——改进后的单元测试

    测试项目 博客文章地址:[http://www.cnblogs.com/jx8zjs/p/5862269.html] 工程地址:https://coding.net/u/jx8zjs/p/wordCo ...

随机推荐

  1. Asp.net Mvc4默认权限详细(下)

    前言 菜鸟去重复之Sql的问题还没有得到满意的答案.如果哪位大哥有相关的资料解释,能够分享给我,那就太谢谢了. 以后每发表一篇博文我都会将以前遗留的问题在前言里指出,直到解决为止. 本文主要在于探讨一 ...

  2. Selenium2学习-029-WebUI自动化实战实例-027-判断元素是否存在

    非常简单的源码,敬请各位小主参阅.若有不足之处,敬请大神指正,不胜感激! /** * Verify the element exist or not * * @author Aaron.ffp * @ ...

  3. SQLSERER 中select排序问题

    SELECT  * FROM 表名 ORDER BY PageNO DESC 这种排序会排出这种效果:1, 11,2,20 select             *,              RIG ...

  4. angularJs之模块化

    <!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Cont ...

  5. http://blog.csdn.net/qiutongyeluo/article/details/52468081

    http://blog.csdn.net/qiutongyeluo/article/details/52468081

  6. Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()

    For Class.getResourceAsStream(String name), if the name parameter doesn't start with a "/" ...

  7. shell脚本编程-循环(for、while、until)

    for命令格式:– list参数:迭代中要用的一系列值– 每个迭代中,变量var会包含列表中的当前值– do和done语句之间输入的命令可以是一条或多条标准的bash shell命令   1 2 3 ...

  8. 高性能Linux服务器 第10章 基于Linux服务器的性能分析与优化

    高性能Linux服务器 第10章    基于Linux服务器的性能分析与优化 作为一名Linux系统管理员,最主要的工作是优化系统配置,使应用在系统上以最优的状态运行.但硬件问题.软件问题.网络环境等 ...

  9. jQuery 插件 获取URL参数

    jQuery 获取URL参数的插件 jQuery Url Query String 下载地址:http://plugins.jquery.com/getUrlQueryString.js/   var ...

  10. 在Matlab中编译c/c++代码需要安装mex

    >> mex -setup Welcome to mex -setup. This utility will help you set up a default compiler. For ...