暂时只测试了TXT文件,其他文件可自行测试
下面就直接贴代码了
//上传作品
@RequestMapping("addWorked.html")
public String addWorked(
@RequestParam("file") CommonsMultipartFile files[],//此处见文件上传,input的那么为file
HttpServletRequest request,HttpSession session, ModelMap model) throws IOException {
SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
sdf.applyPattern("yyyyMMddHHmmss");// a为am/pm的标记
Date date = new Date();// 获取当前时间
System.out.println("现在时间:" + sdf.format(date)); // 输出已经格式化的现在时间(24小时制)
// 上传位置
String path ="D:\\SSM_work\\web\\zuoye\\";
File f = new File(path);
if (!f.exists())
f.mkdirs();
for (int i = 0; i < files.length; i++) {
// 获得原始文件名
String fileName = files[i].getOriginalFilename();
System.out.println("原始文件名:" + fileName);
// 新文件名
String newFileName = sdf.format(date)+fileName.substring(fileName.lastIndexOf("."));
if (!files[i].isEmpty()) {
try {
FileOutputStream fos = new FileOutputStream(path
+ newFileName);
InputStream in = files[i].getInputStream();
int b = 0;
while ((b = in.read()) != -1) {
fos.write(b);
}
fos.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("上传文件到:" + path + newFileName);
String workname=request.getParameter("workname");
Map<String,Object> map =new HashMap<String,Object>();
//读取传入文本
StringBuffer myStringBuffer = new StringBuffer();
// 自定义函数读文本 返回一个StringBuffer
readToBuffer(myStringBuffer, path + newFileName);
// StringBuffer转为String显示
String myWork = myStringBuffer.toString();
//存放每两个文件对比的百分比
List<Float> towWorkSimilarityList=new ArrayList<>();
//将所有作业文件查询
List<Object> list = indexService.queryWorkList("IndexMapper.queryWorkList", map);
//判断是否有已上传作业,如果有,遍历并读取对比,如果没有直接将0转化为浮点型赋值
if (list.size()!=0) {
//遍历所有作业
for (Object el : list) {
// 可为文本相对路径,可以文件夹绝对路径
String workpath = "D:\\SSM_work\\web\\zuoye\\" + el;
// StringBuffer用来接收解析完了之后的文本内容
StringBuffer sb = new StringBuffer();
// 自定义readToBuffer函数读文本 返回一个StringBuffer
readToBuffer(sb, workpath);
// StringBuffer转为String显示
String resultString = sb.toString();
//调用getSimilarityRatio方法比较两个文件得到相似度
Float towWorkSimilarity = getSimilarityRatio(resultString, myWork);
//将相似度填入数组
towWorkSimilarityList.add(towWorkSimilarity);
}
}else {
towWorkSimilarityList.add((float)0);
};
map.put("username",session.getAttribute("username"));
map.put("work",newFileName);
map.put("workname",workname);
//强转keepFour后进行*100加上%,作为相似度
map.put("similarity",Collections.max(towWorkSimilarityList)*100+"%");
map.put("id",session.getAttribute("id"));
indexService.insertWork("IndexMapper.insertWork", map);
session.removeAttribute("id");
}
return "redirect:showWork.html";
};
//用来读取文本内容
public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
reader.close();
is.close();
}
/**
* 比较两个字符串的相识度
* 核心算法:用一个二维数组记录每个字符串是否相同,如果相同记为0,不相同记为1,每行每列相同个数累加
* 则数组最后一个数为不相同的总数,从而判断这两个字符的相识度
*
* @param str
* @param target
* @return
*/
private static int compare(String str, String target) {
int d[][]; // 矩阵
int n = str.length();
int m = target.length();
int i; // 遍历str的
int j; // 遍历target的
char ch1; // str的
char ch2; // target的
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
d = new int[n + 1][m + 1];
// 初始化第一列
for (i = 0; i <= n; i++) {
d[i][0] = i;
}
// 初始化第一行
for (j = 0; j <= m; j++) {
d[0][j] = j;
}
for (i = 1; i <= n; i++) {
// 遍历str
ch1 = str.charAt(i - 1);
// 去匹配target
for (j = 1; j <= m; j++) {
ch2 = target.charAt(j - 1);
if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
temp = 0;
} else {
temp = 1;
}
// 左边+1,上边+1, 左上角+temp取最小
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
}
}
return d[n][m];
}
/**
* 获取最小的值
*/
private static int min(int one, int two, int three) {
return (one = one < two ? one : two) < three ? one : three;
}
/**
* 获取两字符串的相似度
*/
public static float getSimilarityRatio(String str, String target) {
int max = Math.max(str.length(), target.length());
return 1 - (float) compare(str, target) / max;
}
如还有疑问,加QQ275375496