I am using following program to get file from server:
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 12345);
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("E://cy.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
This is creating the file but only a few contents of the image are being copied.
Can someone please explain what is wrong with this code and how to fix it?
You are reading only 1024 bytes from InputStream. You need wrap reading by while loop:
int bytesRead ;
while ((bytesRead = is.read(mybytearray)) != -1) {
bos.write(mybytearray, 0, bytesRead);
}
Try this one.
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
import org.apache.commons.io.IOUtils;
public class Test
{
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 12345);
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("E://cy.jpg");
IOUtils.copy(is, fos);
IOUtils.closeQuietly(fos);
sock.close();
}
}
This code uses IOUtils from Commons IO
Related
For reference, here is the complete error I am getting:
java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at com.genomedownloader.Main.FTPGet(Main.java:245)
at com.genomedownloader.Main.access$400(Main.java:28)
at com.genomedownloader.Main$1.call(Main.java:494)
at com.genomedownloader.Main$1.call(Main.java:468)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1423)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:
I just switched my program from using FTP4J as a Java FTP client to Apache FTPClient. I tweaked my download code so that Apache would work, now I get this exception when I try to unzip the *.gz files the program downloads.
Here is the relevant code:
package com.test;
import org.apache.commons.net.ftp.FTPClient;
import java.io.*;
import java.util.zip.GZIPInputStream;
public class Main {
public static void main(String[] args) throws IOException {
FTPClient client;
client = new FTPClient();
client.connect("ftp.ncbi.nlm.nih.gov");
client.login("anonymous", "abc123");
client.setControlKeepAliveTimeout(300 * 60000);
client.changeWorkingDirectory("/genomes/all/GCF/000/334/875/GCF_000334875.1_ASM33487v1");
client.retrieveFile("GCF_000334875.1_ASM33487v1_genomic.fna.gz", new BufferedOutputStream(new FileOutputStream(new File(System.getProperty("user.dir") + "\\GenomicFNA.gz"))));
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(System.getProperty("user.dir") + "\\GenomicFNA.gz"));
OutputStream out = new FileOutputStream(System.getProperty("user.dir") + "\\GenomicFNA.fsa");
byte[] buf = new byte[1024];
int len;
while ((len = gzipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
gzipInputStream.close();
out.close();
client.logout();
client.disconnect();
}
}
What I can't figure out for the life of me is WHY the code works with FTP4J but then fails with Apache, despite the code using nothing from Apache nor FTP4j libraries... Run the above code with Apache commons net, and it should work. (Work as in give the error at the top)
Change the BufferedOutputStream to separate declaration, and change the FileOutputStream likewise, then flush and close both of them before the GZip declaration. Completed code:
package com.test;
import org.apache.commons.net.ftp.FTPClient;
import java.io.*;
import java.util.zip.GZIPInputStream;
public class Main {
public static void main(String[] args) throws IOException {
BufferedOutputStream streamy;
FileOutputStream stream;
FTPClient client;
client = new FTPClient();
client.connect("ftp.ncbi.nlm.nih.gov");
client.login("anonymous", "abc123");
client.setControlKeepAliveTimeout(300 * 60000);
client.changeWorkingDirectory("/genomes/all/GCF/000/334/875/GCF_000334875.1_ASM33487v1");
client.retrieveFile("GCF_000334875.1_ASM33487v1_genomic.fna.gz", streamy = new BufferedOutputStream(stream = new FileOutputStream(new File(System.getProperty("user.dir") + "\\GenomicFNA.gz"))));
stream.flush();
streamy.flush();
stream.close();
streamy.close();
client.logout();
client.disconnect();
GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(System.getProperty("user.dir") + "\\GenomicFNA.gz"));
OutputStream out = new FileOutputStream(System.getProperty("user.dir") + "\\GenomicFNA.fsa");
byte[] buf = new byte[1024];
int len;
while ((len = gzipInputStream.read(buf)) > 0) {
out.write(buf, 0, len);
}
gzipInputStream.close();
out.close();
}
}
I'm getting error of FileNotFound. Basically, I'm trying to upload file from client to server.
Please, help me with it.
This is client.java class
package ftppackage;
import java.net.*;
import java.io.*;
public class Client {
public static void main (String [] args ) throws IOException {
Socket socket = new Socket("127.0.0.1",15123);
File transferFile = new File ("D:\\AsiaAd.wmv");
byte [] bytearray = new byte [(int)transferFile.length()];
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
bin.read(bytearray,0,bytearray.length);
OutputStream os = socket.getOutputStream();
System.out.println("Sending Files...");
os.write(bytearray,0,bytearray.length);
os.flush();
socket.close();
System.out.println("File transfer complete");
}
}
And this is my server.java class
package ftppackage;
import java.net.*;
import java.io.*;
public class Server {
public static void main (String [] args ) throws IOException {
int filesize=1022386;
int bytesRead;
int currentTot = 0;
ServerSocket serverSocket = new ServerSocket(15123);
Socket socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket);
byte [] bytearray = new byte [filesize];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("E:\\0\\"); // it is creating new file not copying the one from client
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead;
do {
bytesRead = is.read(bytearray, currentTot, (bytearray.length-currentTot));
if(bytesRead >= 0)
currentTot += bytesRead;
} while(bytesRead > -1);
bos.write(bytearray, 0 , currentTot);
bos.flush();
bos.close();
socket.close();
}
}
Plus, guide me how do add progress bar in it with percentage. I read about SwingWorker here but unable to implement it as I'm totally new with threading concepts.
Thank you for considering my questions.
FileNotFoundException is something you will get if you point the File Object to some File which is not existing in that path. it means what ever the file you are trying to upload in not there in the specified path. SO make sure you give a valid path.
I am a newbie and I want to accomplish file transfer from server to client "do something with it" and then send the file back to the server. The most basic code I am using is here:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) throws IOException {
ServerSocket servsock = new ServerSocket(123456);
File myFile = new File("s.pdf");
while (true) {
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
}
}
}
The client module
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
public class Main {
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 123456);
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("s.pdf");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}
Got it from this website: http://www.java2s.com/Code/Java/Network-Protocol/TransferafileviaSocket.htm
I understand how this works but I don't know how to send a file back to the server.
Please help.
I've written a file transfer class in the past, you can use it both in your client and server (by making an instance) and use the methods to send and receive files as much as you want.
import java.io.*;
import java.net.Socket;
public class FileTransferProcessor {
Socket socket;
InputStream is;
FileOutputStream fos;
BufferedOutputStream bos;
int bufferSize;
FileTransferProcessor(Socket client) {
socket = client;
is = null;
fos = null;
bos = null;
bufferSize = 0;
}
void receiveFile(String fileName) {
try {
is = socket.getInputStream();
bufferSize = socket.getReceiveBufferSize();
System.out.println("Buffer size: " + bufferSize);
fos = new FileOutputStream(fileName);
bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[bufferSize];
int count;
while ((count = is.read(bytes)) >= 0) {
bos.write(bytes, 0, count);
}
bos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void sendFile(File file) {
FileInputStream fis;
BufferedInputStream bis;
BufferedOutputStream out;
byte[] buffer = new byte[8192];
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
out = new BufferedOutputStream(socket.getOutputStream());
int count;
while ((count = bis.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
out.close();
fis.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
i would like to transfer file from server to client. before that i want to send file names from specific directory. the file is not transferring as the read is returning -1. can any one correct me where i am going wrong?
My client code goes like this.
import java.io.*;
import java.net.*;
class PC1Client {
public static void main(String args[]) throws UnknownHostException, IOException
{
byte[] aByte = new byte[1];
int bytesRead;
Socket sock = new Socket("localhost",3000);
InputStream is1 = sock.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is1));
String st = br.readLine();
System.out.println(st);
InputStream is = sock.getInputStream();
FileOutputStream fos = null;
fos = new FileOutputStream("F:\\ANI1.TXT");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
System.out.println(bytesRead);
do {
System.out.println("s");
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
bos.flush();
bos.close();
sock.close();
}
}
My server code goes like this.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class PC1Server {
public static void main(String[] args) throws IOException
{
ServerSocket serversocket = new ServerSocket(3000);
try
{
while(true)
{
String str="";
Socket socket = serversocket.accept();
File file = new File("D:\\ani");
for(File fi : file.listFiles() )
{
str=str+fi.getName()+";";
}
PrintWriter outname = new PrintWriter(socket.getOutputStream());
outname.println(str);
outname.flush();
outname.close();
System.out.println("hello der");
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream() );
File myfile = new File("d:/hello.txt");
byte[] mybyte = new byte[(int)myfile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myfile));
bis.read(mybyte, 0, mybyte.length);
out.write(mybyte, 0, mybyte.length);
out.flush();
out.close();
}
}
catch(Exception e)
{
}
}
}
Few notes:
byte[] aByte = new byte[1]; is pointless - it forces you to read the file 1 byte at a time.
InputStream is = sock.getInputStream(); what is the point of opening the same input stream second time?
outname.close(); in your server code is what causes your problem.
Why do you close your output stream just to reopen it again?
i just want to run a sample code for file transfer using sockets , in eclipse! and the client gives me this error:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at Client.main(Client.java:13)
i don't think that it is caused by the other side closing the socket!
here's the code of server and client:
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket servsock = new ServerSocket(1000);
File myFile = new File("s.pdf");
while (true) {
Socket sock = servsock.accept();
byte[] mybytearray = new byte[(int) myFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
}
}
}
public class Client {
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("127.0.0.1", 1000);
byte[] mybytearray = new byte[1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("s.pdf");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}