简体   繁体   English

如何在jsp中使用md5比较两个输入文件?

[英]how to compare two input files using md5 in jsp?

how to compare two txt or pdf files in html or jsp using md5 algorithm? 如何使用md5算法比较html或jsp中的两个txt或pdf文件? please post the code 请发布代码

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form method=post action="nextpage.jsp">
<h3>Select File 1:</h3><input type="file" name="fileName1" id="file1">
<h3>Select File 2:</h3><input type="file" name="fileName2" id="file2">
<br>
<br>
<input type="button" name="Compare" value="Compare">
</form>
</body>
</html>

this is my index.jsp file 这是我的index.jsp文件

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<%= request.getParameter("fileName1") %>
<%= request.getParameter("fileName2") %>
</body>
</html>

this is nextpage.jsp 这是nextpage.jsp

i want to compare these two input files using message digest.. i have a java code of md5 我想使用消息摘要比较这两个输入文件。我有一个md5的Java代码

public String msgDigest(String fname)throws Exception
{
    MessageDigest md = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream(fname);
    byte[] dataBytes = new byte[1024];
    int nread = 0; 
        while ((nread = fis.read(dataBytes)) != -1) 
        {
                md.update(dataBytes, 0, nread);
            };
            byte[] mdbytes = md.digest();
         //convert the byte to hex format method 1
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < mdbytes.length; i++)
        {
                sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            String result= sb.toString();
    return result;
}

how can i compare two files? 如何比较两个文件?

I made a project that check the md5 of two files and show you the result. 我做了一个检查两个文件的md5并显示结果的项目。 here are the codes. 这是代码。

the code for servlet : servlet的代码:

@WebServlet("/upload")
@MultipartConfig
public class Md5ServletProcessor extends HttpServlet {
private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String md5ForFirstFile = getMd5String(request, "file1");
    String md5ForSecondFile = getMd5String(request, "file2");

    response.sendRedirect("result.jsp?file1Md5="+md5ForFirstFile+"&file2Md5="+md5ForSecondFile+"&filesEqual="+md5ForFirstFile.equals(md5ForSecondFile));

    }

  private String getMd5String(HttpServletRequest request, String parameter) throws ServletException, IOException{
    Part filePart = request.getPart(parameter); 
    InputStream is = filePart.getInputStream();
    return DigestUtils.md5Hex(is);
  }
}

jsp main page : jsp主页:

 <%@page contentType="text/html" 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>Java MD5CheckSum</title>
 </head>

 <body> 
    <div>
        <h3> Choose Two Files for MD5CheckSum : </h3>
        <form action="upload" method="post" enctype="multipart/form-data">

        <table>
            <tr>
                <th>File 1:</th>
                <td><input type="file" name="file1" /></td>
            </tr>
            <tr>
                <th>File 2:</th>
                <td> <input type="file" name="file2" /></td>
            </tr>
        </table>
            <input type="submit" value="upload" />
        </form>          
    </div>
 </body>
</html>

and finally the jsp result page: 最后是jsp结果页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>Md5Checksum Result</title>
 </head>

  <body>
   <table>

    <tr>
       <th colspan="2">THE RESULT OF COMPARING TWO FILE IS:</th>
    </tr>
    <tr><th colspan="2"></th></tr>
        <tr>
            <th>File1 Md5:</th>
            <td>${param.file1Md5}</td>
        </tr>

        <tr>
            <th>File2 Md5:</th>
            <td>${param.file2Md5}</td>
        </tr>

        <tr>
            <th>result</th>
            <td><c:if test="${param.filesEqual}">
                    <span style="color: green">These two files are equal</span>
                </c:if> <c:if test="${!param.filesEqual}">
                    <span style="color: red">These two files are not equal</span>
                </c:if></td>
        </tr>

        <tr>
              <th colspan="2"><a href="." >go back</a></th>
        </tr>
   </table>
  </body>
</html>

for downloading the whole eclipse project you can use this link -> link 要下载整个eclipse项目,您可以使用此链接-> 链接

Good Luck ! 祝好运 !

I recommend you to use Commons FileUpload in servlet that processes form input. 我建议您在处理表单输入的servlet中使用Commons FileUpload。 Here is sample tutorial: http://commons.apache.org/proper/commons-fileupload/using.html . 这是示例教程: http : //commons.apache.org/proper/commons-fileupload/using.html Once you are done just dispatch to another jsp. 一旦完成,请分派到另一个jsp。

Is it sufficient for you to go further or do you need code sample? 这足以使您走得更远还是需要代码示例?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM