java从接口获取下载地址,然后下载文件并保存到指定目录

1.java代码,结构如下入

java从接口获取下载地址,然后下载文件并保存到指定目录_第1张图片

1.pom.xml文件



    4.0.0

    tcl.down.url
    downurl
    1.0-SNAPSHOT
    
        
    
        net.sf.json-lib
        json-lib
        2.4
        jdk15
    
    
        
            org.apache.httpcomponents
            httpclient
            4.5.5
        

 
    
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    downurl.Iputil
                
                
                    
                        
                            repackage
                        
                    
                
            
        
    


2. 通过此数据建立实体类

作用:定义json封装的bean对象
java从接口获取下载地址,然后下载文件并保存到指定目录_第2张图片

package downurl;


import java.util.List;

public class JsonBean {
    private String status;
    private String msg;
    private DataBean data;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }


    public static class DataBean {

        private List list;


        public List getList() {
            return list;
        }

        public void setList(List list) {
            this.list = list;
        }
    }

    public static class TuplesBean {
        private String activeDate;
        private String downloadUrl;


        public TuplesBean() {
        }


        public String getActiveDate() {
            return activeDate;
        }

        public void setActiveDate(String activeDate) {
            this.activeDate = activeDate;
        }

        public String getDownloadUrl() {
            return downloadUrl;
        }

        public void setDownloadUrl(String downloadUrl) {
            this.downloadUrl = downloadUrl;
        }
    }
}



3.发送post请求类

package downurl;


import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;

public class JsonPost {
    /**
     * 发送post请求
     * @param url  路径
     * @param jsonObject  参数(json类型)
     * @param encoding 编码格式
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String send(String url, JSONObject jsonObject, String encoding) throws ParseException, IOException {
        String body = "";

        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);

        //装填参数
        StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
        s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        //设置参数到请求对象中
        httpPost.setEntity(s);
        System.out.println("请求地址:"+url);
//        System.out.println("请求参数:"+nvps.toString());

        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
//        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        //执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = client.execute(httpPost);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        return body;
    }

    public static void main(String[] args) throws IOException {
//        post参数封装为map(map里面可以存map)
        HashMap params = new HashMap<>();
        params.put("date", "20200630");
        params.put("typeId", "huannet_active_info");
        HashMap map = new HashMap<>();
        map.put("key", "dafdfawf=q8");
        map.put("id", "dsdafa");
        map.put("pageNum", "0");
        map.put("pageSize", "1000");
        map.put("params", params);
        JSONObject jsonObject = JSONObject.fromObject(map);
//        System.out.println(jsonObject);
        String  url = "http://dc-o.web.da/da/dsa/request";
//        System.out.println(send(url, jsonObject, "UTF-8"));
    }
}


4.下载文件并保存到目录

package downurl;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class DownUrl {
    /**
     * @从制定URL下载文件并保存到指定目录
     * @param filePath 文件将要保存的目录
     * @param method 请求方法,包括POST和GET
     * @param url 请求的路径
     * @return
     */

    public static File saveUrlAs(String url,String filePath,String method){
        //System.out.println("fileName---->"+filePath);
        //创建不同的文件夹目录
        File file=new File(filePath);
        //判断文件夹是否存在
        if (!file.exists())
        {
            //如果文件夹不存在,则创建新的的文件夹
            file.mkdirs();
        }
        FileOutputStream fileOut = null;
        HttpURLConnection conn = null;
        InputStream inputStream = null;
        try
        {
            // 建立链接
            URL httpUrl=new URL(url);
            conn=(HttpURLConnection) httpUrl.openConnection();
            //以Post方式提交表单,默认get方式
            conn.setRequestMethod(method);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // post方式不能使用缓存
            conn.setUseCaches(false);
            //连接指定的资源
            conn.connect();
            //获取网络输入流
            inputStream=conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            //判断文件的保存路径后面是否以/结尾
            if (!filePath.endsWith("/")) {
                filePath += "/";
            }
            //写入到文件(注意文件保存路径的后面一定要加上文件的名称)

            fileOut = new FileOutputStream(filePath+url.substring(url.lastIndexOf("/")+1,url.lastIndexOf("?")));
            BufferedOutputStream bos = new BufferedOutputStream(fileOut);

            byte[] buf = new byte[4096];
            int length = bis.read(buf);
            //保存文件
            while(length != -1)
            {
                bos.write(buf, 0, length);
                length = bis.read(buf);
            }
            bos.close();
            bis.close();
            conn.disconnect();
        } catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("抛出异常!!");
        }

