• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
山高我为峰
博客园    首页    新随笔    联系   管理    订阅  订阅
用socket方式传输Image和Sound文件
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class MediaServer {
    private static ServerSocket serverSocket;
    private static final int PORT = 12345;

    public static void main(String[] args) {
        System.out.println("Opening port...");
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            System.out.println("Unable to attach to port!");
            System.exit(1);
        }
        do {
            try {
                Socket connection = serverSocket.accept();
                Scanner in = new Scanner(connection.getInputStream());
                ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
                String message = in.nextLine();
                System.out.println(message);
                if (message.equalsIgnoreCase("image")) {
                    sendFile("F://apple.jpg", out);
                }
                if (message.equalsIgnoreCase("sound")) {
                    sendFile("F://Matthew.mp3", out);
                }
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } while (true);
    }

    private static void sendFile(String filePath, ObjectOutputStream out) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(filePath);
        long fileLeng = new File(filePath).length();
        int length = (int) fileLeng;
        byte[] bytes = new byte[(int) length];
        fileInputStream.read(bytes);
        fileInputStream.close();
        out.writeObject(bytes);
        out.flush();
    }

}
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class MediaClient {
    private static InetAddress host;
    private static final int PORT = 12345;

    public static void main(String[] args) {
        try {
            host = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            System.out.println("Host ID not found.");
            System.exit(1);
        }
        try {
            Socket connection = new Socket(host, PORT);
            ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
            PrintWriter out = new PrintWriter(connection.getOutputStream(), true);
            Scanner userIn = new Scanner(System.in);
            System.out.println("Enter request (image/sound):");
            String message = userIn.nextLine();
            while (!message.equalsIgnoreCase("image") && !message.equalsIgnoreCase("sound")) {
                System.out.println("Try again:");
                System.out.println("Enter request (image/sound):");
                message = userIn.nextLine();
            }
            userIn.close();
            out.println(message);
            getFile(in, message);
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void getFile(ObjectInputStream in, String message) throws Exception {
        byte[] byteArray = (byte[]) in.readObject();
        FileOutputStream outputStream;
        if (message.equalsIgnoreCase("image")) {
            outputStream = new FileOutputStream("E://apple.jpg");
        } else {
            outputStream = new FileOutputStream("E://Matthew.mp3");
        }
        outputStream.write(byteArray);
        outputStream.close();
    }
}

 

posted on 2016-04-25 15:19  山高我为峰  阅读(453)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3