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;标签:String,buffer,new,inputStream,static,import,download,pdf,save From: https://www.cnblogs.com/myibm/p/18500212
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();
}
}