• 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();
            }
    
        }
    
    }
  • 相关阅读:
    spring中@value注解需要注意
    mysql创建utf-8字符集数据库
    Access denied for user 'root'@'localhost' (using password:YES) 解决方案[转]
    MySql 5.7.20安装
    Shiro 登录认证源码详解
    为什么说Java匿名内部类是残缺的闭包
    Java中的闭包之实例一
    使用Eclipse的Working Set管理项目
    glibc下载安装
    Struts2 整合 Hibernate 框架
  • 原文地址:https://www.cnblogs.com/zywf/p/4851886.html
Copyright © 2020-2023  润新知