2014-03-05 34 views
0

如何在html或jsp中使用md5算法比較兩個txt或pdf文件?請張貼代碼如何在jsp中使用md5比較兩個輸入文件?

<%@ 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> 

這是我的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> 

這是NextPage.jsp上

我想用消息摘要來比較這兩個輸入文件.. 我有java代碼md5

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; 
} 

如何比較兩個文件?

+0

向我們展示'nextpage.jsp'。 – 2014-03-05 13:44:57

+0

我添加了nextpage.jsp來使用servlet.i也不知道servlet。所以留下了想法 – user3382504

回答

0

我建議您在處理表單輸入的servlet中使用Commons FileUpload。這裏是示例教程:http://commons.apache.org/proper/commons-fileupload/using.html。一旦你完成,只需發送到另一個jsp。

您是否足夠走得更遠或您是否需要代碼示例?

+0

非常感謝你的迴應。 – user3382504

+0

不客氣。看看http://stackoverflow.com/tour,這樣你就知道下一步該做什麼了:-) –

1

我做了一個項目,檢查兩個文件的md5並顯示結果。這裏是代碼。

的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主頁:

<%@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> 

終於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> 

下載,你可以使用整個Eclipse項目此鏈接 - >link

祝你好運!