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 classHtmlUtils {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 voidmain(String[] args) throws Throwable{

BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream("D:/h.html")));

String line= null;

StringBuilder sb= newStringBuilder();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 hashSet = new HashSet<>();while(mHead.find()) {//System.out.println(mHead.group());

String url = mHead.group().replace("\"", "").replace("&", "&");

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 voiddownloadNetEasy(String url) throws IOException, MalformedURLException {

InputStreamin = newURL(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 staticString getFileName(String urlStr){

String fileName= null;int bytesum = 0;int byteread = 0;

FileOutputStream fs= null;try{

URL url= newURL(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= newFileOutputStream(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) {

}

}returnfileName;

}public staticString getURLEncoderString(String str) {

String result= "";if (null ==str) {return "";

}try{

result= java.net.URLEncoder.encode(str, "UTF-8");

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}returnresult;

}public staticString URLDecoderString(String str) {

String result= "";if (null ==str) {return "";

}try{

result= java.net.URLDecoder.decode(str, "UTF-8");

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}returnresult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值