mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-09-18 17:48:08 +08:00
文件预览:xi修复各分支不一致的问题
This commit is contained in:
@@ -22,26 +22,18 @@ public class DownloadUtils {
|
||||
* 再次测试的时候,通过前台对比url发现,原来参数中有+号特殊字符存在,但是到后之后却变成了空格,突然恍然大悟
|
||||
* 应该是转义出了问题,url转义中会把+号当成空格来计算,所以才会出现这种情况,遂想要通过整体替换空格为加号,因为url
|
||||
* 中的参数部分是不会出现空格的,但是文件名中就不好确定了,所以只对url参数部分做替换
|
||||
* 注: 针对URLEncoder.encode(s,charset)会将空格转成+的情况需要做下面的替换工作
|
||||
* @param urlAddress
|
||||
* @param type
|
||||
* @param needEncode
|
||||
* 在处理本地文件(测试预览界面的,非ufile)的时候要对中文进行转码,
|
||||
* 因为tomcat对[英文字母(a-z,A-Z)、数字(0-9)、- _ . ~ 4个特殊字符以及所有保留字符]
|
||||
* 以外的字符会处理不正常,导致失败
|
||||
* @return
|
||||
*/
|
||||
public ReturnResponse<String> downLoad(String urlAddress, String type, String fileName, String needEncode){
|
||||
// type = dealWithMS2013(type);
|
||||
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
|
||||
URL url = null;
|
||||
try {
|
||||
if (null != needEncode) {
|
||||
urlAddress = encodeUrlParam(urlAddress);
|
||||
// 因为tomcat不能处理'+'号,所以讲'+'号替换成'%20%'
|
||||
urlAddress = urlAddress.replaceAll("\\+", "%20");
|
||||
}else{
|
||||
urlAddress = replacePlusMark(urlAddress);
|
||||
}
|
||||
urlAddress = encodeUrlParam(urlAddress);
|
||||
// 因为tomcat不能处理'+'号,所以讲'+'号替换成'%20%'
|
||||
urlAddress = urlAddress.replaceAll("\\+", "%20");
|
||||
url = new URL(urlAddress);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
@@ -93,6 +85,8 @@ public class DownloadUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 注:可能是原来因为前端通过encodeURI来编码的,因为通过encodeURI编码+会被转成+号(亦即没有转),
|
||||
* 而通过encodeURIComponent则会转成%2B,这样URLDecoder是可以正确处理的,所以也就没有必要在这里替换了
|
||||
* 转换url参数部分的空格为加号(因为在url编解码的过程中出现+转为空格的情况)
|
||||
* @param urlAddress
|
||||
* @return
|
||||
@@ -116,7 +110,7 @@ public class DownloadUtils {
|
||||
String param = "";
|
||||
if (urlAddress.contains("?")) {
|
||||
path = urlAddress.substring(0, urlAddress.indexOf("?"));
|
||||
param = urlAddress.substring(urlAddress.indexOf("?") + 1);
|
||||
param = urlAddress.substring(urlAddress.indexOf("?"));
|
||||
}else {
|
||||
path = urlAddress;
|
||||
}
|
||||
|
@@ -30,23 +30,6 @@ public class OfficeToPdf {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 连接OpenOffice.org 并且启动OpenOffice.org
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
/*public OfficeManager getOfficeManager() {
|
||||
DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
|
||||
// 获取OpenOffice.org 3的安装目录
|
||||
String officeHome = openOfficePath;
|
||||
config.setOfficeHome(officeHome);
|
||||
// 启动OpenOffice的服务
|
||||
OfficeManager officeManager = config.buildOfficeManager();
|
||||
officeManager.start();
|
||||
return officeManager;
|
||||
}*/
|
||||
|
||||
/**
|
||||
* 转换文件
|
||||
*
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package com.yudianbank.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.ToDoubleFunction;
|
||||
@@ -53,9 +55,8 @@ public class WordToHtml {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException, ArchiveException, RarException {
|
||||
File file = new File("C:\\Users\\yudian-it\\Downloads\\Downloads.zip");
|
||||
System.out.println("Objects.equals(new Integer(1000), new Integer(1000)) :" + Objects.equals(new Integer(1000), new Integer(1000)));
|
||||
System.out.println(Integer.valueOf("-129") == Integer.valueOf("-129"));
|
||||
System.out.println(URLEncoder.encode(" ", "UTF-8"));
|
||||
System.out.println(URLDecoder.decode(" http://keking.ufile.ucloud.com.cn/20171230213253_2017%E5%B9%B4%20%E5%BA%A6%E7%BB%A9%E6%95%88%E8%80%83%E6%A0%B8%E8%A1%A8%E5%8F%8A%E8%AF%84%E4%BC%98%E6%8E%A8%E8%8D%90%E8%A1%A8.xlsxUCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=Pbi/J6UcOZcvGwhAwExe3SpxrGo=", "UTF-8"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,22 +1,15 @@
|
||||
package com.yudianbank.web.controller;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yudianbank.param.ReturnResponse;
|
||||
import com.yudianbank.utils.DownloadUtils;
|
||||
import com.yudianbank.utils.FileUtils;
|
||||
import com.yudianbank.utils.OfficeToPdf;
|
||||
import com.yudianbank.utils.ZipReader;
|
||||
import com.yudianbank.utils.*;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@@ -28,6 +21,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author yudian-it
|
||||
@@ -42,6 +36,10 @@ public class OnlinePreviewController {
|
||||
DownloadUtils downloadUtils;
|
||||
@Autowired
|
||||
ZipReader zipReader;
|
||||
@Autowired
|
||||
SimTextUtil simTextUtil;
|
||||
@Value("${simText}")
|
||||
String[] simText;
|
||||
|
||||
@Value("${file.dir}")
|
||||
String fileDir;
|
||||
@@ -54,31 +52,31 @@ public class OnlinePreviewController {
|
||||
@RequestMapping(value = "onlinePreview",method = RequestMethod.GET)
|
||||
public String onlinePreview(String url, String needEncode, Model model, HttpServletRequest req) throws UnsupportedEncodingException {
|
||||
// 路径转码
|
||||
url = URLDecoder.decode(url, "utf-8");
|
||||
String decodedUrl = URLDecoder.decode(url, "utf-8");
|
||||
String type = typeFromUrl(url);
|
||||
String suffix = suffixFromUrl(url);
|
||||
// 抽取文件并返回文件列表
|
||||
String fileName = fileUtils.getFileNameFromURL(decodedUrl);
|
||||
model.addAttribute("fileType", suffix);
|
||||
if (type.equalsIgnoreCase("picture")) {
|
||||
model.addAttribute("imgurl", url);
|
||||
return "picture";
|
||||
} else if (type.equalsIgnoreCase("txt")
|
||||
|| type.equalsIgnoreCase("html")
|
||||
|| type.equalsIgnoreCase("xml")
|
||||
|| type.equalsIgnoreCase("java")
|
||||
|| type.equalsIgnoreCase("properties")
|
||||
|| type.equalsIgnoreCase("mp3")){
|
||||
model.addAttribute("ordinaryUrl",url);
|
||||
} else if (type.equalsIgnoreCase("simText")){
|
||||
ReturnResponse<String> response = simTextUtil.readSimText(decodedUrl, fileName, needEncode);
|
||||
if (0 != response.getCode()) {
|
||||
model.addAttribute("msg", response.getMsg());
|
||||
return "fileNotSupported";
|
||||
}
|
||||
model.addAttribute("ordinaryUrl", response.getMsg());
|
||||
return "txt";
|
||||
} else if(type.equalsIgnoreCase("pdf")){
|
||||
model.addAttribute("pdfUrl",url);
|
||||
return "pdf";
|
||||
} else if(type.equalsIgnoreCase("compress")){
|
||||
// 抽取文件并返回文件列表
|
||||
String fileName = fileUtils.getFileNameFromURL(url);
|
||||
String fileTree = null;
|
||||
// 判断文件名是否存在(redis缓存读取)
|
||||
if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName))) {
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(url, suffix, fileName, needEncode);
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(decodedUrl, suffix, fileName, needEncode);
|
||||
if (0 != response.getCode()) {
|
||||
model.addAttribute("msg", response.getMsg());
|
||||
return "fileNotSupported";
|
||||
@@ -104,7 +102,6 @@ public class OnlinePreviewController {
|
||||
return "fileNotSupported";
|
||||
}
|
||||
} else if ("office".equalsIgnoreCase(type)) {
|
||||
String fileName = fileUtils.getFileNameFromURL(url);
|
||||
boolean isHtml = suffix.equalsIgnoreCase("xls")
|
||||
|| suffix.equalsIgnoreCase("xlsx");
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
|
||||
@@ -112,7 +109,7 @@ public class OnlinePreviewController {
|
||||
if (!fileUtils.listConvertedFiles().containsKey(pdfName)) {
|
||||
String filePath = fileDir + fileName;
|
||||
if (!new File(filePath).exists()) {
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(url, suffix, null, needEncode);
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(decodedUrl, suffix, null, needEncode);
|
||||
if (0 != response.getCode()) {
|
||||
model.addAttribute("msg", response.getMsg());
|
||||
return "fileNotSupported";
|
||||
@@ -168,6 +165,9 @@ public class OnlinePreviewController {
|
||||
if (fileUtils.listOfficeTypes().contains(fileType.toLowerCase())) {
|
||||
fileType = "office";
|
||||
}
|
||||
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
|
||||
fileType = "simText";
|
||||
}
|
||||
return fileType;
|
||||
}
|
||||
|
||||
|
@@ -1,172 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>图片预览图</title>
|
||||
<link rel="stylesheet" href="css/viewer.min.css">
|
||||
<link rel="stylesheet" href="css/loading.css">
|
||||
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.css" />
|
||||
<style type="text/css">
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>文件预览项目接入和测试界面</h1>
|
||||
<div class="panel-group" id="accordion">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h4 class="panel-title">
|
||||
<a data-toggle="collapse" data-parent="#accordion"
|
||||
href="#collapseOne">
|
||||
接入说明
|
||||
</a>
|
||||
</h4>
|
||||
</div>
|
||||
<div id="collapseOne" class="panel-collapse collapse in">
|
||||
<div class="panel-body">
|
||||
<div>
|
||||
如果你的项目需要接入文件预览项目,达到对docx、excel、ppt、jpg等文件的预览效果,那么通过在你的项目中加入下面的代码就可以
|
||||
成功实现:
|
||||
<pre style="background-color: #2f332a;color: #cccccc">
|
||||
$scope.openWin = function (fileUrl) {
|
||||
var url = configuration.previewUrl + encodeURIComponent(fileUrl);
|
||||
var winHeight = window.document.documentElement.clientHeight-10;
|
||||
$window.open(url, "_blank", "height=" + winHeight
|
||||
+ ",top=80,left=80,toolbar=no, menubar=no, scrollbars=yes, resizable=yes");
|
||||
};
|
||||
</pre>
|
||||
<b>说明:</b>
|
||||
<p>1.这里的fileUrl即是需要预览的服务器文件,一般是ufile文件</p>
|
||||
<p>2.只所以使用encodeURIComponent转码是因为ufile文件中可能会存在&等特殊字符,那么如果不转码会被浏览器处理成多参数
|
||||
这样后台获取的就不是需要预览的文件的全路径了。</p>
|
||||
<p>3.configuration.previewUrl是需要接入项目中配置文件预览项目的地址的配置
|
||||
现在开发和测试地址都是:
|
||||
http://106.75.31.215:8012/onlinePreview?url=服务器(ufile)文件路径</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h4 class="panel-title">
|
||||
<a data-toggle="collapse" data-parent="#accordion"
|
||||
href="#collapseTwo">
|
||||
预览测试
|
||||
</a>
|
||||
</h4>
|
||||
</div>
|
||||
<div id="collapseTwo" class="panel-collapse collapse">
|
||||
<div class="panel-body">
|
||||
<p style="color: red;">因为是测试所以一种文件只允许上传一个</p>
|
||||
<div style="padding: 10px">
|
||||
<form enctype="multipart/form-data" id="fileUpload">
|
||||
<input type="file" name="file" />
|
||||
<input type="button" id="btnsubmit" value=" 上 传 " />
|
||||
</form>
|
||||
</div>
|
||||
<div>
|
||||
<table id="table" data-pagination="true"></table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="loading_container">
|
||||
<div class="spinner">
|
||||
<div class="spinner-container container1">
|
||||
<div class="circle1"></div>
|
||||
<div class="circle2"></div>
|
||||
<div class="circle3"></div>
|
||||
<div class="circle4"></div>
|
||||
</div>
|
||||
<div class="spinner-container container2">
|
||||
<div class="circle1"></div>
|
||||
<div class="circle2"></div>
|
||||
<div class="circle3"></div>
|
||||
<div class="circle4"></div>
|
||||
</div>
|
||||
<div class="spinner-container container3">
|
||||
<div class="circle1"></div>
|
||||
<div class="circle2"></div>
|
||||
<div class="circle3"></div>
|
||||
<div class="circle4"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript" src="config.js"></script>
|
||||
<script src="js/jquery-3.0.0.min.js" type="text/javascript"></script>
|
||||
<script src="https://cdn.bootcss.com/jquery.form/3.09/jquery.form.min.js" type="text/javascript"></script>
|
||||
<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.js"></script>
|
||||
<script>
|
||||
function deleteFile(fileName) {
|
||||
$.ajax({
|
||||
url: env_config.server_delete_url + encodeURIComponent(fileName),
|
||||
success: function (data) {
|
||||
// 删除完成,刷新table
|
||||
if (1 == data.code) {
|
||||
alert(data.msg);
|
||||
}else{
|
||||
$('#table').bootstrapTable('refresh', {});
|
||||
}
|
||||
},
|
||||
error: function (data) {
|
||||
console.log(data);
|
||||
}
|
||||
})
|
||||
}
|
||||
$(function () {
|
||||
$('#table').bootstrapTable({
|
||||
url: 'listFiles',
|
||||
columns: [{
|
||||
field: 'fileName',
|
||||
title: '文件名'
|
||||
}, {
|
||||
field: 'action',
|
||||
title: '操作'
|
||||
},]
|
||||
}).on('pre-body.bs.table', function (e,data) {
|
||||
// 每个data添加一列用来操作
|
||||
$(data).each(function (index, item) {
|
||||
item.action = "<a class='btn btn-default' target='_blank' href='"+env_config.server_preview_url
|
||||
+ encodeURIComponent(env_config.server_base_url + item.fileName ) +"&needEncode=1'>预览</a>" +
|
||||
"<a class='btn btn-default' target='_blank' href='javascript:void(0);' onclick='deleteFile(\""+item.fileName+"\")'>删除</a>";
|
||||
});
|
||||
return data;
|
||||
}).on('post-body.bs.table', function (e,data) {
|
||||
return data;
|
||||
});
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function showLoadingDiv() {
|
||||
var height = window.document.documentElement.clientHeight - 1;
|
||||
$(".loading_container").css("height", height).show();
|
||||
}
|
||||
|
||||
$("#btnsubmit").click(function () {
|
||||
showLoadingDiv();
|
||||
$("#fileUpload").ajaxSubmit({
|
||||
success: function (data) {
|
||||
// 上传完成,刷新table
|
||||
if (1 == data.code) {
|
||||
alert(data.msg);
|
||||
}else{
|
||||
$('#table').bootstrapTable('refresh', {});
|
||||
}
|
||||
$(".loading_container").hide();
|
||||
},
|
||||
error: function (error) { alert(error); $(".loading_container").hide();},
|
||||
url: 'fileUpload', /*设置post提交到的页面*/
|
||||
type: "post", /*设置表单以post方法提交*/
|
||||
dataType: "json" /*设置返回值类型为文本*/
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@@ -55,9 +55,22 @@
|
||||
},
|
||||
onClick:function (event, treeId, treeNode) {
|
||||
if (!treeNode.directory) {
|
||||
var winHeight = window.document.documentElement.clientHeight-10;
|
||||
window.open("${baseUrl}onlinePreview?url=" + encodeURIComponent("${baseUrl}" + treeNode.fileName) + "&needEncode=1",
|
||||
"_blank", "height=" + winHeight + ",top=80,left=80,toolbar=no, menubar=no, scrollbars=yes, resizable=yes");
|
||||
/**实现窗口最大化**/
|
||||
var fulls = "left=0,screenX=0,top=0,screenY=0,scrollbars=1"; //定义弹出窗口的参数
|
||||
if (window.screen) {
|
||||
var ah = screen.availHeight - 30;
|
||||
var aw = (screen.availWidth - 10) / 2;
|
||||
fulls += ",height=" + ah;
|
||||
fulls += ",innerHeight=" + ah;
|
||||
fulls += ",width=" + aw;
|
||||
fulls += ",innerWidth=" + aw;
|
||||
fulls += ",resizable"
|
||||
} else {
|
||||
fulls += ",resizable"; // 对于不支持screen属性的浏览器,可以手工进行最大化。 manually
|
||||
}
|
||||
window.open("onlinePreview?url="
|
||||
+ encodeURIComponent("${baseUrl}" + treeNode.fileName)
|
||||
+ "&needEncode=1", "_blank",fulls);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,9 @@
|
||||
<h1>如果图片质量很好,首次加载图片时间可能会有点长,请耐心等待</h1>
|
||||
|
||||
<ul id="dowebok">
|
||||
<li><img id="Imgbox" src="#" width="800px" height="550px"></li>
|
||||
<#--<li><img id="Imgbox" src="#" width="800px" height="550px"></li>-->
|
||||
|
||||
<li><img id="Imgbox" src="#" width="800px" height="auto"></li>
|
||||
|
||||
</ul>
|
||||
<script src="js/viewer.min.js"></script>
|
||||
@@ -26,6 +28,7 @@
|
||||
document.getElementById("Imgbox").src =document.getElementById("url").value;
|
||||
}
|
||||
var viewer = new Viewer(document.getElementById('dowebok'), {url: 'src'});
|
||||
viewer.show();
|
||||
</script>
|
||||
<input name="url" value="${imgurl}" type="hidden" id="url" >
|
||||
</body>
|
||||
|
Reference in New Issue
Block a user