前台主要代码:
后台主要代码:
final String path = request.getParameter("Path");
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
BufferedInputStream bis = null;
OutputStream os = null;
FileInputStream fileInputStream = new FileInputStream(new File(path));
bis = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[512];
response.reset();
response.setCharacterEncoding("UTF-8");
//不同类型的文件对应不同的MIME类型
response.setContentType("image/png");
//文件以流的方式发送到客户端浏览器
//response.setHeader("Content-Disposition","attachment; filename=img.jpg");
//response.setHeader("Content-Disposition", "inline; filename=img.jpg");
response.setContentLength(bis.available());
os = response.getOutputStream();
int n;
while ((n = bis.read(buffer)) != -1) {
os.write(buffer, 0, n);
}
bis.close();
os.flush();
os.close();
阅读(4470) | 评论(0) | 转发(1) |