上传sftp,创建20171024目录,判断目录是否存在,复制文件,判断文件字符集

来源:互联网 发布:贵州 大数据大诚信 编辑:程序博客网 时间:2024/06/04 19:17

上传sftp,创建20171024目录,判断目录是否存在,复制文件,判断文件字符集

public class UpLoadFile {
private static final String GBK = “GBk”;
public static void main(String[] args) {
String sPath = “D://tempPath//20171023014826qbfk”;
Date date = new Date();
String sftpPath = CommonUtil.getPropertiesValue(“sftp.properties”, “tengbang.test”)+”/”+new SimpleDateFormat(“yyyyMMdd”).format(date);
upLoadFile(sPath, sftpPath);
}

public static void upLoadFile(String sPath, String sftpPath) {    /*//2创建文件夹存放影响文件    Date date = new Date();     String path= ConfigUtil.getConfig(Constants.DirPath.COMMON_TEMP) + File.separator+new SimpleDateFormat("yyyyMM/dd").format(date);     //创建文件夹     File file2 = new File(path);    if (!file2.exists() && !file2.isDirectory()) {        file2.mkdirs(); // 没有则创建    }*/    Channel channel = null;    try {        Date date = new Date();        SFTPUtils sftp1 = getSftp();        sftpPath = CommonUtil.getPropertiesValue("sftp.properties", "upload")+"/"+new SimpleDateFormat("yyyyMMdd").format(date);        ChannelSftp sftp = sftp1.getSFTPClient();        //创建路径        String rpath = sftpPath;        try {            createDir(rpath, sftp);        } catch (Exception e) {            e.printStackTrace();            throw new SystemException("创建路径失败:" + rpath);        }        sftp.cd(sftpPath);        File file = new File(sPath);        String path = file.getAbsolutePath();        System.out.println("path"+path);        copyFile(sftp, file, sftp.pwd());    } catch (Exception e) {        e.printStackTrace();    } }/**   * 创建一个文件目录  * @throws SystemException   */   public static void createDir(String createpath, ChannelSftp sftp) throws SystemException {        try {            if (isDirExist(createpath,sftp)) {            }else{                String pathArry[] = createpath.split("/");                for (String path : pathArry) {                    if (path.equals("")) {                        continue;                    }                    if (isDirExist(path,sftp)) {                        sftp.cd(path);                        if (isDirExist(createpath, sftp)) {                        }                    } else {                        // 建立目录                        sftp.mkdir(path);                        // 进入并设置为当前目录                        sftp.cd(path);                    }                }            }            System.out.print(sftp.pwd());            //sftp.cd(createpath);        } catch (SftpException e) {            throw new SystemException("创建路径错误:" + createpath);        }    }   /**   * 判断目录是否存在   */   public static boolean isDirExist(String directory,ChannelSftp sftp) {      boolean isDirExistFlag = false;    try {        SftpATTRS sftpATTRS = sftp.lstat(directory);        isDirExistFlag = true;        return sftpATTRS.isDir();    } catch (Exception e) {        if (e.getMessage().toLowerCase().equals("no such file")) {            isDirExistFlag = false;        }    }    return isDirExistFlag; } private static SFTPUtils getSftp() {    Properties prop = new Properties();    ClassPathResource cp = new ClassPathResource("sftp.properties");    try {        prop.load(cp.getInputStream());    } catch (IOException e) {        e.printStackTrace();    }    String host = prop.getProperty("yibin.host").trim();    int port = Integer.parseInt(prop.getProperty("yibin.port").trim());    String username = prop.getProperty("yibin.username").trim();    String password = prop.getProperty("yibin.password").trim();    return new SFTPUtils(host, port, username, password);}public static void copyFile(ChannelSftp sftp, File file, String pwd) throws IOException {    if (file.isDirectory()) {        File[] list = file.listFiles();        try {            try {                String fileName = file.getName();                sftp.cd(pwd);                System.out.println("正在创建目录:" + sftp.pwd() + "/" + fileName);                sftp.mkdir(fileName);                System.out.println("目录创建成功:" + sftp.pwd() + "/" + fileName);            } catch (Exception e) {            }            pwd = pwd + "/" + file.getName();            try {                sftp.cd(file.getName());            } catch (SftpException e) {                e.printStackTrace();            }        } catch (Exception e) {            e.printStackTrace();        }        for (int i = 0; i < list.length; i++) {            copyFile(sftp, list[i], pwd);        }    } else {        try {            sftp.cd(pwd);        } catch (SftpException e1) {            e1.printStackTrace();        }        System.out.println("正在复制文件:" + file.getAbsolutePath());        InputStream instream = null;        OutputStream outstream = null;        try {            //String FileName = file;;            //查看编码            String charset = get_charset(file);            //String filename = new String (file.getName().getBytes("GBK"),"gb2312");        //  String filename = new String(bytes,"ISO-8859-1");            //String string = new String (filename.getBytes("ISO-8859-1"),"gb2312");            //String string2 = new String (string.getBytes("GBK"),"gb2312");            //String string=new String(new String(file.getBytes(), "ISO-8859-1").getBytes(),"gb2312");            /*System.out.println("string+"+filename);            outstream = sftp.put(fileName);*/            instream = new FileInputStream(file);            byte b[] = new byte[1024];            int n;            try {                while ((n = instream.read(b)) != -1) {                    outstream.write(b, 0, n);                }            } catch (IOException e) {                e.printStackTrace();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                outstream.flush();                outstream.close();                instream.close();            } catch (Exception e2) {                e2.printStackTrace();            }        }    }}private static String get_charset(File file) {     String charset = "GBK";          byte[] first3Bytes = new byte[3];          try {              boolean checked = false;              ;              BufferedInputStream bis = new BufferedInputStream(                      new FileInputStream(file));              bis.mark(0);              int read = bis.read(first3Bytes, 0, 3);              if (read == -1)                  return charset;              if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {                  charset = "UTF-16LE";                  checked = true;              } else if (first3Bytes[0] == (byte) 0xFE                      && first3Bytes[1] == (byte) 0xFF) {                  charset = "UTF-16BE";                  checked = true;              } else if (first3Bytes[0] == (byte) 0xEF                      && first3Bytes[1] == (byte) 0xBB                      && first3Bytes[2] == (byte) 0xBF) {                  charset = "UTF-8";                  checked = true;              }              bis.reset();              if (!checked) {                  // int len = 0;                  int loc = 0;                  while ((read = bis.read()) != -1) {                      loc++;                      if (read >= 0xF0)                          break;                      if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK                          break;                      if (0xC0 <= read && read <= 0xDF) {                          read = bis.read();                          if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)                              // (0x80                              // - 0xBF),也可能在GB编码内                              continue;                          else                              break;                      } else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小                          read = bis.read();                          if (0x80 <= read && read <= 0xBF) {                              read = bis.read();                              if (0x80 <= read && read <= 0xBF) {                                  charset = "UTF-8";                                  break;                              } else                                  break;                          } else                              break;                      }                  }              }              bis.close();          } catch (Exception e) {              e.printStackTrace();          }          return charset;  }

}`

阅读全文
0 0
原创粉丝点击