master
huanghai 5 years ago
parent 1caa1470c3
commit 40d90aa71e

@ -3,12 +3,15 @@ package PreviewController
import (
"dsSupport/Const"
"dsSupport/Utils/CommonUtil"
"dsSupport/Utils/FileUtil"
"dsSupport/Utils/ObsUtil"
"encoding/base64"
"github.com/gin-gonic/gin"
"log"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
//模块的路由配置
@ -30,11 +33,32 @@ func previewOffice(c *gin.Context) {
uEnc := c.Query("p")
uDec, err := base64.URLEncoding.DecodeString(uEnc)
if err != nil {
log.Fatalln(err)
c.String(http.StatusOK, "输入的参数p转为base64Url时失败请确认是否未经Base64.URLEdcoding进行加码!")
return
}
fullPath := string(uDec)
//(1) 是否以http或https开头
if strings.HasPrefix(strings.ToLower(fullPath), "https://") || strings.HasPrefix(strings.ToLower(fullPath), "http://") {
//下载
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)
}
f, err := os.Create(fullPath)
if err != nil {
c.String(http.StatusOK, "创建文件时发生读写错误:"+err.Error())
return
}
io.Copy(f, res.Body)
}
//(2) 是不是本地文件
_, p := filepath.Split(fullPath)

@ -7,17 +7,25 @@ import (
"net/http"
)
func main() {
// 如果要用在url中需要使用URLEncoding
input:="C:\\Users\\Administrator\\Desktop\\技术讨论会\\20A4782A-512C-40ED-8AF9-FCFC7CD29E5B.docx"
func showPreview(input string){
uEnc := base64.URLEncoding.EncodeToString([]byte(input))
fmt.Println(uEnc)
//生成预览OFFICE的服务地址
url := "http://10.10.24.100:9000/support/preview/preview"
url := "http://10.10.24.100:9000/support/preview/previewOffice"
resp, _ := http.Get(url+"?p="+uEnc)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
func main() {
//(1)传入本地文件路径
// 如果要用在url中需要使用URLEncoding
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)
//Java的调用方法示例
//https://blog.csdn.net/orangleliu/article/details/38309367

@ -7,6 +7,8 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
/**
@ -65,3 +67,29 @@ func PathExists(path string) (bool, error) {
}
return false, err
}
/**
2020-06-02
*/
func GetCurrentPath() string {
dir, _ := os.Getwd()
dir = strings.ReplaceAll(dir, "\\", "/")
return dir
}
/**
2020-06-02
*/
func GetCurrentParentPath()string{
return GetParentPath(GetCurrentPath())
}
/**
:
*/
func GetParentPath(directory string) string {
return strings.Replace(filepath.Dir(directory), "\\", "/", -1)
}

@ -56,4 +56,3 @@ func WriteLines(lines []string, path string) error {
return w.Flush()
}

Loading…
Cancel
Save