zoukankan      html  css  js  c++  java
  • 如何实现文件的上传

    package upload;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    /*
    * 将c:\a.doc的文件复制到d:\a.doc
    */
    public class Test {
    public static void main(String[] args) throws Exception {
    getInputStream("c://a.doc", "d://a.doc");
    }

    private static void getInputStream(String pathName,String copyName) throws FileNotFoundException, IOException{
    File file = new File(pathName);
    if(!file.exists()){
    throw new RuntimeException("文件不存在");
    }else{
    getCopy(copyName, new BufferedInputStream(new FileInputStream(file)));
    }

    }
    private static void getCopy(String copyName,BufferedInputStream bis) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyName));
    BufferedInputStream biss=bis;
    byte[] b = new byte[biss.available()];
    int len = 0;
    while((len=biss.read(b))!=-1){
    bos.write(b, 0, len);
    }
    bos.close();
    biss.close();
    System.out.println(copyName+"复制成功!");
    }
    }

  • 相关阅读:
    判断质数
    猜拳三局两胜
    输入年月日判断是这一年的哪一天
    switch case
    猜拳
    判断年月日是否正确
    老狼老狼几点了
    判断是否中奖
    平滑部署war包到tomcat-deploy.sh
    只用120行Java代码写一个自己的区块链-3挖矿算法
  • 原文地址:https://www.cnblogs.com/charlas/p/7576639.html
Copyright © 2011-2022 走看看