I am making a program to connect to FTP Server using FTPCLient class of apache commons net API. here is code:
FTPClient client = new FTPClient();
byte[] b = new byte[4];
b[ 0] = new Integer(127).byteValue();
b[ 1] = new Integer(0).byteValue();
b[ 2] = new Integer(0).byteValue();
b[ 3] = new Integer(1).byteValue();
try{
InetAddress address = InetAddress.getByAddress(b);
client.connect(address,22);
}
.....
I get the exception at connect line().
org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
Try with this
FTPClient f = new FTPClient();
f.connect(server);
f.login(username, password);
FTPFile[] files = listFiles(directory);
Note: port 22 is used for SSH,sftp not for ftp
If its sftp then you need to go for commons-vfs
Related
I try to send a java application file to a .Net application (c #) using a socket. Here is what I did Java (server side)
ServerSocket serverSocket = new ServerSocket(1592);
Socket socket = serverSocket.accept();
System.out.println("Connection accepted from " + socket);
PrintWriter out = new PrintWriter(socket.getOutputStream());
File file = new File("C:\\test.txt");
Thread.sleep(2000);
out.println(file.length());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = socket.getOutputStream();
byte[] bytes = new byte[(int) file.length()];
bis.read(bytes, 0, bytes.length);
os.write(bytes, 0, bytes.length);
C#(client)
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(ip, 1592);
using (var stream = tcpClient.GetStream())
using (var output = File.Create("result.txt"))
{
Console.WriteLine("Client connected. Starting to receive the file");
// read the file in chunks of 1KB
var buffer = new byte[1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)//(Exception caught here)
{
output.Write(buffer, 0, bytesRead);
}
I had an exception in the specified line containing the following problem
Additional information: Unable to read data from the transport connection: Une connexion existante a dû être fermée par l’hôte distant.
Please any help , i've been facing the issue since few days , and i could not figure it out .
Thank you in advance
The Client is running, but the service is not available. (stopped, crashed..). Perhaps, the Firewall block the connection
Verify that the firwall not blocking your server
You must debug you code and verify its behavior (exception, etc..)
regards
How to setup Apache Thrift communication between a Java client and a PHP server?
I have PHP codes on server side:
$header('Content-Type', 'application/x-thrift');
$handler = new MyApplicationHandler();
$processor = new \tutorial\MyApplicationProcessor($handler);
$transport = new TFramedTransport(
new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TBinaryProtocol($transport, true, true);
$transport->open();
$processor->process($protocol, $protocol);
$transport->close();
And Java codes on the client side:
THttpClient httpClient =
new THttpClient("http://my.application.com/PhpServer.php");
TTransport transport = new TFramedTransport(httpClient);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
MyApplication.Client client = new MyApplication.Client(protocol);
Boolean result = client.someApi(someData); // <-- will crash here
transport.close();
The client will crash when executing this line:
client.someApi(someData);
Is there something wrong in my codes?
I am trying to send an image in the form of a byte array from a client application to a server. The server code is written in python while the client is written in java. The image is being transferred correctly however the image saved on the server machine is corrupted.
The following is the code for the server.
import socket
import struct
HOST = "192.168.1.100"
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
print('SERVER STARTED RUNNING')
while True:
client, address = s.accept()
buf = ''
while len(buf) < 4:
buf += client.recv(4 - len(buf))
size = struct.unpack('!i', buf)[0]
with open('/home/isaac/Desktop/image.jpg', 'wb') as f:
while size > 0:
data = client.recv(1024)
f.write(data)
size -= len(data)
print('Image Saved')
client.sendall('Image Received')
client.close()
The following is the source code for the java client:
public static void main(String[] args) throws IOException {
byte[] array = extractBytes("/home/isaac/Desktop/cat.jpg");
Socket sock = new Socket(IP_ADDRESS, PORT_NO);
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
System.out.println("Array Length - " + array.length);
dos.writeInt(array.length);
dos.write(array);
BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println(reader.readLine());
}
Hopefully you can help me. I've tried to google to get my answer but no solution has worked so far.
In what sense you get a corrupted image? I've tried it with the following array:
byte[] array = new byte[3000];
for(int i=0; i<array.length; i++) {
array[i] = (byte)('0' + (i % 10));
}
and I get the same array I sent.
Another thing, just to be sure: the file is less than 2Gb, right?
This can help you
import java.nio.file.Files;
File file = new File("picture path");
byte[] array = Files.readAllBytes(file.toPath());
I have a Websphere application that creates/modfies/deletes files as part of the business process.
Now it has to be able to copy files over to a different, archive server. How can I give the Websphere process on Server1 access to Server2/myArchiveDir?
Is there a userId that is associated with Websphere?
(We are on a Windows machine.)
you need one side to be a server and the other to be a client.
Server Connection:
ServerSocket servSocket = new ServerSocket(port);
Socket connect = servSocket.accept();
File file = new File(location);
FileWriter write = new FileWriter(file);
write.write(connect.getInputStream());
file.close();
write.close();
connection.close();
Client Side:
File transferFile = new File(file);
InputStream in = new InputStream(transferFile);
// Create connection
Socket conn = new Socket(address, port);
OutputStream out = conn.getOutputStream();
copyStream(in,out);
in.close();
out.close();
conn.close();
I have this little piece of code below which uploads a file in java, the code functions correctly however it hangs for a long time when opening the output stream.
// open file to upload
InputStream filein = new FileInputStream("/path/to/file.txt");
// connect to server
URL url = new URL("ftp://user:pass#host/dir/file.txt");
URLConnection urlConn = url.openConnection();
urlConn.setDoOutput(true);
// write file
// HANGS FROM HERE
OutputStream ftpout = urlConn.getOutputStream();
// TO HERE for about 22 seconds
byte[] data = new byte[1024];
int len = 0;
while((len = filein.read(data)) > 0) {
ftpout.write(data,0, len);
}
// close file
filein .close();
ftpout.close();
In this example the URLConnection.getOutputStream() method hangs for about 22 seconds before continuing as normal, the file is successfully uploaded. The file is only 4 bytes in this case, just a text file with the word 'test' in it and the code hangs before the upload commences so its not because its taking time to upload the file.
This is only happening when connecting to one server, when I try a different server its as fast I could hope for which leads me to think it is a server configuration issue in which case this question may be more suited to server fault, however if I upload from an FTP client (in my case FileZilla) it works fine so it could be there is something I can do with the code to fix this.
Any ideas?
I have solved the problem by switching to use the Commons Net FTPClient which does not apear to have the same problems which changes the code to this below.
InputStream filein = new FileInputStream(new File("/path/to/file.txt"));
// create url
FTPClient ftp = new FTPClient();
ftp.connect(host);
ftp.login(user, pass);
int reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
return;
}
OutputStream ftpout = ftp.appendFileStream("text.txt");
// write file
byte[] data = new byte[1024];
int len = 0;
while((len = filein.read(data)) > 0) {
ftpout.write(data,0, len);
}
filein.close();
ftpout.close();
ftp.logout();