java 下載網絡中在文件

javap 10年前發布 | 941 次閱讀 Java

    URL url = new URL(Config.local + "/excel/" + name);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();

            // 設置請求信息  
            httpConnection.setRequestProperty("GET", "/down.zip HTTP/1.1");  

            // 設置接受信息  
            httpConnection.setRequestProperty("Accept", "image/gif,image/x-xbitmap,application/msword");  

            // 設置連接信息  
            httpConnection.setRequestProperty("Connection", "Keep-Alive");  

            // 獲得輸入流  
            InputStream input = httpConnection.getInputStream();  

            // 創建隨機文件  
            flist.add(new File(name));  
            RandomAccessFile oSavedFile = new RandomAccessFile(name, "rw");  

            byte[] b = new byte[1024];  
            int nRead;  

            // 從輸入流中讀入字節流,然后寫到文件中  
            while ((nRead = input.read(b, 0, 1024)) > 0) {  
                oSavedFile.write(b, 0, nRead);  
            }  

            input.close();  
            oSavedFile.close();  
            httpConnection.disconnect();  </pre> 


 本文由用戶 javap 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!