惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
Last Week in AI
Last Week in AI
罗磊的独立博客
量子位
Jina AI
Jina AI
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
美团技术团队
雷峰网
雷峰网
爱范儿
爱范儿
S
SegmentFault 最新的问题
小众软件
小众软件
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报

博客园 - 三驾马车

Claude Code 官宣:可以在 IDEA 用了! idea gitee 更新已取消 解决方案 ByteBuffer和ByteBuf区别 Marshalling.getProvidedMarshallerFactory("serial") 参数有那些 ProtobufVarint32FrameDecoder和ProtobufDecoder区别 protobuf 的 Varint 编码规范 netty initChannel ch.pipeline().addLast 先后顺序很重要 ChannelInboundHandlerAdapter 的channelRead和channelReadComplete的区别 Unpooled.buffer()和Unpooled.copiedBuffer区别 ServerBootstrap 和Bootstrap 区别 childhandler 和 handler 区别 ChannelInitializer<SocketChannel> 的作用详解 ChannelHandlerAdapter 和 ChannelInboundHandlerAdapter 的区别 SimpleChannelInboundHandler 中的 messageReceived 和 channelRead0 ChannelHandlerAdapter 与 ChannelInboundHandler 的区别 Application run failed .ParserException: while parsing a block mapping in 'reader' openssl genrsa 自签名ssl证书 上传本地项目到新建git项目
save download pdf
三驾马车 · 2024-10-24 · via 博客园 - 三驾马车
package cn.didisos.sos.fams.controller;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

private static String url ="";
private static String outputDict="/Users/shihw/IdeaProjects/rescue2/didisos-fams-server/";
private static String fileName ="a3.pdf";

public static void savePdf() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (IOException e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();

if (entity != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream inputStream = entity.getContent();
// 保存PDF文件
FileOutputStream fileOutputStream = new FileOutputStream(new File(outputDict + fileName));
byte[] buffer = new byte[1024]; int len;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len); }
fileOutputStream.close();
inputStream.close();}
httpClient.close();

}

public static void downLoadPdf(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
byte[] getData = readInputStream(inputStream);
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
}catch (Exception ex){
System.out.println(ex.getMessage());
}
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}

public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}

public static void main(String[] args) {
try{ downLoadPdf(url, fileName,outputDict);

//savePdf();
}catch (Exception e) {

}
}

public static void main11(String[] args) {
try (InputStream inputStream = new URL(url).openStream();
OutputStream outputStream = new FileOutputStream(outputDict)) {

byte[] buffer = new byte[4096];
int bytesRead;

while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

System.out.println("PDF文件已经被保存至: " + outputDict);

} catch (IOException e) {
e.printStackTrace();
}
}

/*public static void main2(String[] args) throws IOException {
URL url = new URL(url);
URLConnection connection = url.openConnection();
try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
byte[] pdfBytes = out.toByteArray();

*//*PDDocument document = PDDocument.load(pdfBytes);
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
System.out.println(text);

document.close();*//*
}
}*/
}

package cn.didisos.sos.fams.controller;

import cn.didisos.sos.fams.common.util.DateUtil;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TestPdf {

private static String url ="http://fy.hgcyberdata.com:10804/hgmes/steel/common/download/%E6%B5%8B%E8%AF%95-20240730151158.pdf?_t=1722323940&fileTypeId=1731574960262987777";
private static String outputDict="/Users/shihw/IdeaProjects/rescue2/didisos-fams-server/";
private static String fileName ="a3.pdf";

public static void main(String[] args) {
/*try{
downLoadPdf(url, fileName,outputDict);
}catch (Exception e) {
System.out.println("err:" + e.getMessage());
}*/

WeekFields wf = WeekFields.of(Locale.getDefault());
LocalDate locale = LocalDate.now();
int weekNumber = locale.get(wf.weekOfWeekBasedYear());
for(int i=1;i<=5;i++){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,calendar.getFirstDayOfWeek());
calendar.set(Calendar.WEEK_OF_YEAR, weekNumber-i);
Date beginDate = calendar.getTime();
calendar.add(Calendar.DAY_OF_WEEK,6);
Date endDate = calendar.getTime();
//数据库根据开始日期结束日期查询
//select sum(*) from table_日记录表 where 日期>=beginDate and 日期<=endDate;
System.out.println("第" + (weekNumber-i) + "周,开始日期:" + DateUtil.formatDate(beginDate) + ",结束日期:" + DateUtil.formatDate(endDate));
}

}

public static void downLoadPdf(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream inputStream = conn.getInputStream();
byte[] getData = readInputStream(inputStream);
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
}catch (Exception ex){
System.out.println(ex.getMessage());
}
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}

public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}

}