代码拉取完成,页面将自动刷新
该项目通过iframe 的形式引入pdf.js, 原则上不管是不是vue,都可以使用
实现的功能:
iframe.contentWindow.PDFViewerApplication.pagesCount
iframe.contentWindow.PDFViewerApplication.page
// 注: 以下形式也可以跳转到指定页
// https://commoncms.ynyplt.cn/file-server/13648711629/9b613676-d867-47e3-873d-47d6d59015d5.pdf#page=1
controller
// 接口部分
@RequestMapping("/pdfdownload")
@ResponseBody
public void fileDownload(@RequestParam("filename") String filename,
HttpServletResponse response) throws UnsupportedEncodingException {
// 通过网络获取文件流
DownloadUtils.getFileStream(filename, response);
}
DownloadUtils
package com.example.mybatis.utils;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class DownloadUtils {
// 下载工具部分
// 将文件流传送到response
public static void getFileStream(String filename, HttpServletResponse response) {
try {
// 根据文件url下载文件
openFile(filename, response);
} catch (Exception e) {
System.out.println("下载失败");
}
}
// 通过url下载文件的方法
private static void openFile(String filePath, HttpServletResponse response) {
// 服务器状态
int HttpResult;
try {
// URL
URL url = new URL(filePath);
// URL连接对象
URLConnection urlconn = url.openConnection();
urlconn.connect();
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
HttpResult = httpconn.getResponseCode();
if (HttpResult != HttpURLConnection.HTTP_OK) {
System.out.println("网络连接失败");
throw new ConnectException("网络连接失败");
} else {
InputStream in = urlconn.getInputStream();
try {
String fileName = "test.pdf";
response.setContentType("application/pdf;charset=utf-8");
response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
int b = 0;
byte[] buffer = new byte[512];
while (b != -1) {
b = in.read(buffer);
if (b != -1) {
response.getOutputStream().write(buffer, 0, b);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件取得失败");
} finally {
try {
if (in != null) {
in.close();
}
response.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件取得失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件取得失败");
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。