你好,在运行client时提示有运行异常,指向socket.shutdownOutput();这一行,很奇怪的是,我把bos.close();这一行放到末尾一行的时候,又没有异常了,请问这个输出流应该什么时候关闭,用完了不就可以关闭了吗,为什么放末行就不报异常了,好奇怪呀,蟹蟹
```java
package upload;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPFileUploadServer {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("sever in 8888 ing");
Socket socket = serverSocket.accept();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
String destPath = "f:\\tower";
byte[] bytes = StreamUtils.streamToByteArray(bis);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
bos.write(bytes);
bos.close();
System.out.println("save successful");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("收到图片");
//bw.newLine();
bw.flush();
socket.shutdownOutput();
System.out.println("发送完成");
bw.close();
socket.close();
serverSocket.close();
}
}
```java
package upload;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TCPFileUploadClient {
public static void main(String[] args) throws Exception{
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
String filePath = "e:\\tower.jpg";
BufferedInputStream bis = new BufferedInputStream
(new FileInputStream(filePath));
byte[] bytes = StreamUtils.streamToByteArray(bis);
BufferedOutputStream bos = new BufferedOutputStream
(socket.getOutputStream());
bos.write(bytes);
bos.close();
bis.close();
socket.shutdownOutput();
//BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
InputStream inputStream = socket.getInputStream();
String s=StreamUtils.streamToString(inputStream);
System.out.println(s);
inputStream.close();
socket.close();
}
}