zoukankan      html  css  js  c++  java
  • GET方式,获取服务器文件

    package com.http.get;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.sql.Savepoint;
    import java.text.DateFormat;
    import java.util.Date;
    
    public class HttpUtils {
        private static String URL_PATH="http://www.dazhongtry.com/TryPic/image/20150928/20150928142189698969.jpg";
        
    
        public HttpUtils() {
            // TODO Auto-generated constructor stub
        }
        
        public static void saveImagestodisk() throws IOException
        {
            InputStream inputStream=getInputStream();
            byte[] data=new byte[1024];
            int len=0;
            String ext=URL_PATH.substring(URL_PATH.lastIndexOf(".")).toLowerCase();
            
            
            Date date=new Date();
            
            long lSysTime1 = date.getTime() ;
            FileOutputStream fileoutputstream=new FileOutputStream("D:\"+lSysTime1+ext);
            while((len=inputStream.read(data))!=-1)
            {
                fileoutputstream.write(data,0,len);
                
            }
            fileoutputstream.close();
            inputStream.close();
            
        }
        
        public static InputStream getInputStream() throws IOException{
            InputStream inputStream=null;
            HttpURLConnection httpurlconn=null;
            try {
                URL url=new URL(URL_PATH);
                if(url!=null)
                {
                httpurlconn=(HttpURLConnection) url.openConnection();
                //设置连接超时时间
                httpurlconn.setConnectTimeout(3000);
                //表示使用GET方式请求
                httpurlconn.setRequestMethod("GET");
                int responsecode=httpurlconn.getResponseCode();
                if(responsecode==200)
                {
                    //从服务返回一个输入流
                    inputStream=httpurlconn.getInputStream();
                }
                }
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return inputStream;
            
            
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            try {
                saveImagestodisk();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
  • 相关阅读:
    【转】Win7下VS2010中配置Opencv2.4.4的方法(32位和64位都有效)(亲测成功)
    【转】vs2008中leptonica-1.68安装配置
    python在Mac上做数据分析
    javascript在浏览器中调用sqlserver数据
    浏览器console中加入jquery方便调试
    2008 iis7.5 + php7 + mysql + SSL
    SSL基础知识
    iis7配置php7
    ubuntu 16.04下手动安装apache2php7.0mysqlphpmyadminftp等环境
    ubuntu 14.04下的rails开发环境
  • 原文地址:https://www.cnblogs.com/zywf/p/4851886.html
Copyright © 2011-2022 走看看