- //客户端
- public class JPGClient {
- public static void main(String[] args) throws Exception {
- Socket s = new Socket("LocalHost", 10000);
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- "a.jpg"));
- BufferedOutputStream bufOut = new BufferedOutputStream(
- s.getOutputStream());
- byte[] buf = new byte[1024];
- int len = 0;
- while ((len = bis.read(buf)) != -1) {
- bufOut.write(buf, 0, len);
- }
- s.shutdownOutput();
- BufferedReader bufIn = new BufferedReader(new InputStreamReader(
- s.getInputStream()));
- String str = bufIn.readLine();
- System.out.println(str);
-
- bis.close();
- s.close();
- }
- }
- //服务端
- public class JPGServer {
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(10000);
- Socket s = ss.accept();
- BufferedInputStream bisIn = new BufferedInputStream(s.getInputStream());
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream("b.jpg"));
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = bisIn.read(buf)) != -1){
- bos.write(buf, 0, len);
- }
- PrintWriter out = new PrintWriter(s.getOutputStream());
- out.println("图片上传成功");
- bos.close();
- s.close();
- ss.close();
- }
- }
复制代码 文件上传成功,为什么读不到反馈信息? |