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();  

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10262.html

(0)
上一篇 2021年7月19日 10:09
下一篇 2021年7月19日 10:09

相关推荐

发表回复

登录后才能评论