how to send a complete (.txt,.doc) file in java - java

i am making a client server program in java, a portion of which requires to send
a complete file (.txt or .docx) file from client to server. I am not sure how to do
it, i have tried using this webpage
http://www.rgagnon.com/javadetails/java-0542.html
, but it seems that it don't work correctly
in form + multi-threaded application, is there any other way to send a complete
file from client to server?
Any help/suggestions are appreciated...
regards
usama

In principle you have to read the file on your client application, open an OutputStream to the server, and write the contents of the file to this stream.
On the server you read from the other end of the stream.
Depending on your architecture, you either will use a new socket-pair for the file transfer, or somehow embed it into your existing communication protocol.

It would be better to use a Messaging Application for your purpose. I would recommend using JMS. I personally use ActiveMQ . If this does not suit your need then try out Apache Mina. It would abstract you from the Network Programming.

Related

Store a file inside an object

I have a Java client/server desktop application, where the communication between client and server is based on Sockets, and the messages exchanged between client and server are serialized objects (message objects, that incapsulate requests and responses).
Now I need to make the client able to upload a file from the local computer to the server, but I can't send the File through the buffer, since the Buffer is already used for exchanging the message objects.
Should i open another stream to send the file, or is there any better way to upload a file for my situation?
I need to make the client able to upload a file from the local computer to the server
- Open a Solely Dedicated Connection to the Server for File uploading.
- Use File Transfer Protocol to ease your work, and moreover its quite easy and reliable to use the Apache's common lib for File uploading and downloading....
See this link:
http://commons.apache.org/net/
You really only have two options:
Open another connection dedicated to the file upload and send it through that.
Make a message object representing bits of a file being uploaded, and send the file in chunks via these message objects.
The former seems simpler & cleaner to me, requiring less overhead and less complicated code.
You can keep your solution and pass the file content as an object, for example as a String - use Base64 encoding (or similar) of the content if it contains troublesome characters

Read a remote XML file or copy it to locale (using Java)?

My question is about reading a remote XML file by using Java.
My files are stored in one device that runs Windows CE. I should access to few of these devices several times per day.
Which solution is more efficient considering network constraints, stablishment of a TCP session and data loss: to open and read the file remotelly or get a copy locally to the server and process it then?
Thank you very much.
It seems u want the files to be read from client by server, whereas it most cases its the other way round. In this case you should have some push functionality from the client to server and this can be over HTTP.
Or you can have a Http connection listener running in the client which accepts request from server and sends back the XML file to the server. Essentially its like a server thread running in the client.
Not sure if u running JAVA on Windows CE. Look for solutions in Windows CE HTTP listener.
See if it helps

GWT - Client-Side File Uploads

I have been messing around with GWT uploads lately. I want to have the ability to upload an XML file from the client, and get the contents of that file (display the contents in a TextArea).
From what I have found on the net, it seems I would have to upload the file to the server, and then get the contents of the file. I do not particularly like the idea of allowing file uploads to the server (even if they are only XML). Is there anyway to pull the contents of a file that the client specifies without sending it to the server?
Thanks
Recent (decent?) browsers implement the "HTML5" File API that's quite easy to use in GWT using JSNI.
See also: https://developer.mozilla.org/en/Using_files_from_web_applications
Because of security restrictions you cannot access the file on the client side alone. It has to be sent to the server for processing.

FTP upload via sockets

how can I upload a file with sockets to a ftp server? I am reading something with "STOR" in RFC...It is the same how i transfer a file between two computers?A sample code would be helpfuly...thanks
I donĀ“t advice to use raw sockets to upload FTP files, instead use something like SimpleFTP, it's easier to use and it will save you a lot of time. And since you're learning Java, you should do it on the easiest possible way.
JavaWorld presents a discussion of some FTP client libraries. To begin with, they even teach you how to use the "built-in" Java URLConnection to do FTP with in a simple way. That should get you started!
Instead of uploading a file to a ftp I'd suggest that you write your own server and client software and have them communicate with eachother if you want to learn to use sockets in java.
When you have learned that you can read the RFC for ftp which should tell you how to communicate with the FTP server. You need to learn and understand the ftp protocol in order to construct valid packets with data to send and in order to parse the data you receive from the ftp server.
its usually requests, responses and data sent back and forth.
Filedata itself is just sent in its raw form unless the ftp server requires encryption, but requests and responses can be in any form, binary values, decimal values, strings etc all depending on the protocol and the type of request.. You need to know this to setup a valid communication channel.
I don't know the ftp protocol myself so I'm just demonstrating examples so you'll easier understand what it's all about.
You really do not want to write your own implementation of the FTP protocol - it's not as simple as it might seem at first glance.
I'm using commons ftp , which gives you a nice API to all ftp operations.
If you don't need that much control, you could use URLConnection from the standard java library.
What language are you using?
You can use the FTP functions if you have access to the APIs.

