简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM