/**
* @param
* @param response
* @功能描述 单条公告下载
*/
@ApiOperation("单条公告下载")
@Log(title = "单条公告下载", businessType = BusinessType.OTHER)
@PostMapping("/download")
public void download(String remoteFilePath, HttpServletResponse response) {
log.info(" list list ");
//Pdf2HtmlUtils.pdf2html("D:\\pdf2htmlEX-v1.0\\pdf2htmlEX.exe","D:\\pdf2htmlEX-v1.0\\PDF\\test2.pdf","D:\\pdf2htmlEX-v1.0\\HTML","my22.html");
URL url = null;
try {
url = new URL(remoteFilePath);
URLConnection conn = null;
conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
response.reset();
response.setContentType(conn.getContentType());
String filename="filename1.pdf";
if (false) {
// 在线打开方式 文件名应该编码成UTF-8
response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(filename, "UTF-8"));
} else {
//纯下载方式 文件名应该编码成UTF-8
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
}
byte[] buffer = new byte[1024];
int len;
OutputStream outputStream = response.getOutputStream();
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 下载zip 包
* @param
* @param
* @param response
* @throws IOException
*/
@ApiOperation("多选下载zip包")
@Log(title = "多选下载zip包", businessType = BusinessType.OTHER)
@PostMapping("/downloadZipFile")
public void downloadZipFile(String ossIds, HttpServletResponse response) throws IOException {
//List<String> fileList=Arrays.asList("E:\\google_down\\688793\\688711公告.PDF","E:\\google_down\\688793\\688793公告.PDF");
//List<String> fileList=Arrays.asList("https://static.cninfo.com.cn/finalpage/2024-01-31/1219044841.PDF","https://static.cninfo.com.cn/finalpage/2024-01-31/1219044842.PDF");
//List<String> fileUrlList = null;
//List<String> fileNameList = null;
OssMetaGq ossMetaGq=new OssMetaGq();
ossMetaGq.setOssIds(ossIds);
List<OssMetaGq> ossMetaGqList = ossMetaGqService.selectJwOssMetaGqListDownZipSql(ossMetaGq);
String zipFileName="batch_down_announcement.zip";
byte[] buf = new byte[1024];
BufferedOutputStream bos = null;
ZipOutputStream out = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
//响应到浏览器
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment;filename="+zipFileName);
out = new ZipOutputStream(bos);
for (int i=0;i<ossMetaGqList.size();i++){
//File file = new File(filePathName);
//FileInputStream in = new FileInputStream(file);
OssMetaGq ossMetaGqOne = ossMetaGqList.get(i);
String filePathName = ossMetaGqOne.getUrl();
String pdate = DateUtils.dateTime(ossMetaGqOne.getPublishdate());
String fileName = ossMetaGqOne.getStockcode()+" "+ossMetaGqOne.getStockticker()+" "+pdate+" "+ossMetaGqOne.getTitle()+".pdf";
URL url = new URL(filePathName);
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
out.putNextEntry(new ZipEntry(fileName));
int len = -1;
while ((len=in.read(buf))!=-1){
out.write(buf,0,len);
}
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (out!=null)
out.close();
if (bos!=null)
bos.close();
}
}