javaweb学习总结二十六(response对象的用法二 下载文件)

一:浏览器打开服务器上的文件

1:读取服务器上面的资源,如果在web层,可以直接使用servletContext,如果在非web层

可以使用类加载器读取文件

2:向浏览器写数据,实际上是把数据封装到response对象上,然后服务器发现response中响应

体中有数据绑定,然后写给浏览器

3:设置响应头,控制浏览器的读取或者解析方式

如下:打开服务器上的图片

 1 /**在页面上查看图片*/
 2     private void viewImage(HttpServletResponse response) throws IOException {
 3         // 设置响应头   在网页上查看图片
 4         response.setHeader("content-type", "image/jpeg");
 5         InputStream in = this.getServletContext().getResourceAsStream(
 6                 "/download/1.jpg");
 7         OutputStream out = response.getOutputStream();
 8         int length = 0;
 9         byte[] buf = new byte[1024];
10         while ((length = in.read(buf)) > 0) {
11             out.write(buf, 0, length);
12         }
13         out.flush();
14         out.close();
15     }

二:下载服务器上面的文件

1:下载文件与打开文件类似,都是先读取服务器上面的文件,然后再想浏览器写文件,

只是响应头不同而已。

 response.setHeader("content-disposition", "attachment;filename=1.jpg");

 1 /**下载图片*/
 2     private void downloadImage(HttpServletResponse response) throws IOException {
 3         // 设置响应头   在网页上查看图片
 4         response.setHeader("content-disposition", "attachment;filename=1.jpg");
 5         InputStream in = this.getServletContext().getResourceAsStream(
 6                 "/download/1.jpg");
 7         OutputStream out = response.getOutputStream();
 8         int length = 0;
 9         byte[] buf = new byte[1024];
10         while ((length = in.read(buf)) > 0) {
11             out.write(buf, 0, length);
12         }
13         out.flush();
14         out.close();
15     }


2:如果需要获取文件的名称,最好先获取服务器上文件的绝对路径,然后在读取,写内容到浏览器。

String path = this.getServletContext().getRealPath("/download/高圆圆.jpg");

 1 private void downloadImage2(HttpServletResponse response){
 2         String path = this.getServletContext().getRealPath("/download/高圆圆.jpg");
 3         String filename = path.substring(path.lastIndexOf("\")+1);
 4                  //设置下载文件响应头
 5         response.setHeader("content-disposition", "attachment;filename="+filename);
 6         InputStream in = null;
 7         OutputStream out = null;
 8         try {
 9             in = new FileInputStream(path);
10             out = response.getOutputStream();
11             int len = 0;
12             byte[] buf = new byte[1024];
13             while((len = in.read(buf)) > 0){
14                 out.write(buf, 0, len);
15             }
16         } catch (Exception e) {
17             e.printStackTrace();
18         } finally{
19             if(null != in){
20                 try {
21                     in.close();
22                 } catch (IOException e) {
23                     e.printStackTrace();
24                 }
25             }
26             if(null != out){
27                 try {
28                     out.close();
29                 } catch (IOException e) {
30                     e.printStackTrace();
31                 }
32             }
33         }
34     }

如果文件名为中文时,下载会出现乱码问题,导致无法下载,

这时我们可以先对文件名称进行编码,如下:

1 String filename = path.substring(path.lastIndexOf("\")+1);
2         try {
3             filename = URLEncoder.encode(filename,"utf-8");
4         } catch (UnsupportedEncodingException e1) {
5             e1.printStackTrace();
6         }

这样乱码问题就解决了!

原文地址:https://www.cnblogs.com/warrior4236/p/6024883.html