I need a sample code that copies files over Network to Local File System in Java.
How this is done in Java?
here is code that copies files in the local file system
File fromfile = new File("file");
File tofile = new File("../copiedfile");
tofile.createNewFile();
FileInputStream from = new FileInputStream(fromfile);
FileOutputStream to = new FileOutputStream(tofile);
byte [] buffer = new byte[4096];
int bytesread;
while ((bytesread = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesread);
}
I think, if you want to copy files over network you should send buffer using ObjectOutput and sockets
Related
I am trying to write a program to transfer a file between client and server using java tcp sockets I am using buffer size of 64K but The problem I am facing is that when when the tcp sometimes fail to send the whole 64K it sends the remaing part for example 32K in anther go
There for A garbage data of some Spaces or so is being taken by the buffer at reading side to make 64K complete and thus unnecessary data is making the file useless at receiving side.
Is there any solution to overcome this problem ???
I am using TCP protocol this code is using to send data to client
Server-side code
File transferFile = new File ("Document.txt");
byte [] bytearray = new byte [1024];
int byRead=0;
FileInputStream fin = new FileInputStream(transferFile);
BufferedInputStream bin = new BufferedInputStream(fin);
OutputStream os = socket.getOutputStream();
while(byRead>-1) {
byRead=bin.read(bytearray,0,bytearray.length);
os.write(bytearray,0,bytearray.length);
os.flush();
}
Client-side code
byte [] bytearray = new byte [1024];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("C:\\Users\\NetBeansProjects\\"+filename);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(bytearray,0,bytearray.length);
currentTot = bytesRead; System.out.println("Data is being read ...");
do {
bytesRead = is.read(bytearray, 0, (bytearray.length));
if(bytesRead == 0) continue;
if(bytesRead >= 0) currentTot += bytesRead;
bos.write(bytearray,0,bytearray.length);
} while(bytesRead > -1);
here I tried to skip the loop if the byte is empty by continue; statement but it is not
working.
bos.write(bytearray,0,bytearray.length);
This should be
bos.write(bytearray,0,bytesRead);
The region after 'bytesRead' in the buffer is undisturbed by the read. It isn't 'garbage'. It's just whatever was there before.
use CLIENT Side Code as below to get the total write bytes without garbage
int availableByte = socket.available();
if (availableByte > 0) {
byte[] buffer = new byte[availableByte];
int bytesRead = socketInputStream.read(buffer);
FileOutputStream fileOutputStream = new FileOutputStream(FilePath, true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.write(buffer.toString());
bufferedWriter.close();
}
I'm trying to write inputstream to a file, but it never gets written on disk, I just get the error file doesnt exist. The file I open is a drawable icluded in the project, I would like to save it to sd card. This is what I have so far:
File storagePath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tester");
InputStream inputStream = getResources().openRawResource(R.drawable.test);
OutputStream out = new FileOutputStream(new File(storagePath, "test.png"));
byte buffer[] = new byte[900];
int len;
while ((len = inputStream.read(buf)) > 0)
out.write(buffer, 0, len);
out.close();
inputStream.close();
Your tester directory doesn't exist. Check for it and create it if necessary before opening your FileOutputStream.
A web Service expects a byte[] coming from a zip file.
I have some files in a folder that I zip with Java and then I get the byte[] from this zip file.
Is this necessary or can I create the byte[] straight from the folder?
I think something like this would allow you to do what you want without writing as long as the files are not going to be very big.
String[] sourceFiles = { "C:/file1", "C:/file2" };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(baos);
byte[] buffer = new byte[4096];
for (int i = 0; i < sourceFiles.length; i++)
{
FileInputStream fin = new FileInputStream(sourceFiles[i]);
zout.putNextEntry(new ZipEntry(sourceFiles[i]));
int length;
while ((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
zout.close();
byte[] bytes = baos.toByteArray();
A folder is a collection of files. It is a container. It does not have a byte stream to get in the first place.
On the other hand, a ZIP (or any archive) is a file. The information about the different files is stored within the ZIP file itself
However, you can iterate through the folder contents, cook up a byte array and then use it (you are doing that anyways while creating the ZIP).
This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 7 years ago.
I have used the code here to send an individual file over a socket. However, I need to be able to send multiple files (basically all files in a directory) over the socket and have the client recognize how the separation between files. Frankly, I am at a complete loss for what to do. Any tips would be helpful.
NOTE 1: I need a way to send the files in one continuous stream that the client can segregate into individual files. It cannot rely on individual requests from the client.
NOTE 2: To answer a question I am pretty sure I will get in the comments, no, this is NOT homework.
EDIT it has been suggested that I could send the size of the file before the file itself. How can I do this, as sending a file over the socket is always done in either a predetermined array of bytes, or a single byte individually, rather than the long returned by File.length()
Here is a full implementation:
Sender Side:
String directory = ...;
String hostDomain = ...;
int port = ...;
File[] files = new File(directory).listFiles();
Socket socket = new Socket(InetAddress.getByName(hostDomain), port);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(files.length);
for(File file : files)
{
long length = file.length();
dos.writeLong(length);
String name = file.getName();
dos.writeUTF(name);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int theByte = 0;
while((theByte = bis.read()) != -1) bos.write(theByte);
bis.close();
}
dos.close();
Receiver Side:
String dirPath = ...;
ServerSocket serverSocket = ...;
Socket socket = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
DataInputStream dis = new DataInputStream(bis);
int filesCount = dis.readInt();
File[] files = new File[filesCount];
for(int i = 0; i < filesCount; i++)
{
long fileLength = dis.readLong();
String fileName = dis.readUTF();
files[i] = new File(dirPath + "/" + fileName);
FileOutputStream fos = new FileOutputStream(files[i]);
BufferedOutputStream bos = new BufferedOutputStream(fos);
for(int j = 0; j < fileLength; j++) bos.write(bis.read());
bos.close();
}
dis.close();
I did not test it, but I hope it will work!
You could send the size of the file first before each file, that way the client will know when the current file is over and expect the next (size). This will allow you to use one contiguous stream for all files.
A very simple way to do it would be to send the file length before sending each file so that you can determine the separation between files.
Of course, if the receiving process is Java, you can just send Objects.
You could zip the files on the client side and send this zipped stream to the server.
e.g: http://www.exampledepot.com/egs/java.util.zip/CreateZip.html
with ...
OutputStream output = connection.getOutputStream();
ZipOutputStream out = new ZipOutputStream(output);
Maybe the fastest way is to automatically zip and unzip the files in your directory into one file, see java.util.zip package
I`m hoping can help me out with a file creation/response question.
I know how to create and save a file. I know how to send that file back to the user via a ServletOutputStream.
But what I need is to create a file, without saving it on the disk, and then send that file via the ServletOutputStream.
The code above explains the parts that I have. Any help appreciated. Thanks in Advance.
// This Creates a file
//
String text = "These days run away like horses over the hill";
File file = new File("MyFile.txt");
Writer writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
// Missing link goes here
//
// This sends file to browser
//
InputStream inputStream = null;
inputStream = new FileInputStream("C:\\MyFile.txt");
byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ( (bytesRead = inputStream.read(buffer)) != -1)
baos.write(buffer, 0, bytesRead);
response.setContentType("text/html");
response.addHeader("Content-Disposition", "attachment; filename=Invoice.txt");
byte[] outBuf = baos.toByteArray();
stream = response.getOutputStream();
stream.write(outBuf);
You don't need to save off a file, just use a ByteArray stream, try something like this:
inputStream = new ByteArrayInputStream(text.getBytes());
Or, even simpler, just do:
stream.write(text.getBytes());
As cHao suggests, use text.getBytes("UTF-8") or something similar to specify a charset other than the system default. The list of available charsets is available in the API docs for Charset.