我想使用 Java 将 pdf 发送到客户端 web 服务 url。如何才能做到这一点?
问问题
189 次
1 回答
1
几个简单的步骤。我将在括号之间添加一些术语以进行谷歌搜索。
- 打开 pdf 文件的 FileInputStream。(java文件输入流)
- 告诉服务器您将发送一个文件。
- 使用 byte[] 缓冲区并从输入流中填充它并将其写入服务器。(java读取输入流缓冲区)。您必须告诉服务器即将到来的缓冲区的大小。
这是一些示例代码。
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();
现在,由您来编写它的服务器部分。
于 2012-04-24T16:06:05.863 回答