当前位置: 移动技术网 > IT编程>开发语言>Java > java实现文件复制上传操作

java实现文件复制上传操作

2019年07月22日  | 移动技术网IT编程  | 我要评论

利用java复制文件到处都可以用到,这里总结了一个类供大家参考。里面总共有两个方法:

public static boolean copyfile(string srcfilename, string destfilename,boolean overlay); 
public static boolean copydirectory(string srcdirname, string destdirname,boolean overlay) ; 

其中:
srcfilename 待复制的文件名
descfilename  目标文件名
overlay  如果目标文件存在,是否覆盖
如果复制成功返回true,否则返回false

代码:

 import java.io.file; 
import java.io.fileinputstream; 
import java.io.filenotfoundexception; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
 
import javax.swing.joptionpane; 
 
/** 
 * 复制文件或文件夹 
 * 
 * zww 
 */ 
public class copyfileutil { 
 
  private static string message = ""; 
 
  /** 
   * 复制单个文件 
   * 
   * @param srcfilename 
   *      待复制的文件名 
   * @param descfilename 
   *      目标文件名 
   * @param overlay 
   *      如果目标文件存在,是否覆盖 
   * @return 如果复制成功返回true,否则返回false 
   */ 
  public static boolean copyfile(string srcfilename, string destfilename, 
      boolean overlay) { 
    file srcfile = new file(srcfilename); 
 
    // 判断源文件是否存在 
    if (!srcfile.exists()) { 
      message = "源文件:" + srcfilename + "不存在!"; 
      joptionpane.showmessagedialog(null, message); 
      return false; 
    } else if (!srcfile.isfile()) { 
      message = "复制文件失败,源文件:" + srcfilename + "不是一个文件!"; 
      joptionpane.showmessagedialog(null, message); 
      return false; 
    } 
 
    // 判断目标文件是否存在 
    file destfile = new file(destfilename); 
    if (destfile.exists()) { 
      // 如果目标文件存在并允许覆盖 
      if (overlay) { 
        // 删除已经存在的目标文件,无论目标文件是目录还是单个文件 
        new file(destfilename).delete(); 
      } 
    } else { 
      // 如果目标文件所在目录不存在,则创建目录 
      if (!destfile.getparentfile().exists()) { 
        // 目标文件所在目录不存在 
        if (!destfile.getparentfile().mkdirs()) { 
          // 复制文件失败:创建目标文件所在目录失败 
          return false; 
        } 
      } 
    } 
 
    // 复制文件 
    int byteread = 0; // 读取的字节数 
    inputstream in = null; 
    outputstream out = null; 
 
    try { 
      in = new fileinputstream(srcfile); 
      out = new fileoutputstream(destfile); 
      byte[] buffer = new byte[1024]; 
 
      while ((byteread = in.read(buffer)) != -1) { 
        out.write(buffer, 0, byteread); 
      } 
      return true; 
    } catch (filenotfoundexception e) { 
      return false; 
    } catch (ioexception e) { 
      return false; 
    } finally { 
      try { 
        if (out != null) 
          out.close(); 
        if (in != null) 
          in.close(); 
      } catch (ioexception e) { 
        e.printstacktrace(); 
      } 
    } 
  } 
 
