首页 > 其他分享 >save download pdf

save download pdf

时间:2024-10-24 18:43:33浏览次数:1  
标签:String buffer new inputStream static import download pdf save

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


}

标签:String,buffer,new,inputStream,static,import,download,pdf,save
From: https://www.cnblogs.com/myibm/p/18500212

相关文章

  • 如何使用格式工厂将 PDF 转换为 Word 文档
    格式工厂是一款多功能的多媒体文件转换工具,它不仅可以转换音频和视频文件,还可以处理文档格式之间的转换。下面是一篇详细的教程,简鹿办公教你如何使用格式工厂将PDF文档转换为Word(.docx)格式。需要注意的是,如果PDF文件是由扫描件组成的图片,则可能无法直接转换为可编辑的文本......
  • object references an unsaved transient instance - save the transient instance be
    org.hibernate.TransientPropertyValueException:objectreferencesanunsavedtransientinstance-savethetransientinstancebeforeflushing:com.jms.cfc.product.domain.ProductMaintainHistory.product->com.One;nestedexceptionisjava.lang.IllegalSt......
  • 如何给pdf文档加密码保护?(2024全新)6个靠谱pdf加密方法分享
    如何给pdf文档加密码保护?日常办公生活中,我们经常需要使用到pdf文件。作为一个打工人,我们经常需要进行文件传输交流,当你将PDF文件分享到一个群或者人数较多的平台时,为了不让其他人随意查看文件,和保护文件隐私信息不被泄露,我们会选择给文件进行加密保护。那么要如何给PDF文件进行......
  • edge下默认pdf图标的修复办法
    系统默认是edge打开pdf,最近pdf图标确换了,默认程序选edge也不行。修复pdf图标办法:打开注册表,找到计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\MSEdgePDF检查DefaultIcon是不是C:\ProgramFiles(x86)\Microsoft\Edge\Application\msedge.exe,11默认应该是11,不知道为什么,我的值改成1......
  • python pdf 转图片
    1.需要安装requests,PyMuPDF依赖pipinstallrequests,PyMuPDF。可以通过定义的缩放因子和旋转因子去缩放图片和旋转。#!/usr/bin/envpython3#-*-coding:utf-8-*-importdatetimeimportosimportrequestsimportfitz#fitz就是pipinstallPyMuPDFheaders......
  • 单月30k+ Downloads!一款头部Embedding开源模型
    在数字化转型的浪潮中,文本数据的处理和分析成为了各行各业关注的焦点。如何将人类阅读的文本转换为机器可理解的形式,并且能够准确地召回和提取这些转换结果,成为了提升我们工作效率和体验的关键。无论是从社交媒体中提取情感倾向,还是对大量文档进行内容相似性分析,或是在复杂的对话......
  • Excel批量生成word文档,按模板批量生成word,批量生成pdf
    Excel批量生成word文档软件主页:http://6laohu.com下载地址 按照Word模板从Excel逐行取数据填空式批量生成Word与PDF文档,免安装下载即用,防泄密软件,可离线断网使用比如:我要生成多分销售合同,我准备一个word模板,把合同中的[公司名]、[价格]、[法人]、[日期]都用中括号[]括起来......
  • 如何在 Mac 和 Windows 上恢复未保存或删除的 PDF
    文章提供了在Mac和Windows上恢复已删除或未保存PDF文件的方法。用户可以检查垃圾箱/回收站,利用AdobeAcrobat的自动保存功能,或者使用数据恢复软件如奇客数据恢复。在Mac上,TimeMachine备份可以用来恢复文件;在Windows上,文件历史记录提供类似功能。摘要由CSDN通过智能技术生成Ado......
  • IDM下载器 (Internet Download Manager) v6.42.2 中文免激活绿色版
    InternetDownloadManager(IDM下载器)是一款先进的下载工具,可以提升您的下载速度高达5倍,支持续传,IDM可以让用户自动下载某些类型的文件,它可将文件划分为多个下载点以更快速度下载,并列出最近的下载,方便访问文件。相对于其它下载管理工具,它具有独特的动态档案分区技术。......
  • PDF Shaper Professional v13.5 中文破解版
    PDFShaperProfessionalv13.5中文破解版https://www.aihao.cc/thread-93682-1-1.html新版变化Releasenotes-PDFShaperhttps://www.pdfshaper.com/release-notes.html•批量提取PDF中的文本、图像等内容•合并多个PDF文件为一个PDF即PDF合并功能•加密和解密PDF文......