• 解决kkFileView4.4.0版本pdf、word不能预览问题


    这里使用的是http下载流url预览,遇到的问题。

    官方使用指南:kkFileView - 在线文件预览

    1 前端测试代码

    1.1 官方示例代码

    1.2 本人测试代码

    注意:要给预览文件的url进行编码encodeURIComponent(Base64.encode(previewUrl))。

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <h2>在线预览h2>
    9. <button type="button" onclick="kkFileView()">点击在线预览button>
    10. body>
    11. html>
    12. <script>
    13. var Base64 = {
    14. // private property
    15. _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
    16. //从普通字符串转换成Base64字符串的函数
    17. encode : function (input) {
    18. var output = "";
    19. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    20. var i = 0;
    21. input = Base64._utf8_encode(input);
    22. while (i < input.length) {
    23. chr1 = input.charCodeAt(i++);
    24. chr2 = input.charCodeAt(i++);
    25. chr3 = input.charCodeAt(i++);
    26. enc1 = chr1 >> 2;
    27. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    28. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    29. enc4 = chr3 & 63;
    30. if (isNaN(chr2)) {
    31. enc3 = enc4 = 64;
    32. } else if (isNaN(chr3)) {
    33. enc4 = 64;
    34. }
    35. output = output +
    36. Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
    37. Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
    38. }
    39. return output;
    40. },
    41. //从Base64字符串转换成普通字符串的函数
    42. decode : function (input) {
    43. var output = "";
    44. var chr1, chr2, chr3;
    45. var enc1, enc2, enc3, enc4;
    46. var i = 0;
    47. input = input.replace(/[^A-Za-z0-9+/=]/g, "");
    48. while (i < input.length) {
    49. enc1 = Base64._keyStr.indexOf(input.charAt(i++));
    50. enc2 = Base64._keyStr.indexOf(input.charAt(i++));
    51. enc3 = Base64._keyStr.indexOf(input.charAt(i++));
    52. enc4 = Base64._keyStr.indexOf(input.charAt(i++));
    53. chr1 = (enc1 << 2) | (enc2 >> 4);
    54. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    55. chr3 = ((enc3 & 3) << 6) | enc4;
    56. output = output + String.fromCharCode(chr1);
    57. if (enc3 != 64) {
    58. output = output + String.fromCharCode(chr2);
    59. }
    60. if (enc4 != 64) {
    61. output = output + String.fromCharCode(chr3);
    62. }
    63. }
    64. output = Base64._utf8_decode(output);
    65. return output;
    66. },
    67. //将普通字符串转换成UTF8编码的函数
    68. _utf8_encode : function (string) {
    69. string = string.replace(/\r\n/g,"\n");
    70. var utftext = "";
    71. for (var n = 0; n < string.length; n++) {
    72. var c = string.charCodeAt(n);
    73. if (c < 128) {
    74. utftext += String.fromCharCode(c);
    75. }
    76. else if((c > 127) && (c < 2048)) {
    77. utftext += String.fromCharCode((c >> 6) | 192);
    78. utftext += String.fromCharCode((c & 63) | 128);
    79. }
    80. else {
    81. utftext += String.fromCharCode((c >> 12) | 224);
    82. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
    83. utftext += String.fromCharCode((c & 63) | 128);
    84. }
    85. }
    86. return utftext;
    87. },
    88. //将UTF8编码的字符串转换成普通字符串的函数
    89. _utf8_decode : function (utftext) {
    90. var string = "";
    91. var i = 0;
    92. var c = c1 = c2 = 0;
    93. while ( i < utftext.length ) {
    94. c = utftext.charCodeAt(i);
    95. if (c < 128) {
    96. string += String.fromCharCode(c);
    97. i++;
    98. }
    99. else if((c > 191) && (c < 224)) {
    100. c2 = utftext.charCodeAt(i+1);
    101. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    102. i += 2;
    103. }
    104. else {
    105. c2 = utftext.charCodeAt(i+1);
    106. c3 = utftext.charCodeAt(i+2);
    107. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
    108. i += 3;
    109. }
    110. }
    111. return string;
    112. }
    113. }
    114. function kkFileView(){
    115. // pdf的文件流
    116. var originUrl = 'http://192.168.1.58:8080/getFile?s/lV3bcibQiz34TStDR0NJ3tYfr1fqIvKRrtvEOy5ATi3a7u18wax87gN4DWjch/hcpeqQS8mQMoAYMDIXRzEpqtYk0H7DV/+pmYS/KtdPpk0tQrLSnT5OK8ND6QQgaTypPSswbJNvwhs+l5fE4GHLnvBX+kD9J42YAKO6VzAdzFmyonGjiAbegeLD4QspUcXXsdNSzz/7fDeRjDzc0M+HlMMS8/CiuMUmHqL4ErjuNF4Mn3s42UveYOHlukb0p4PCIJlhZeRBx4zXlTOcLlXItZKVb6gBVHVBH9e6ZpfTuIQJ4VRSN3twEOuIsBZ83w3SIepUH9oaA=';
    117. // 使用fullfilename=xxx给文件指定名称
    118. var previewUrl = originUrl + '&fullfilename=1.pdf';
    119. window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(previewUrl)))
    120. }
    121. script>

    2 测试

    点击在线预览按钮出现以下情况:

    3 解决下载失败报错 

    3.1 报错信息 

    下载失败:org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 Internal Server Error: [<a href="https://so.csdn.net/so/search?q=Apache&spm=1001.2101.3001.7020" target="_blank" class="hl hl-1" data-report-view="{"spm":"1001.2101.3001.7020","dest":"https://so.csdn.net/so/search?q=Apache&spm=1001.2101.3001.7020","extra":"{\"searchword\":\"Apache\"}"}" data-report-click="{"spm":"1001.2101.3001.7020","dest":"https://so.csdn.net/so/search?q=Apache&spm=1001.2101.3001.7020","extra":"{\"searchword\":\"Apache\"}"}" data-tit="Apache" data-pretit="apache">Apache</a> Tomcat/8.0.30 - Error report