com.fastchar.pdfviewer.action.PDFAction Maven / Gradle / Ivy
package com.fastchar.pdfviewer.action;
import com.fastchar.core.FastAction;
import com.fastchar.core.FastChar;
import com.fastchar.utils.FastFileUtils;
import com.fastchar.utils.FastHttpURLConnectionUtils;
import com.fastchar.utils.FastMD5Utils;
import com.fastchar.utils.FastStringUtils;
import java.io.InputStream;
/**
* @author 沈建(Janesen)
* @date 2021/10/22 10:28
*/
public class PDFAction extends FastAction {
@Override
protected String getRoute() {
return "/web/pdf";
}
/**
* 在线实时预览pdf文件
* 参数:
* url 文件地址
*/
public void viewer() {
String url = getParam("url", true);
if (url.startsWith("http://") || url.startsWith("https://")) {
String key = FastMD5Utils.MD5To16(url);
FastChar.getMemoryCache().put(key, url);
redirect("/base/pdfviewer/web/viewer.html?file=" + getProjectHost() + "web/pdf/open/" + key);
}
redirect( "/base/pdfviewer/web/viewer.html?file=" + url);
}
/**
* 打开文件流
* 参数:
* url 文件地址
*/
public void open() {
String urlParam = getUrlParam(0);
String fileUrl = FastChar.getMemoryCache().get(urlParam);
if (FastStringUtils.isEmpty(fileUrl)) {
responseText("无效文件地址!");
}
fileUrl = fileUrl.split("@")[0];
try {
InputStream inputStream = FastHttpURLConnectionUtils.getInputStream(fileUrl);
if (inputStream != null) {
responseStream(inputStream, "application/pdf");
}
} catch (Exception e) {
e.printStackTrace();
}
responseText("文件流打开失败!");
}
}