  /** 
   * 复制整个目录的内容 
   * 
   * @param srcdirname 
   *      待复制目录的目录名 
   * @param destdirname 
   *      目标目录名 
   * @param overlay 
   *      如果目标目录存在,是否覆盖 
   * @return 如果复制成功返回true,否则返回false 
   */ 
  public static boolean copydirectory(string srcdirname, string destdirname, 
      boolean overlay) { 
    // 判断源目录是否存在 
    file srcdir = new file(srcdirname); 
    if (!srcdir.exists()) { 
      message = "复制目录失败:源目录" + srcdirname + "不存在!"; 
      joptionpane.showmessagedialog(null, message); 
      return false; 
    } else if (!srcdir.isdirectory()) { 
      message = "复制目录失败:" + srcdirname + "不是目录!"; 
      joptionpane.showmessagedialog(null, message); 
      return false; 
    } 
 
    // 如果目标目录名不是以文件分隔符结尾,则加上文件分隔符 
    if (!destdirname.endswith(file.separator)) { 
      destdirname = destdirname + file.separator; 
    } 
    file destdir = new file(destdirname); 
    // 如果目标文件夹存在 
    if (destdir.exists()) { 
      // 如果允许覆盖则删除已存在的目标目录 
      if (overlay) { 
        new file(destdirname).delete(); 
      } else { 
        message = "复制目录失败:目的目录" + destdirname + "已存在!"; 
        joptionpane.showmessagedialog(null, message); 
        return false; 
      } 
    } else { 
      // 创建目的目录 
      system.out.println("目的目录不存在,准备创建。。。"); 
      if (!destdir.mkdirs()) { 
        system.out.println("复制目录失败:创建目的目录失败!"); 
        return false; 
      } 
    } 
 
    boolean flag = true; 
    file[] files = srcdir.listfiles(); 
    for (int i = 0; i < files.length; i++) { 
      // 复制文件 
      if (files[i].isfile()) { 
        flag = copyfileutil.copyfile(files[i].getabsolutepath(), 
            destdirname + files[i].getname(), overlay); 
        if (!flag) 
          break; 
      } else if (files[i].isdirectory()) { 
        flag = copyfileutil.copydirectory(files[i].getabsolutepath(), 
            destdirname + files[i].getname(), overlay); 
        if (!flag) 
          break; 
      } 
    } 
    if (!flag) { 
      message = "复制目录" + srcdirname + "至" + destdirname + "失败!"; 
      joptionpane.showmessagedialog(null, message); 
      return false; 
    } else { 
      return true; 
    } 
  } 
 
  public static void main(string[] args) { 
    string srcdirname = "c:/test/test0/test1"; 
    string destdirname = "c:/ttt"; 
    copyfileutil.copydirectory(srcdirname, destdirname, true); 
  } 
} 

不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):

private static void niotransfercopy(file source, file target) { 
  filechannel in = null; 
  filechannel out = null; 
  fileinputstream instream = null; 
  fileoutputstream outstream = null; 
  try { 
    instream = new fileinputstream(source); 
    outstream = new fileoutputstream(target); 
    in = instream.getchannel(); 
    out = outstream.getchannel(); 
    in.transferto(0, in.size(), out); 
  } catch (ioexception e) { 
    e.printstacktrace(); 
  } finally { 
    close(instream); 
    close(in); 
    close(outstream); 
    close(out); 
  } 
} 

如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):

private static void niobuffercopy(file source, file target) { 
  filechannel in = null; 
  filechannel out = null; 
  fileinputstream instream = null; 
  fileoutputstream outstream = null; 
  try { 
    instream = new fileinputstream(source); 
    outstream = new fileoutputstream(target); 
    in = instream.getchannel(); 
    out = outstream.getchannel(); 
    bytebuffer buffer = bytebuffer.allocate(4096); 
    while (in.read(buffer) != -1) { 
      buffer.flip(); 
      out.write(buffer); 
      buffer.clear(); 
    } 
  } catch (ioexception e) { 
    e.printstacktrace(); 
  } finally { 
    close(instream); 
    close(in); 
    close(outstream); 
    close(out); 
  } 
} 

常用的方法1是:

private static void custombufferbufferedstreamcopy(file source, file target) { 
  inputstream fis = null; 
  outputstream fos = null; 
  try { 
    fis = new bufferedinputstream(new fileinputstream(source)); 
    fos = new bufferedoutputstream(new fileoutputstream(target)); 
    byte[] buf = new byte[4096]; 
    int i; 
    while ((i = fis.read(buf)) != -1) { 
      fos.write(buf, 0, i); 
    } 
  } 
  catch (exception e) { 
    e.printstacktrace(); 
  } finally { 
    close(fis); 
    close(fos); 
  } 
} 

常用的方法2是:

private static void custombufferstreamcopy(file source, file target) { 
  inputstream fis = null; 
  outputstream fos = null; 
  try { 
    fis = new fileinputstream(source); 
    fos = new fileoutputstream(target); 
    byte[] buf = new byte[4096]; 
    int i; 
    while ((i = fis.read(buf)) != -1) { 
      fos.write(buf, 0, i); 
    } 
  } 
  catch (exception e) { 
    e.printstacktrace(); 
  } finally { 
    close(fis); 
    close(fos); 
  } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网