Copy a file from linux to windows.

Hey,

I'm writing a program that has to copy a file from a linux box to a windows server.

The program runs on the linux box.

I have tried to work with java.io.File and java.net.URL.

Booth didn't work on the linux box.

Someone has a working example or can push me in the right direction?

Tx.

[335 byte] By [DriesSa] at [2007-11-26 18:39:56]
# 1

> The program runs on the linux box.

> I have tried to work with java.io.File and

> java.net.URL.

> Booth didn't work on the linux box.

Both didnt work on the linux box? How did you use them? And by the way to send a file from one machine to another u need some ftp service, use sun.net.ftp or apache net apis.

ganesh_renganathana at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...
# 2

The two servers are on the same network.

I developed the program on a windows machine and everything worked fine.

Now the program has to run on a linux box.

Here my two test with file and URL.

String path = \\\\host\\dir;

or

String path = //host/dir;

File file = new File(path);

System.out.println(file.exists());

-

String path = file:////host/dir;

URL url = new URL(path);

URLConnection con = url.openConnection();

InputStream input = con.getInputStream();

input.read();

This code is just an example code.

DriesSa at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...
# 3

did u ever try opening the URL from a browser in a different machine - linux or windows?

file://hostname/dir/filename if you are able to open this file from the browser, then your code should work...is there any exceptions? and when you say your code works fine did u try from windows to windows or windows to linux...please be specific with your exceptions...

ganesh_renganathana at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...
# 4

Code workted find from windows to windows. Now it has to run from Linux to windows.

'file://hostname/dir/' opens up in a browser on a widows machine.

Now we are going to try to solve the problem with samba, so my program writes in locale folder and samba takes care of the rest.

But I was hoping that there was also a java solution.

DriesSa at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...
# 5

You have to use telnet or FTP through java;

This code use jscape;

/*

* @(#)TelnetExample.java

*

* Copyright (c) 2001-2002 JScape

* 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.

* All rights reserved.

*

* This software is the confidential and proprietary information of

* JScape. ("Confidential Information"). You shall not disclose such

* Confidential Information and shall use it only in accordance with

* the terms of the license agreement you entered into with JScape.

*/

import com.jscape.inet.telnet.*;

import java.io.*;

public class TelnetExample extends TelnetAdapter {

private Telnet telnet = null;

private OutputStream output = null;

private static BufferedReader reader = null;

private boolean connected = false;

public TelnetExample(String hostname) throws IOException, TelnetException {

String input = null;

// create new Telnet instance

telnet = new Telnet(hostname);

// register this class as TelnetListener

telnet.addTelnetListener(this);

// establish Telnet connection

telnet.connect();

connected = true;

// get output stream

output = telnet.getOutputStream();

// sends all data entered at console to Telnet server

while ((input = reader.readLine()) != null) {

if (connected) {

((TelnetOutputStream) output).println(input);

} else {

break;

}

}

}

/** Invoked when Telnet socked is connected.

* @see TelnetConnectedEvent

* @see Telnet#connect

*/

public void connected(TelnetConnectedEvent event) {

System.out.println("Connected");

}

/**

* Invoked when Telnet socket is disconnected. Disconnect can

* occur in many circumstances including IOException during socket read/write.

* @see TelnetDisconnectedEvent

* @see Telnet#disconnect

*/

public void disconnected(TelnetDisconnectedEvent event) {

connected = false;

System.out.print("Disconnected. Press enter key to quit.");

}

/**

* Invoked when Telnet server requests that the Telnet client begin performing specified <code>TelnetOption</code>.

* @param event a <code>DoOptionEvent</code>

* @see DoOptionEvent

* @see TelnetOption

*/

public void doOption(DoOptionEvent event) {

// refuse any options requested by Telnet server

telnet.sendWontOption(event.getOption());

}

/**

* Invoked when Telnet server offers to begin performing specified <code>TelnetOption</code>.

* @param event a <code>WillOptionEvent</code>

* @see WillOptionEvent

* @see TelnetOption

*/

public void willOption(WillOptionEvent event) {

// refuse any options offered by Telnet server

telnet.sendDontOption(event.getOption());

}

/**

* Invoked when data is received from Telnet server.

* @param event a <code>TelnetDataReceivedEvent</code>

* @see TelnetDataReceivedEvent

*/

public void dataReceived(TelnetDataReceivedEvent event) {

// print data recevied from Telnet server to console

System.out.print(event.getData());

}

/**

* Main method for launching program

* @param args program arguments

*/

public static void main(String[] args) {

try {

reader = new BufferedReader(new InputStreamReader(System.in));

// prompt user for Telnet server hostname

System.out.print("Enter Telnet server hostname (e.g. 10.0.0.1): ");

String hostname = reader.readLine();

// create new TelnetExample instance

TelnetExample example = new TelnetExample(hostname);

} catch (Exception e) {

e.printStackTrace(System.out);

}

}

}

G_Abubakra at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...
# 6

This also uses JScape

/*

* @(#)FtpExample.java

*

* Copyright (c) 2001-2002 JScape

* 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.

* All rights reserved.

*

* This software is the confidential and proprietary information of

* JScape. ("Confidential Information"). You shall not disclose such

* Confidential Information and shall use it only in accordance with

* the terms of the license agreement you entered into with JScape.

*/

