java - 如何使用java将文件从windows文件夹写入unix文件夹

标签 java windows unix copy filewriter

我需要使用 Java 将文件从 Windows 文件夹复制到 unix 文件夹

我需要一个 FTP 实用程序。

该文件由Java程序处理,并且该文件必须写入Unix文件夹

我有 unix 服务器名称和文件夹名称。

谁能帮我解决一下吗?

最佳答案

你可以用 JSch 来做到这一点

import com.jcraft.jsch.*;
import java.io.*;

public class ScpTo{
public static void main(String[] arg){
FileInputStream fis=null;
try{
    String lfile="file.txt";
    String user="username";
    String host="host";
    String rfile="file1.txt";

  JSch jsch=new JSch();
  Session session=jsch.getSession(user, host, 22);

  java.util.Properties config = new java.util.Properties(); 
  config.put("StrictHostKeyChecking", "no");
  session.setPassword("******");
  session.setConfig(config);
  session.connect();

  boolean ptimestamp = false;
  // exec 'scp -t rfile' remotely
  String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rfile;
  Channel channel=session.openChannel("exec");
  ((ChannelExec)channel).setCommand(command);

  // get I/O streams for remote scp
  OutputStream out=channel.getOutputStream();
  InputStream in=channel.getInputStream();

  channel.connect();

  if(checkAck(in)!=0){
System.exit(0);
  }

  File _lfile = new File(lfile);

  if(ptimestamp){
    command="T "+(_lfile.lastModified()/1000)+" 0";
    // The access time should be sent here,
    // but it is not accessible with JavaAPI ;-<
    command+=(" "+(_lfile.lastModified()/1000)+" 0\n"); 
    out.write(command.getBytes()); out.flush();
    if(checkAck(in)!=0){
  System.exit(0);
    }
  }

  // send "C0644 filesize filename", where filename should not include '/'
  long filesize=_lfile.length();
  command="C0644 "+filesize+" ";
  if(lfile.lastIndexOf('/')>0){
    command+=lfile.substring(lfile.lastIndexOf('/')+1);
  }
  else{
    command+=lfile;
  }
  command+="\n";
  out.write(command.getBytes()); out.flush();
  if(checkAck(in)!=0){
System.exit(0);
  }

  // send a content of lfile
  fis=new FileInputStream(lfile);
  byte[] buf=new byte[1024];
  while(true){
    int len=fis.read(buf, 0, buf.length);
if(len<=0) break;
    out.write(buf, 0, len); //out.flush();
  }
  fis.close();
  fis=null;
  // send '\0'
  buf[0]=0; out.write(buf, 0, 1); out.flush();
  if(checkAck(in)!=0){
System.exit(0);
  }
  out.close();
  System.out.print("Transfer done.");

  channel.disconnect();
  session.disconnect();

  System.exit(0);
}
catch(Exception e){
  System.out.println(e);
  try{if(fis!=null)fis.close();}catch(Exception ee){}
}
}

static int checkAck(InputStream in) throws IOException{
int b=in.read();
// b may be 0 for success,
//          1 for error,
//          2 for fatal error,
//          -1
if(b==0) return b;
if(b==-1) return b;

if(b==1 || b==2){
  StringBuffer sb=new StringBuffer();
  int c;
  do {
c=in.read();
sb.append((char)c);
  }
  while(c!='\n');
  if(b==1){ // error
System.out.print(sb.toString());
  }
  if(b==2){ // fatal error
System.out.print(sb.toString());
  }
}
return b;
 }
}

关于java - 如何使用java将文件从windows文件夹写入unix文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26654307/

相关文章:

php - 实时统计方程

java - 预熔 : Adding edge weights to the GraphView Demo

java - StringEscapeUtils 取消转义除换行符 (\n) 之外的所有内容

Linux:获取空闲空间物理 block 号(空闲空间位图)

Ruby:读取未保存的文本文件

java - Authenticode、SPC 和 Java CodeSign 之间的区别?

linux - 邮件 : Options MUST PRECEDE persons

java - 将 POJO 序列化为 json,并将某些值作为字符串或列表

java - 为 POI 生成的 Excel 文件中的单元格添加边框

python - Anaconda 已安装但无法启动 Navigator