zoukankan      html  css  js  c++  java
  • java 解析网易邮箱里面的附件地址,获取下载地址

    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 + "
    ");
            }
            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;
        }
        
    }
  • 相关阅读:
    Razor 常用又容易忘记语法
    游览器 reflow
    正则表达式
    migration to end point routing
    js 翻译 c# 注意事项
    Angular 学习笔记 work with excel (导出 excel)
    html4,5 basic
    IIS 服务器配置
    meta 的用途
    正则表达 常用
  • 原文地址:https://www.cnblogs.com/liangblog/p/13498163.html
Copyright © 2011-2022 走看看