<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    夢想飛翔

    自強不息
    posts - 111, comments - 30, trackbacks - 0, articles - 0
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Java實現Socket發送和接收文件的代碼

    Posted on 2009-09-10 20:34 love1563 閱讀(1004) 評論(0)  編輯  收藏
    /** 
     * 發送端 
     */ 
    class Client {  
       
        // 網上抄來的,將 int 轉成字節  
        public static byte[] i2b(int i) {  
            return new byte[]{  
                    (byte) ((i >> 24) & 0xFF),  
                    (byte) ((i >> 16) & 0xFF),  
                    (byte) ((i >> 8) & 0xFF),  
                    (byte) (i & 0xFF)  
            };  
        }  
       
        /** 
         * 發送文件。文件大小不能大于 {@link Integer#MAX_VALUE} 
         * 
         * @param hostname 接收端主機名或 IP 地址 
         * @param port     接收端端口號 
         * @param filepath 文件路徑 
         * 
         * @throws IOException 如果讀取文件或發送失敗 
         */ 
        public void sendFile(String hostname, int port, String filepath) throws IOException {  
            File file = new File(filepath);  
            FileInputStream is = new FileInputStream(filepath);  
       
            Socket socket = new Socket(hostname, port);  
            OutputStream os = socket.getOutputStream();  
       
            try {  
                int length = (int) file.length();  
                System.out.println("發送文件:" + file.getName() + ",長度:" + length);  
       
                // 發送文件名和文件內容  
                writeFileName(file, os);  
                writeFileContent(is, os, length);  
            } finally {  
                os.close();  
                is.close();  
            }  
        }  
       
        // 輸出文件內容  
        private void writeFileContent(InputStream is, OutputStream os, int length) throws IOException {  
            // 輸出文件長度  
            os.write(i2b(length));  
       
            // 輸出文件內容  
            byte[] buffer = new byte[4096];  
            int size;  
            while ((size = is.read(buffer)) != -1) {  
                os.write(buffer, 0, size);  
            }  
        }  
       
        // 輸出文件名  
        private void writeFileName(File file, OutputStream os) throws IOException {  
            byte[] fn_bytes = file.getName().getBytes();  
       
            os.write(i2b(fn_bytes.length));         // 輸出文件名長度  
            os.write(fn_bytes);    // 輸出文件名  
        }  

    本文來自: IT知道網(http://www.itwis.com) 詳細出處參考:http://www.itwis.com/html/java/j2se/20090304/3503_2.html

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 无码天堂va亚洲va在线va| 十八禁在线观看视频播放免费| 四虎影视免费永久在线观看| 国产在线观看麻豆91精品免费 | 中文精品人人永久免费| 亚洲国产精品成人久久| 久久久久国产精品免费免费搜索| 污污免费在线观看| 国产午夜亚洲精品午夜鲁丝片| 91精品导航在线网址免费| 亚洲最大的成网4438| 全免费一级午夜毛片| 国产亚洲欧美在线观看| 亚洲高清国产AV拍精品青青草原| 免费无码精品黄AV电影| 中文字幕永久免费视频| 国产精品亚洲专区在线观看| 久久亚洲国产精品一区二区| 免费毛片网站在线观看| 亚洲免费视频网站| 亚洲综合在线成人一区| 青青久在线视频免费观看| 日韩av无码免费播放| 亚洲JIZZJIZZ妇女| 亚洲短视频在线观看| 中文字幕中韩乱码亚洲大片| 性xxxx视频播放免费| 一级毛片不卡片免费观看| 边摸边吃奶边做爽免费视频99 | 国内一级一级毛片a免费| 久久久久久一品道精品免费看| 边摸边吃奶边做爽免费视频99| 亚洲日本在线免费观看| 日韩精品无码人妻免费视频| 98精品全国免费观看视频| 一个人看的免费高清视频日本| 亚洲三级电影网站| 久久久久亚洲爆乳少妇无| 国产成人无码a区在线观看视频免费 | 亚洲免费电影网站| 国产乱子伦精品免费无码专区 |