0

I need to copy a file from Windows folder to unix folder using Java

I need a FTP utility.

The file is processed by a Java program and the file has to be written to Unix folder

I have the unix server name and folder name.

Can anyone please help me how to do it?

5
  • Is this file stored on server? Commented Oct 30, 2014 at 13:30
  • First, pick a protocol that your server supports. Then find a library in Java that implements that protocol. Then write some code to invoke that library to transfer your file(s) over said protocol. Commented Oct 30, 2014 at 13:31
  • The file is processed by a Java program and the file has to be written to Unix folder Commented Oct 30, 2014 at 13:32
  • I need a FTP utility to copy the file from source to destination Commented Oct 30, 2014 at 13:33
  • Yes the file is stored on the server Vighanesh Gursale Commented Oct 30, 2014 at 13:35

2 Answers 2

1

You can do it with 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;
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like you need a Java FTP client like the one provided by Apache Commons Net, which also provides an example. There is also a complete Java FTP file upload tutorial and example, which should cover your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.