前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java 解析网易邮箱里面的附件地址,获取下载地址

java 解析网易邮箱里面的附件地址,获取下载地址

作者头像
JQ实验室
发布2022-02-14 20:55:47
1.2K0
发布2022-02-14 20:55:47
举报
文章被收录于专栏:实用技术实用技术
代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtils {

    public static String download_html = "http://fs.163.com/fs/display/?p=X-NETEASE-HUGE-ATTACHMENT&file=tAiok--dQS6-DVsCLRBYyI2XOe_l6T4F8OTPVps1U4pLtIVKNGH5Q2XcfF-Cypg6As-rb-qyA0G7EQ5khQc6lQ&title=%25E8%25B0%25AD%25E6%2599%25B6%25E3%2580%258A%25E4%25B9%259D%25E5%2584%25BF%25E3%2580%258B%2520-%2520%25E9%25AD%2594%25E9%25AC%25BC%25E4%25B8%25AD%25E7%259A%2584%25E5%25A4%25A9%25E4%25BD%25BF";
    
    public static void main(String[] args) throws Throwable{
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("D:/h.html")));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        System.out.println(sb.toString());
        br.close();
        Pattern pHead = Pattern.compile("\"http://fs.163.com([^\"]*display[^\"]*)\"");
        Matcher mHead = pHead.matcher(sb.toString());
        System.out.println(mHead.matches());
        HashSet<String> hashSet = new HashSet<>();
         while (mHead.find()) {
            //System.out.println(mHead.group());
             String url = mHead.group().replace("\"", "").replace("&amp;", "&");
             url = URLDecoderString(url);
             hashSet.add(url);
        }
         System.out.println(hashSet.toString());
         hashSet.forEach(k->{
            try {
                downloadNetEasy(k);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
    }

    public static void downloadNetEasy(String url) throws IOException, MalformedURLException {
        InputStream in = new URL(url).openStream();
        //Path temp = Paths.get("D:\\temp.html");
        //Files.copy(in,temp,StandardCopyOption.REPLACE_EXISTING);
        //File file = temp.toFile();
        //System.out.println(file.getAbsolutePath());
        String download_url = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line = null;
        while ((line = br.readLine()) != null) {
            if(line.contains("downloadlink")) {
                System.out.println(line);
                download_url = line.substring(line.indexOf("'") + 1,line.lastIndexOf("'"));
                System.out.println(download_url);
            }
        }
        //getFileName(download_url);
        //in = new URL(download_url).openStream();
        //Path temp = Paths.get("D:\\"+getFileName(download_url));
        //Files.copy(in,temp,StandardCopyOption.REPLACE_EXISTING);
        //File file = temp.toFile();
        //System.out.println(file.getAbsolutePath());
    }
    
    public static String getFileName(String urlStr){
        String fileName = null;
        int bytesum = 0;
        int byteread = 0;
        FileOutputStream fs = null;
        try {
            URL url = new URL(urlStr);
            URLConnection uc = url.openConnection();
            uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36");
            uc.connect();
            if(((HttpURLConnection)uc).getResponseCode()==200) {
                String file = uc.getURL().getFile();
                fileName = file.substring(file.lastIndexOf('/')+1);
            }
            //fileName = URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
            System.out.println("文件名为:" + fileName + "  大小" + (uc.getContentLength()/1024)+"KB");
            InputStream inStream = uc.getInputStream();
            File file = new File("D:/"+fileName);
            fs = new FileOutputStream(file);
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
            System.out.println(file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fs.close();
            } catch (IOException e) {
            }
        }
        return fileName;
    }
    
    public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static String URLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
    
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档