Servlet案例2:文件下载

2018-06-18 03:30:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

首先,解决两个小问题

1.输入中文乱码问题

一个小Demo即可解决:

package demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DemoServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //设置response查询的码表
        //response.setCharacterEncoding("UTF-8");
        
        //通过一个头 Content-Type 告知客户端使用何种码表
        //response.setHeader("Content-Type", "text/html;charset=UTF-8");
        
        //以上两行可以简写成这样,Tomcat自动设置编码
        response.setContentType("text/html;charset=UTF-8");
        
        PrintWriter writer = response.getWriter();
        //writer.write("hello response!!!");写英文不需要考虑编码问题
        writer.write("你好");
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
View Code

 

2.怎么在客户端页面看到图片

用到字节流,示例如下:

package demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ByteServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 使用response获得字节输出流
        ServletOutputStream out = response.getOutputStream();

        // 获得服务器上的图片
        String realPath = this.getServletContext().getRealPath("a.jpg");
        InputStream in = new FileInputStream(realPath);

        // 字节数组提高效率
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
View Code

 

 

接下来是文件下载案例:

WebContent目录下创建download文件夹,放入各种文件,以供下载

 

这里有两种方式下载:

1.<a>标签直接指向服务器资源,浏览器显示能解析的,解析不了的提供下载

这种方式只需要html代码即可(新建download.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>使用a标签指向服务器上的资源</h1>
    <a href="/WEB4/download/a.flv">a.flv</a>
    <br />
    <a href="/WEB4/download/a.jpg">a.jpg</a>
    <br />
    <a href="/WEB4/download/a.flv">a.mp3</a>
    <br />
    <a href="/WEB4/download/a.mp4">a.mp4</a>
    <br />
    <a href="/WEB4/download/a.txt">a.txt</a>
    <br />
    <a href="/WEB4/download/a.zip">a.zip</a>
    <br />
    <h1>使用服务器端编码的方式实现文件下载</h1>
    <a href="/WEB4/downloadServlet?filename=a.flv">a.flv</a>
    <br />
    <a href="/WEB4/downloadServlet?filename=a.jpg">a.jpg</a>
    <br />
    <a href="/WEB4/downloadServlet?filename=a.mp3">a.mp3</a>
    <br />
    <a href="/WEB4/downloadServlet?filename=a.mp4">a.mp4</a>
    <br />
    <a href="/WEB4/downloadServlet?filename=a.txt">a.txt</a>
    <br />
    <a href="/WEB4/downloadServlet?filename=a.zip">a.zip</a>
    <br />
</body>
</html>
View Code

 

2.使用服务器端编码的方式实现文件下载

package demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得要下载文件名称
        String filename = request.getParameter("filename");
        // 下载的文件类型(MIME类型)
        response.setContentType(this.getServletContext().getMimeType(filename));
        // 告诉客户端该文件不是直接解析 而是以附件形式打开(下载)
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        // 获取文件的绝对路径
        String path = this.getServletContext().getRealPath("download/" + filename);
        // 获得该文件输入流
        InputStream in = new FileInputStream(path);
        // 通过response获得输出流
        ServletOutputStream out = response.getOutputStream();
        // 文件拷贝
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
View Code

 

还有一个小问题没有解决,文件名如果是中文,需要解决编码问题:

package demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Encoder;

public class DownloadServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // *******文件名称是中文的下载*******

        // 获得要下载的文件的名称
        String filename = request.getParameter("filename");
        // 解决获得中文参数的乱码
        filename = new String(filename.getBytes("ISO8859-1"), "UTF-8");

        // 获得请求头中的User-Agent
        String agent = request.getHeader("User-Agent");
        // 根据不同浏览器进行不同的编码
        String filenameEncoder = "";
        if (agent.contains("MSIE")) {
            // IE浏览器
            filenameEncoder = URLEncoder.encode(filename, "utf-8");
            filenameEncoder = filenameEncoder.replace("+", " ");
        } else if (agent.contains("Firefox")) {
            // 火狐浏览器
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
        } else {
            // 其它浏览器
            filenameEncoder = URLEncoder.encode(filename, "utf-8");
        }

        // 要下载的这个文件的类型-----客户端通过文件的MIME类型去区分类型
        response.setContentType(this.getServletContext().getMimeType(filename));
        // 告诉客户端该文件不是直接解析 而是以附件形式打开(下载)----filename="+filename 客户端默认对名字进行解码
        response.setHeader("Content-Disposition", "attachment;filename=" + filenameEncoder);

        // 获取文件的绝对路径
        String path = this.getServletContext().getRealPath("download/" + filename);
        // 获得该文件的输入流
        InputStream in = new FileInputStream(path);
        // 获得输出流---通过response获得的输出流 用于向客户端写内容
        ServletOutputStream out = response.getOutputStream();
        // 文件拷贝的模板代码
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
View Code

 

注意事项:

1.response获得的流其实不需要手动关闭,Tomcat会自动关闭

2.response的getwrite和getoutputstream方法不能同时调用

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java网络爬虫Hello world实现——Httpclient爬取百度首页

下一篇:尝试Spring Data Jpa--告别CRUD