Java客户端给服务器发送文件

来源:互联网 发布:华大基因与阿里云 编辑:程序博客网 时间:2024/05/06 14:50
public static void sendMsg(String url,int port) throws Exception{
        int size=0,length=0;
        Socket  s = null;
        OutputStream os = null;
        InputStream is = null;
        FileInputStream fis = null;
        try {
             s = new Socket("localhost",port);
            os =s.getOutputStream();
            is = s.getInputStream();
            File sendFile = new File(url);
            fis = new FileInputStream(sendFile);
            byte[] buffer = new byte[1024*5];
            int i=0;
            while((size=is.read(buffer))!=-1){
                System.out.println(size+" ");
                os.write(buffer,0,size);
                os.flush();
                System.out.println("第"+ ++i+"次输出");
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            is.close();
            fis.close();
            os.close();
            s.close();

        }

期中

String url = "//home//test//my//cache//1.odc";

port::对应端口

0 0
原创粉丝点击