Download the file and preview

Make a simple download and preview course this is only a demo want to built their own projects also need to be slightly altered, but very simple

Reception code is very simple

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

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

 

Place on back-end code turn into a pdf Note

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

I have here introduced

/ **
* The word transferred into pdf file preview
* @param Id
* @param Response
* @throws IOException
* /
@RequestMapping ( "/ preview")
public void preview (@RequestParam String Id, the HttpServletResponse Response) throws IOException {
/ /Integer.valueOf(Id)
// pass in a normal reception is based on id id query file address
FileInputStream BIS = null;
OutputStream OS1 = null;
FileOutputStream os = null;
the try {
// remove pdf watermark
InputStream is = Test.class. getClassLoader () getResourceAsStream ( "the license.xml");.
License aposeLic = new new License ();
aposeLic.setLicense (IS);

File file1 = new new File ( "D: \\ the Test");
IF (file1.exists (! )) {// create a directory does not exist on
file1.mkdirs ();
}
File file = new File ( "D : \\ test \\ demo.pdf"); // create a new blank pdf file
IF (File.Exists ()) {
File.delete ();
}
// turn PDF
OS = new new FileOutputStream (File);
the document DOC = new new the document ( "D: \\ \\ 96543.docx the Test"); // Address word document is to be transformed // this is a demo only write dead
doc.save (os, SaveFormat.PDF); // full support for DOC, DOCX, OOXML, RTF HTML , OpenDocument, PDF, EPUB, XPS, SWF conversion
// output to the browser
response.setContentType ( "text / html; charset = UTF-8" );
the response.setContentType ( "file application / PDF");
BIS new new = the FileInputStream (file.getPath ());
OS1 = response.getOutputStream ();
int COUNT = 0;
byte [] = new new byte Buffer [1024 * 1024] ;
the 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();
}
}
}

The next step is to download 

Reception

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

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

/ **
* Download files
* @param Id
* @param the Response
* @throws IOException
* /
@ RequestMapping ( "/ LoadFile")
public void LoadFile (@RequestParam String Id, HttpServletResponse the Response) throws IOException {
// UfOmsOrderSop SOP = ufOmsOrderSopService. selectid (Integer.valueOf (Id));
the InputStream FIS = null;
the OutputStream toClient = null;
the try {
// path refers to the path to the downloaded file.
File file = new File ( "D : \\ test \\ 96543.docx"); // demo only because it is written dead Normally you should obtain the address from the database
// Get the file name.
File.getName filename = String ();
// get the file extension.
Filename.substring EXT = String (filename.lastIndexOf () + 1 ".");
"." Filename = "filename" + + EXT;

// download file as a stream.
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();
}
}

}

 

Guess you like

Origin www.cnblogs.com/Mr-Y1907/p/11275366.html