我正在尝试编写一个非常简单的应用程序,它将使用 PUT 请求通过 HTTP 将视频上传到我的一个服务器。这是我的代码的样子:
socket = new Socket();
socket.connect(new InetSocketAddress(ticket.getHost(), 80));
outputStream = socket.getOutputStream();
// begin forming the request
outputStream.write(("PUT " + ticket.getEndpoint() + " HTTP/1.1\r\n" +
"Host: " + ticket.getHost() + "\r\n" +
"Content-Length: " + getStreamFile().length() + "\r\n" +
"Content-Type: video/mp4\r\n\r\n").getBytes());
// now, let's write us some binary data
fileInputStream = new FileInputStream(getStreamFile());
inputStream = new BufferedInputStream(fileInputStream);
byte[] fileBytes = new byte[1024];
int bytesRead = 0;
int totalBytesRead = 0;
while ((bytesRead = inputStream.read(fileBytes)) > 0) {
outputStream.write(fileBytes, 0, bytesRead);
totalBytesRead += bytesRead;
notifyListenersOnProgress((double)totalBytesRead / (double)getStreamFile().length());
}
很简单,对吧?直到我在大约两个循环后看到这个错误:
java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.6.0_23]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) ~[na:1.6.0_23]
at java.net.SocketOutputStream.write(SocketOutputStream.java:141) ~[na:1.6.0_23]
我在这里做错了什么?