由于xml文件的格式比较特殊,因为和html存在冲突,所以在这里说明一下我用的是这个标签,因为 标签之间的内容不会被当作文档内容解析,而会被用等宽字体直接呈现。
@RequestMapping(value = "/previewFile", method = RequestMethod.GET)
public String previewFile(String key, String fileFormat) {
String url = getPath(key);
String aimPath = url.replace(fileFormat, ".html");
String encode = "";
String fileContent = "";
try {
encode = EncodingDetect.getJavaEncode(url);
fileContent = FileUtils.readFileToString(new File(url), encode);
} catch (Exception e) {
logger.warn("文件转换异常");
}
File newfile = new File(aimPath);
String str = "<html><head></head><title>" + newfile.getName() + "</title><xmp style='margin-left: 5px;'>" + fileContent + "</xmp></html>";
return str;
}
本身是下载+预览的功能,这里主要介绍下载
<body>
<button onclick="downLoad()" id="btn">下载</button>
<iframe id="mainframe" style="float: left;width:100%;height:96%;" frameborder="0" scrolling="auto"></iframe>
</body>
function downLoad() {
window.location.href = baseURL + "cms/systemModuleFileCollectNonCommon/downLoadFile?path=" + encodeURIComponent(filePath)+"&&ip="+ip;
}
后端下载的java接口