io流读取json并下载pdf⽂档并写⼊⽂件
1.⾸先将json⽂件放⼊磁盘中,(需要保证确定是json格式,根据实际需求放⼊响应的⽂件,要保证java能够正常读取)
2.引⼊jar包
<dependencies>
<!-- mvnrepository/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
</dependencies>
3.创建⼯具类
下载pdf
package com.lsp.utils;
import java.io.*;
import java.*;
/
**
* @Author: liusupeng
* @Date: 2021/7/13 19:00
*/
public class DownloadPdf {
/**
* 从⽹络Url中下载⽂件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static void  downLoadByUrl(String urlStr,String fileName,String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(5*1000);
//防⽌屏蔽程序抓取⽽返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输⼊流
InputStream inputStream = InputStream();
//获取⾃⼰数组
byte[] getData = readInputStream(inputStream);
//⽂件保存位置
File saveDir = new File(savePath);
if(!ists()){
saveDir.mkdir();
}
File file = new File(saveDir+ File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}
/**
* 从输⼊流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static  byte[] readInputStream(InputStream inputStream) throws IOException {        byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = ad(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
ByteArray();
}
}
读取json⽂件
package com.lsp.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lsp.Policy;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
connect下载public class JsonTest {
/
/读取json⽂件
public static String readJsonFile(String fileName) {
String jsonStr = "";
try {
File jsonFile = new File(fileName);
FileReader fileReader = new FileReader(jsonFile);
Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"UTF-8");            int ch = 0;
StringBuffer sb = new StringBuffer();
while ((ch = ad()) != -1) {
sb.append((char) ch);
}
fileReader.close();
reader.close();
jsonStr = sb.toString();
return jsonStr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
4.逻辑代码
package com.lsp.mq.io;
import com.alibaba.fastjson.JSONArray;
import com.lsp.Policy;
import com.lsp.utils.DownloadPdf;
import org.apachemons.io.FileUtils;
import javax.print.DocFlavor;
import java.io.*;
import java.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Author: liusupeng
* @Date: 2021/7/13 18:06
*/
public class IoTest {
public static void main(String[] args){
List<Integer> beginInteger =new ArrayList<>();
String dir ="H://data.json";
try {
/
/准备读取json⽂件
File file = new File(dir);
if (!ists()) {
}
String str= adFileToString(file, "UTF-8");
//转成list集合,这⾥根据实际需求去类型转换
List<Map<String,Object>> parse= (List) JSONArray.parse(str);
//输出下数量看下是否读取的正确数据
System.out.println(parse.size());
parse.forEach(e->{
/
/创建file对象
File file1 = new File("H://gds//"+e.get("title_of_policy_paper"));
//创建⽂件夹
file1.mkdir();
try {
//开始下载
DownloadPdf.downLoadByUrl((String) e.get("policy_text"),e.get("title_of_policy_paper")+".pdf",String());                } catch (IOException ioException) {
ioException.printStackTrace();
System.out.("id"));
}
//计数器
beginInteger.add((Integer) e.get("id"));
//在判断下是否存在集合
List<Map<String,Object>> files = (List) e.get("files");
if(files!=null&&files.size()>0){
//和上⾯操作⼀致在⽂件夹⾥下载⽂件
files.forEach(f->{
File file2 = new File("H://gds//"+e.get("title_of_policy_paper")+"//"+f.get("file_name"));
file2.mkdirs();
try {
DownloadPdf.downLoadByUrl((String) f.get("url"),f.get("file_name")+".pdf",String());
} catch (IOException ioException) {
ioException.printStackTrace();
}
});
}
});
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("shuliang:"+beginInteger.size());
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。