import com.jscape.inet.ftp.*;

import java.io.*;

import java.util.Enumeration;

public class FtpExample extends FtpAdapter {

private String hostname;

private String username;

private String password;

public FtpExample(String hostname, String username, String password) {

this.hostname = hostname;

this.username = username;

this.password = password;

}

// print out directory listing

public void getListing() throws FtpException {

Ftp ftp = new Ftp(hostname,username,password);

//capture Ftp related events

ftp.addFtpListener(this);

ftp.connect();

String results = ftp.getDirListingAsString();

System.out.println(results);

ftp.disconnect();

}

// captures connect event

public void connected(FtpConnectedEvent evt) {

System.out.println("Connected to server: " + evt.getHostname());

}

// captures disconnect event

public void disconnected(FtpDisconnectedEvent evt) {

System.out.println("Disconnected from server: " + evt.getHostname());

}

public static void main(String[] args) {

String hostname;

String username;

String password;

try {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter Ftp hostname (e.g. ftp.yourhost.com): ");

hostname = reader.readLine();

System.out.print("Enter username (e.g. anonymous): ");

username = reader.readLine();

System.out.print("Enter password (e.g. user@here.com): ");

password = reader.readLine();

FtpExample example = new FtpExample(hostname,username,password);

example.getListing();

}

catch(Exception e) {

e.printStackTrace();

}

}

}

G_Abubakra at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...
# 7

This is using Apache Commons net

/*

* PixTelnetConnection.java

*

* Created on 13 January 2007, 11:16

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package telnet;

/**

*

* @author Administrator

*/

import java.io.*;

//import class telnet which made by Apache Project

import org.apache.commons.net.telnet.*;

public class PixTelnetConnection {

private TelnetClient telnet = new TelnetClient();

private InputStream in;

private PrintStream out;

private char prompt = '>';

private StringBuffer stringb = null;

private String param = "";

/** Creates a new instance of TelnetLinux */

public PixTelnetConnection(String server, String password) {

try {

// Connect to the specified server

telnet.connect(server, 23);

// Get input and output stream references

in = telnet.getInputStream();

out = new PrintStream(telnet.getOutputStream());

readUntil("Password: ");

write(password);

// Advance to a prompt

readUntil(prompt + " ");

} catch (Exception e) {

System.out.println("error");

// e.printStackTrace();

}

}

public void en(String password) {

try {

write("en");

readUntil("Password: ");

write(password);

prompt = '#';

readUntil(prompt + " ");

} catch (Exception e) {

System.out.println("error");

//e.printStackTrace();

}

}

public String readUntil(String pattern) {

try {

char lastChar = pattern.charAt(pattern.length() - 1);

StringBuffer sb = new StringBuffer();

boolean found = false;

char ch = (char) in.read();

while (true) {

System.out.print(ch);

sb.append(ch);

if (ch == lastChar) {

if (sb.toString().endsWith(pattern)) {

stringb = sb;

return sb.toString();

}

}

ch = (char) in.read();

}

} catch (Exception e) {

System.out.println("error");

// e.printStackTrace();

}

return null;

}

public void write(String value) {

try {

out.println(value);

out.flush();

System.out.println(value);

} catch (Exception e) {

System.out.println("error");

//e.printStackTrace();

}

}

public String sendCommand(String command) {

try {

write(command);

return readUntil(prompt + " ");

} catch (Exception e) {

System.out.println("error");

//e.printStackTrace();

}

return null;

}

public String getHostname() {

sendCommand("sh run | grep hostname");

param = "";

for (int i = 33; i < (stringb.length() - 9); i++) {

param += stringb.charAt(i);

}

return param;

}

public String getInter0IP() {

param = "";

sendCommand("sh run | grep name ");

for (int i = 58; i < (stringb.length() - 74); i++) {

param += stringb.charAt(i);

}

return param;

}

public String getNameif0() {

param = "";

sendCommand("sh run | grep name");

for (int i = 37; i < (stringb.indexOf("sec")); i++) {

param += stringb.charAt(i);

}

return param;

}

public String getSecif0() {

param = "";

sendCommand("sh run | grep name");

for (int i = stringb.indexOf("ethernet0")+10; i < (stringb.indexOf("sec",20)-1); i++) {

param += stringb.charAt(i);

}

return param;

}

public void disconnect() {

try {

telnet.disconnect();

} catch (Exception e) {

System.out.println("error");

// e.printStackTrace();

}

}

public static void main(String[] args) {

try {

PixTelnetConnection telnet = new PixTelnetConnection("IP", "Username");

telnet.en("");

// PixDataStore db1 = new PixDataStore("db1");

//

// db1.insert(telnet.getHostname(), telnet.getInter0IP(), "notset", "notset");

// System.out.println(telnet.getHostname());

// telnet.stringb=null;

//telnet.stringb="";

//System.out.println(telnet.getInter0IP());

//System.out.println(telnet.getHostname());

//System.out.println(telnet.getInter0IP());

//telnet.sendCommand("sh run | grep param");

// System.out.println(telnet.stringb.length());

System.out.println( "-\n"+telnet.getSecif0());

telnet.disconnect();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Hope you find it helpful

G_Abubakra at 2007-7-9 6:13:58 > top of Java-index,Java Essentials,Java Programming...