        return file;

    }
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        String photoUrl = "http://dasfda.oss-cn-hangzhou.aliyuncs.com/logclean/dafa/user_info2/date%3D2020-06-01/part-00003-07477e29-b6da-40b5-8c09-af5c4067606f-c000.snappy.parquet?Expires=1594915200&OSSAccessKeyId=dafa&Signature=4qATSNRtaW4u3TQsjPb%2B73KzqkM%3D";   //文件URL地址
//        String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));     //为下载的文件命名
        String fileName = "/tupian";
        String filePath = "e:";      //保存目录
        File file = saveUrlAs(photoUrl, filePath + fileName,"GET");
    }
}

5.主类

package downurl;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class Iputil {
    //存储执行日期
    static String date;


    public static void main(String[] args) throws ParseException {
//获取当前执行日期的前一天
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sj = new SimpleDateFormat("yyyyMMdd");
        calendar.add(Calendar.DATE, -1);//当前时间减去一年,即一年前的时间  
        String str = sj.format(calendar.getTime());
        date = str;
//        封装post参数
        HashMap params = new HashMap<>();
        params.put("date", date);
        params.put("typeId", "vgavadva");
        HashMap map = new HashMap<>();
        map.put("key", "fafege=q8");
        map.put("id", "fagfa");
        map.put("pageNum", "0");
        map.put("pageSize", "1000");
        map.put("params", params);
//将map转为json对象
        JSONObject jsonObject = JSONObject.fromObject(map);

        Iputil iputil = new Iputil();
//        执行run方法
        iputil.run(jsonObject);


    }

    public void run(JSONObject params) {
        JsonBean jsonBean = new JsonBean();
        try {
            JSONObject jsonObject = JSONObject.fromObject(params);
            JsonPost jsonpost = new JsonPost();

            System.out.println(jsonObject);
            String url = "http://dc-o.web.leiniao.com/dafadaf/open/request";
//            调用send方法,发送post请求,获取数据
            String urlString = jsonpost.send(url, params, "UTF-8");

            JSONObject json = JSONObject.fromObject(urlString);//大括号object 字符串string 数组list
//            封账json对象数据
            jsonBean.setStatus(json.optString("status"));
            jsonBean.setMsg(json.optString("msg"));
            JSONObject dataObj = json.optJSONObject("data");
            JsonBean.DataBean data = new JsonBean.DataBean();
            JSONArray tuplesArray = dataObj.optJSONArray("result");
            List tuplesBeansList = new ArrayList<>();

            for (int i = 0; i < tuplesArray.size(); i++) {
                JSONObject tuplesObj = tuplesArray.optJSONObject(i);
//                System.out.println(tuplesObj.getString("downloadUrl"));
                if (tuplesObj.toString().contains("part")) {
                    System.out.println(tuplesObj.getString("downloadUrl"));
                    JsonBean.TuplesBean tuplesBean = new JsonBean.TuplesBean();
                    tuplesBean.setActiveDate(tuplesObj.getString("activeDate"));
                    tuplesBean.setDownloadUrl(tuplesObj.getString("downloadUrl"));
                    tuplesBeansList.add(tuplesBean);
                }
            }
            data.setList(tuplesBeansList);
            jsonBean.setData(data);

        } catch (
                IOException e) {
            e.printStackTrace();
        }//get数据

        try {

            DownUrl downurl = new DownUrl();
//            循环调用saveUrlAs方法,将文件下载到指定目录
            for (int i = 0; i < jsonBean.getData().getList().size(); i++) {
                String photoUrl = jsonBean.getData().getList().get(i).getDownloadUrl();  //文件URL地址
//        String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));     //为下载的文件命名
//                System.out.println(photoUrl);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                formatter.setLenient(false);
                Date newDate = formatter.parse(date);
                formatter = new SimpleDateFormat("yyyy-MM-dd");
                String filedir = formatter.format(newDate);

                String fileName = "/lbird/" + filedir;
                String filePath = "/root";      //保存目录
                File file = downurl.saveUrlAs(photoUrl, filePath + fileName, "GET");


            }
        } catch (
                Exception e) {
            e.printStackTrace();
        }

    }
}



2.将打好的jar上传到服务器

1.打包步骤

点击idea的Maven后,点击package。如下图
在这里插入图片描述
java从接口获取下载地址,然后下载文件并保存到指定目录_第3张图片

2.执行打包后的jar

java -jar downurl-1.0-SNAPSHOT.jar

你可能感兴趣的:(java,大数据,hdfs,cloudera)