Sapphire
Sapphire

Reputation: 1169

How to send pdfs to a remote server using Java?

I want to send pdfs to a client webservice url using Java. How can this be done?

Upvotes: 0

Views: 337

Answers (1)

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

A few simple steps. I will add some terms between brackets for googling.

  1. Open a FileInputStream for the pdf file. (java file inputstream)
  2. Tell the server you will send a file.
  3. Use a byte[] buffer and fill it from the inputstream and write it to the server. (java read inputstream buffer). You will have to tell the server what the size is of the coming buffer.

Here is some sample code.

InputStream in = new FileInputStream(file);
OuputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
int n;
byte[] b = new byte[1024 * 16];
while ((n = in.read(b)) != -1)
{
    dos.writeByte(1); // tell the server a buffer is coming
    dos.writeInt(n); // tell it the how big the buffer is
    dos.write(b, 0, n); // write the buffer
}
dos.writeByte(0); // tell the server no more buffers are coming.
dos.flush();

Now, it is up to you to write the server part of it.

Upvotes: 1

Related Questions