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

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog

博客园 - 三驾马车

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();
}

}