读取文件 匹配小括号内容 下载图片 替换文本

package;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName: Test
 * @Package: 
 * @Description:
 * @Datetime: 2020/6/9   11:14
 * @Author: 
 */
public class Test {
    /**
     * 读取文件每一行
     * @param pathname
     * @return
     * @throws IOException
     */
    public static ArrayList readFromTextFile(String pathname) throws IOException {
        ArrayList strArray = new ArrayList();
        File filename = new File(pathname);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(filename));
        BufferedReader br = new BufferedReader(reader);
        String line = "";
        line = br.readLine();
        while(line != null) {
            strArray.add(line);
            line = br.readLine();
        }
        return strArray;
    }
    /**
     * 替换文本文件中的 非法字符串
     * @param path
     * @throws IOException
     */
    public static void replacTextContent(String path,ArrayList strArrayUrls) throws IOException{
        for (String s : strArrayUrls) {
            if(s.length()>0){
                //原有的内容
                String srcStr;
                System.out.println(s);
                try {
                    srcStr = s.substring(0,s.lastIndexOf("/"));
                    //要替换的内容
                    String replaceStr = "/images";
                    // 读
                    File file = new File(path);
                    FileReader in = new FileReader(file);
                    BufferedReader bufIn = new BufferedReader(in);
                    // 内存流, 作为临时流
                    CharArrayWriter  tempStream = new CharArrayWriter();
                    // 替换
                    String line = null;
                    while ( (line = bufIn.readLine()) != null) {
                        // 替换每行中, 符合条件的字符串
                        line = line.replaceAll(srcStr, replaceStr);
                        // 将该行写入内存
                        tempStream.write(line);
                        // 添加换行符
                        tempStream.append(System.getProperty("line.separator"));
                    }
                    // 关闭 输入流
                    bufIn.close();
                    // 将内存中的流 写入 文件
                    FileWriter out = new FileWriter(file);
                    tempStream.writeTo(out);
                    out.close();
                    System.out.println("====path:"+path);
                }catch (Exception e){
                    e.getMessage();
                    continue;
                }
            }
        }
    }
    /**
     * 获取某个文件夹下的所有文件
     *
     * @param path 文件夹的路径
     * @return
     */
    private static ArrayList getFile(String path) {
        File file = new File(path);
        File[] array = file.listFiles();
        ArrayList strArray = new ArrayList();
        for (int i = 0; i < array.length; i++) {
            if (array[i].isFile()) {
                // only take file name
                //System.out.println("^^^^^" + array[i].getName());
                // take file path and name
                //System.out.println("#####" + array[i]);
                // take file path and name
                //System.out.println("*****" + array[i].getPath());
                strArray.add(array[i].toString());
            } else if (array[i].isDirectory()) {
                getFile(array[i].getPath());
            }
        }
        return strArray;
    }

    /**
     * 截取括号内的图片地址
     * @param gSQL
     * @return
     */
    private static String getUrl(String gSQL){
        String quStr=gSQL.substring(gSQL.indexOf("https"),gSQL.indexOf("#"));
        System.out.println(quStr);
        if(StringUtils.isEmpty(quStr)){
            return "";
        }
        return quStr;
    }
    /**
     * @从制定URL下载文件并保存到指定目录
     * @param filePath 文件将要保存的目录
     * @param method 请求方法,包括POST和GET
     * @param url 请求的路径
     * @return
     */

    public static File saveUrlAs(String url,String filePath,String fileName ,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+fileName);
            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;

    }
    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }
    public static void main(String[] args) throws IOException {
        String pathname = "D:\\360MoveData\\Users\\Administrator\\Desktop\\output";
        ArrayList strArrayFilePaths;
        strArrayFilePaths = getFile(pathname);
        ArrayList strArray;
        ArrayList strArrayUrls;
        int num = 0;
        String url;
        //文件列表文件路径f
        for (String f : strArrayFilePaths){
            String s1 = readFileContent(f);
            List vars = new ArrayList<>();
            List list = parse(s1, vars);
            strArrayUrls = new ArrayList<>();
            /*strArray = readFromTextFile(f);*/
            //读取到图片链接
            for (String s : list) {
                //![] ![image.png] 目前就发现这两种
                if(s.contains("https:")){
                    /*url = getUrl(s);*/
                    //放入图片地址集合
                    strArrayUrls.add(s);
                    num=num+1;
                }
            }

            //下载图片
            downloadPictures(strArrayUrls,f);
            //转换图片前缀为本地路路径或者私服oss
            /*replacTextContent(f,strArrayUrls);*/
        }
        System.out.println("图片总个数"+num);
    }
    /**
     * 下载图片
     * @param strArrayUrls
     */
    public static void downloadPictures(ArrayList strArrayUrls,String fileAddress) {
        for (String u : strArrayUrls){
            //文件URL地址
            String photoUrl = u;
            //为下载的文件命名
            try {
                //System.out.println("正常文件地址"+photoUrl);
                String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
                String str1=fileName.substring(1, fileName.indexOf("#"));
                //保存目录
                String filePath = "D:/360MoveData/Users/Administrator/Desktop/gitbook图片";
                File file = saveUrlAs(photoUrl, filePath,str1,"GET");
            }catch (Exception e){
                System.err.printf("下载"+fileAddress+"中文件出错");
                System.out.println("图片地址"+photoUrl);
                e.getMessage();
                continue;
            }
        }
    }

    /**
     * 生成md目录
     * @param args
     * @throws IOException
     */
    public static void main2(String[] args) throws IOException {
        String s = readFileContent("D:\\360MoveData\\Users\\Administrator\\Desktop\\目录.txt");
        JSONObject jo = JSONObject.parseObject(new String(s));
        JSONArray jsonArray = null;
        jsonArray = jo.getJSONArray("data");
        File f=new File("D:\\360MoveData\\Users\\Administrator\\Desktop\\SUMMARY.md");
        FileOutputStream fos1=new FileOutputStream(f);
        OutputStreamWriter dos1=new OutputStreamWriter(fos1);
        for (int i = 0; i  parse(String exp, List vars) {
        final Pattern BRACED_REDEX = Pattern.compile("\\(([^()]*)\\)");
        for (;;) {
            Matcher m = BRACED_REDEX.matcher(exp);
            if (!m.find()) {
                break;
            }
            String value = m.group(1);
            String var = "@" + vars.size();
            vars.add(value);
            StringBuffer sb = new StringBuffer();
            m.appendReplacement(sb, var);
            m.appendTail(sb);
            exp = sb.toString();
        }
        // Add last unreduced expr too.
        vars.add(exp);
        return vars;
    }
}

你可能感兴趣的:(读取文件 匹配小括号内容 下载图片 替换文本)