|
在电脑浏览器上是能够在线浏览pdf文件的,但是在苹果手机中在新开的窗口中是没有内容的。
前台代码:
var url = "http://192.168.31.190:8080/baas/base/download";
var name = "1";
fileApi.browse(url, name).done(function(){
//alert("成功打开");
}).fail(function(){
alert("打开出错");
});
后台代码:
@RequestMapping("download")
@ResponseBody
public void downLoad(HttpServletResponse response) throws Exception {
try {
String filePath = "d:/1.pdf";
File f = new File(filePath);
if (!f.exists()) {
System.out.println("error");;
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
System.out.println(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
System.out.println("end");
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} |
|