您的位置:首页 > 理论基础 > 计算机网络

java在线预览网络pdf文件和图片

2018-01-16 14:22 363 查看
后台代码:
public void previewFileOnline(){
BufferedInputStream bis = null;
OutputStream os = null;
try {
String path = request("filePath");//网络图片地址
response().setContentType("text/html; charset=UTF-8");
if("pdf".equalsIgnoreCase(request("type"))){
response().setContentType("application/pdf");
}else{
response().setContentType("image/jpeg");
}
URL url =new URL(path);
bis = new BufferedInputStream(url.openStream());
os = response().getOutputStream();
int count = 0;
byte[] buffer = new byte[1024 * 1024];
while ((count =bis.read(buffer)) != -1){
os.write(buffer, 0,count);
}
os.flush();
}catch (Exceptione) {
e.printStackTrace();
} finally {
if (os !=null){
try {
os.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
if (bis !=null){
try {
bis.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
}
}

前台代码:

window.open("${ctx}/battFile_previewFileOnline?filePath="+filePath+"&type="+type);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: