jsp 页面根据路径直接打开.pdf 文件
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
导入java.io.*
String aa=request.getparameter("url");//直接打开pdf
out.clear();
out=pageContext.pushBody():
response.setContentType("application/pdf");
try{
String strpdfPath=new String(aa);
File file=new File(strpdfPath);
if(file.exists()){
DataOutputStream temps=new DataOutputStream(response.getOutputStream());
DataInputStream in=new DataInputStream(new FileInputStream(strpdfPath));
byte[] b=new byte[2048];
while((in.read(b))!=-1){
temps.write(b);
temps.flush();
}
in.close();
temps.close();
}else{
out.print(strpdfPath+"文件不存在");
}
}catch(Exception e){
out.print(e.getMessage());
}
else{ //暂无法直接打开word ,,弹出另存为对话框
try {
String fileName =aa;
System.out.println("=====" + fileName);
File wordFile = new File(fileName);
response.reset();
response.setContentType("application/vnd.msword");
response.setHeader("Content-Disposition ",
"inline; filename= " + wordFile.getName());
InputStream is = new FileInputStream(wordFile);
OutputStream os = response.getOutputStream();
int byteread;
byte[] buffer = new byte[1024];
while ((byteread = is.read(buffer)) != -1) {
os.write(buffer, 0, byteread);
}
os.flush();
os.close();
out.clear(); ///加入这两句 , 避免getOutputStream 已经调用的页面的报错
out = pageContext.pushBody();///
} catch (Exception e) {
e.printStackTrace();
}
}
pdf 在不同的浏览器 能直接打开 ,但是在火狐有乱码的问题, 在谷歌浏览器在打开word 时总是弹出另存为对话框 , 其他浏览器可能会弹出直接打开,下载,取消对话框 ,,