master
huanghai 5 years ago
parent 40d90aa71e
commit e75f2af9d0

@ -10,6 +10,7 @@ import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
@ -29,6 +30,9 @@ func Routers(r *gin.RouterGroup) {
2020-07-15
*/
func previewOffice(c *gin.Context) {
// 允许预览的扩展名
allowExtension := []string{`doc`, `docx`, `rtf`, `xls`, `xlsx`, `ppt`, `pptx`,
`pdf`, `zip`, `rar`, `7z`, `png`, `jpg`, `jpeg`, `wps`, `txt`, `js`, `json`, `css`, `sql`, `xml`, `java`, `cs`}
//本地物理路径
uEnc := c.Query("p")
uDec, err := base64.URLEncoding.DecodeString(uEnc)
@ -39,15 +43,19 @@ func previewOffice(c *gin.Context) {
fullPath := string(uDec)
//(1) 是否以http或https开头
if strings.HasPrefix(strings.ToLower(fullPath), "https://") || strings.HasPrefix(strings.ToLower(fullPath), "http://") {
//文件名称
arr := strings.Split(fullPath, "/")
fi := arr[len(arr)-1]
if !CommonUtil.IsContainString(allowExtension,"."+path.Ext(fi)){
c.String(http.StatusOK, "输入的预览文件,系统不支持!")
return
}
//下载
res, err := http.Get(fullPath)
if err != nil {
c.String(http.StatusOK, "输入的网络地址无法找到,不能进行预览!")
return
}
//文件名称
arr := strings.Split(fullPath, "/")
fi := arr[len(arr)-1]
fullPath = CommonUtil.GetCurrentPath() + "/TempFiles/" + fi
if FileUtil.Exists(fullPath) {
os.Remove(fullPath)
@ -66,7 +74,6 @@ func previewOffice(c *gin.Context) {
//(1)只允许上传office系列的文件
//(2)在上传前判断文件是否已存在,存在了直接返回,不做重复工作,也可以支持强制刷新,需要添加参数
//(3)需要判断文件名是不是是标准的GUID形式不是的不让上传
//(4)对于异常的明显提示信息
//判断是不是本地存在
isExist, err := CommonUtil.PathExists(fullPath)

@ -7,6 +7,11 @@ import (
"net/http"
)
/**
:2020-07-15
*/
func showPreview(input string){
uEnc := base64.URLEncoding.EncodeToString([]byte(input))
fmt.Println(uEnc)
@ -21,12 +26,14 @@ func showPreview(input string){
func main() {
//(1)传入本地文件路径
// 如果要用在url中需要使用URLEncoding
input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx"
showPreview(input)
//input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx"
//showPreview(input)
//2传入http参数
input="https://dsideal.obs.cn-north-1.myhuaweicloud.com/Material/02/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc"
showPreview(input)
//input="https://dsideal.obs.cn-north-1.myhuaweicloud.com/Material/02/0200C9BE-07C6-4EFC-A945-8554C7D41790.doc"
//showPreview(input)
input:=`E:\Work\ShlProjectJava\src\main\java\com\shl\zuowen\model\zuowenModel.java`
showPreview(input)
//Java的调用方法示例
//https://blog.csdn.net/orangleliu/article/details/38309367
}

@ -92,4 +92,26 @@ func GetCurrentParentPath()string{
*/
func GetParentPath(directory string) string {
return strings.Replace(filepath.Dir(directory), "\\", "/", -1)
}
}
/**
2020-05-30
*/
func IsContainString(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
func IsContainInt32(items []int32, item int32) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}

Loading…
Cancel
Save