文件下载和预览

做一个简单的下载和预览 当然这只是一个demo 想要内嵌到自己的项目还需要稍微改一改 不过很简单

前台代码很简易

<button type="button" id="preview">预览</button>

// 预览
$("#preview").on("click",function(){
window.open("/test/main/preview?Id=9527");
})

后端代码  关于转成pdf需要注意的的地方

https://www.cnblogs.com/Mr-Y1907/p/11263807.html 

我这里有介绍

/**
* 把word文件转成pdf进行预览
* @param Id
* @param response
* @throws IOException
*/
@RequestMapping("/preview")
public void preview(@RequestParam String Id, HttpServletResponse response) throws IOException {
//Integer.valueOf(Id)
// 正常是前台传入一个id 根据id查询文件地址
FileInputStream bis = null;
OutputStream os1 = null;
FileOutputStream os=null;
try {
//除去pdf水印
InputStream is = Test.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);

File file1 = new File("D:\\test");
if (!file1.exists()) {// 目录不存在就创建
file1.mkdirs();
}
File file = new File("D:\\test\\demo.pdf"); //新建一个空白pdf文档
if (file.exists()) {
file.delete();
}
//转PDF
os = new FileOutputStream(file);
Document doc = new Document("D:\\test\\96543.docx"); //Address是将要被转化的word文档 // 这是demo 只能写死
doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
//输出到浏览器
response.setContentType("text/html; charset=UTF-8");
response.setContentType("application/pdf");
bis = new FileInputStream(file.getPath());
os1 = response.getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 1024];
while ((count =bis.read(buffer)) != -1){
os1.write(buffer, 0,count);
}
os1.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os1 !=null){
os1.close();
}
if (os !=null){
os.close();
}
if (bis !=null){
bis.close();
}
}
}

接下来就是下载 

前台

<button type="button" id="loadfile">下载文件</button>

// 下载
$("#loadfile").on("click",function(){
window.open("/test/main/loadfile?Id=9527");
})

/**
* 下载文件
* @param Id
* @param response
* @throws IOException
*/
@RequestMapping("/loadfile")
public void loadfile(@RequestParam String Id, HttpServletResponse response) throws IOException {
//UfOmsOrderSop sop=ufOmsOrderSopService.selectId(Integer.valueOf(Id));
InputStream fis=null;
OutputStream toClient=null;
try {
// path是指欲下载的文件的路径。
File file = new File("D:\\test\\96543.docx");// 因为是demo 只能写死 正常来说 你应该从数据库里获取地址
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1);
filename="文件名"+"."+ext;

// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream("D:\\test\\96543.docx"));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition","attachment;filename=" + new String(filename.getBytes("utf-8"),"ISO8859-1")); //文件名解决中文乱码
response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
}finally {
if(fis!=null) {
fis.close();
}
if(toClient!=null) {
toClient.close();
}
}

}

猜你喜欢

转载自www.cnblogs.com/Mr-Y1907/p/11275366.html
今日推荐