springboor+vue跳转预览存在sftp服务器中的pdf,不用任何组件
时间: 2023-11-27 15:55:28 浏览: 50
预览pdf,无需安装插件
首先,你需要在Spring Boot项目中使用JSch库连接到SFTP服务器,并从服务器上下载PDF文件。以下是一个简单的示例:
```java
import com.jcraft.jsch.*;
import java.io.*;
public class SftpUtil {
private static final int BUFFER_SIZE = 1024;
public void downloadFile(String host, int port, String username, String password, String remoteFilePath, String localFilePath) throws JSchException, SftpException, IOException {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
InputStream inputStream = channelSftp.get(remoteFilePath);
OutputStream outputStream = new FileOutputStream(new File(localFilePath));
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
channelSftp.disconnect();
session.disconnect();
}
}
```
接下来,在Spring Boot的Controller中处理GET请求,下载PDF并将其写入响应流中以进行预览:
```java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Controller
public class PdfController {
private static final String SFTP_HOST = "your_sftp_server_host";
private static final int SFTP_PORT = 22;
private static final String SFTP_USERNAME = "your_sftp_username";
private static final String SFTP_PASSWORD = "your_sftp_password";
private static final String SFTP_REMOTE_FILE_PATH = "/path/to/your/pdf/file/on/sftp_server";
@GetMapping("/preview/{filename}")
public void previewPdf(@PathVariable("filename") String filename, HttpServletResponse response) {
String localFilePath = "/path/to/local/temporary/file/" + filename;
try {
SftpUtil sftpUtil = new SftpUtil();
sftpUtil.downloadFile(SFTP_HOST, SFTP_PORT, SFTP_USERNAME, SFTP_PASSWORD, SFTP_REMOTE_FILE_PATH, localFilePath);
File file = new File(localFilePath);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "inline; filename=" + filename);
response.setContentLength((int) file.length());
FileInputStream fileInputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
最后,在Vue中使用`<embed>`标签来显示PDF文件:
```html
<template>
<div>
<embed :src="'/preview/' + filename" type="application/pdf" width="100%" height="600px" />
</div>
</template>
<script>
export default {
name: 'PdfPreview',
props: {
filename: String
}
}
</script>
```
这样,当你在Vue中使用`<pdf-preview>`组件时,它会从SFTP服务器中下载PDF文件并在页面上预览它。
阅读全文