How to transfer files from one computer to another over the network using Java?

I need a simple application, preferably a cross-platform one, that enables sending of files between two computers.
It just need to accept and send the files, and show a progress bar. What applications could I use or how could I write one?
Sending and Receiving Files
The sending and receiving of a file basically breaks down to two simple pieces of code.
Recieving code:
ServerSocket serverSoc = new ServerSocket(LISTENING_PORT);
Socket connection = serverSoc.accept();
// code to read from connection.getInputStream();
Sending code:
File fileToSend;
InputStream fileStream = new BufferedInputStream(fileToSend);
Socket connection = new Socket(CONNECTION_ADDRESS, LISTENING_PORT);
OutputStream out = connection.getOutputStream();
// my method to move data from the file inputstream to the output stream of the socket
copyStream(fileStream, out);
The sending piece of code will be ran on the computer that is sending the code when they want to send a file.
The receiving code needs to be put inside a loop, so that everytime someone wants to connect to the server, the server can handle the request and then go back to waiting on serverSoc.accept().
To allow sending files between both computers, each computer will need to run the server (receiving code) to listen for incoming files, and they will both need to run the sending code when they want to send a file.
Progress Bar
The JProgressBar in Swing is easy enough to use. However, getting it to work properly and show current progress of the file transfer is slightly more difficult.
To get a progress bar to show up on a form only involves dropping it onto a JFrame and perhaps setting setIndeterminate(false) so hat it shows that your program is working.
To implement a progress bar correctly you will need to create your own implementation of a SwingWorker. The Java tutorials have a good example of this in theirlesson in concurrency.
This is a fairly difficult issue on its's own though. I would recommend asking this in it's own question if you need more help with it.
Woof is a cool Python script that might work for you:
http://www.home.unix-ag.org/simon/woof.html
I would strongly consider using FTP. Apache has a FTP client and a server
Edit: spdenne's suggestion of HTTP is also good, especially if everyone has Java 6. If not, you can use something like Tiny Java Web Server.
You can write one by using Socket programming in Java. You would need to write a Server and a Client program. The server would use a ServerSocket to listen for connections, and the Client would use a Socket to connect to that server on the specified port.
Here's a tutorial: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html
Sun's Java 6 includes a light-weight HTTP server API and implementation. You could fairly easily use this to serve your file, using URLConnection to obtain it.
Check out this tutorial, it's a really basic example. You would probably also want to send control headers prior to the actual file being sent, containing the size of the file, filename, etc.
Alternatively, base it on an existing protocol, like this project.
Can you install FTP servers on (one of) your machines ?
If you can, you will just have to use a FTP client (FileZilla for example, which have a progress bar).
Two popular apps are "scp" and "rsync". These are standard on Linux, are generally available on Unix and can be run on Windows under cygwin, although you may be able to find windows-native apps that can do it as well. (PuTTY can serve as an SCP client).
For any sort of pc-to-pc file transfer, you need to have a listener on the destination PC. This can be a daemon app (or Windows system process), or it can be a Unix-style "superserver" that's configured to load and run the actual file-copy app when someone contacts the listening port.
SCP and one of the rsync modes do require that there be some sort of remote login capability. Rsync can also publish resources that it will handle directory. Since the concept of a Windows "remote login" isn't as well-established as it is under Linux, this may be preferable. Plus it limits remote access to defined sources/targets on the destination machine instead of allowing access to any (authorized) part of the filesystem.
To transfer over a network more efficiently. Take a look at this article that explains efficient data transfer through zero copy

Categories