Java实现对文本文件MD5加密并ftp传送到远程主机目(2)

public void setUpload(File upload) {
    this.upload = upload;
    }
public String getUploadContentType() {
    return uploadContentType;
    }

public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    }

public String getUploadFileName() {
    return uploadFileName;
    }

public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }

public String getUploadServiceId() {
    return uploadServiceId;
    }

public void setUploadServiceId(String uploadServiceId) {
    this.uploadServiceId = uploadServiceId;
    }

然后是对当前的文本文件进行MD5加密,生成同名的MD5文件,文件中只有一行加密之后的MD5字符串。

由于通过struts上传的文件是存放在临时目录下,我处理的思路是,先把文件copy到指定的路径下
String datapath = getRealPath()+"upload"+File.separator+UUID.randomUUID()+File.separator;
File newFile=new File(new File(datapath),uploadFileName);
if (!newFile.getParentFile().exists())
    newFile.getParentFile().mkdirs();
FileUtils.copyFile(upload, newFile);

然后是生成MD5同名文件
FileMD5 filemd5=new FileMD5();
String md5str=filemd5.getMD5(newFile);

实现的思路是调用byteToHexString方法得到加密之后MD5的字符串,通过writeFileContent实现把文本写入同名的MD5文件中。FileMD5类的getMD5(File file)方法:
  public  String getMD5(File file) {
    Boolean bool = false;
    FileInputStream fis = null;
    String filename=file.getName();
    String[] newfilepath=filename.split("\\.");
    String filenameTemp = file.getParent()+file.separator+newfilepath[0]+ ".md5";
   
    File md5file = new File(filenameTemp);
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      fis = new FileInputStream(file);
      byte[] buffer = new byte[2048];
      int length = -1;
      long s = System.currentTimeMillis();
      while ((length = fis.read(buffer)) != -1) {
      md.update(buffer, 0, length);
      }
      byte[] b = md.digest();
      String filecontent=byteToHexString(b);
      if (!md5file.exists()) {
      md5file.createNewFile();
      bool = true;
      System.out.println("success create file,the file is "
        + md5file.getName());
      writeFileContent(filenameTemp, filecontent);
      }
      else {
      md5file.delete();
      System.out.println("success delete file,the file is "
        + md5file.getName());
      md5file.createNewFile();
      bool = true;
      System.out.println("success create file,the file is "
        + md5file.getName());
      writeFileContent(filenameTemp, filecontent); 
      }
      return byteToHexString(b);
    } catch (Exception ex) {
      ex.printStackTrace();
      return null;
    } finally {
      try {
      fis.close();
      } catch (IOException ex) {
      ex.printStackTrace();
      }
    }
      }

byteToHexString方法,主要是实现对文本文件的MD5加密,得到加密之后的MD5文件
  private  String byteToHexString(byte[] tmp) {
    String s;
    char str[] = new char[16 * 2];
    int k = 0;
    for (int i = 0; i < 16; i++) { 
      byte byte0 = tmp[i];
      str[k++] = hexdigits[byte0 >>> 4 & 0xf];   
      str[k++] = hexdigits[byte0 & 0xf];
      }
      s = new String(str);
      return s;
      }

writeFileContent方法,实现把文本写入同名的MD5文件中
  public  boolean writeFileContent(String filepath, String newstr) throws IOException {
    Boolean bool = false;
          //String filein = newstr + "\r\n";
    String filein = new String(newstr);
    String temp = "";

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3a19bf98f2872e488ebe40cb9e58ef75.html