Servlet case 2: File Download

Posted by psymonic on Wed, 22 Apr 2020 16:53:01 +0200

First, solve two small problems

1. Input Chinese code disorder

A small Demo can solve:

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 {
        
        //Set up response Code table queried
        //response.setCharacterEncoding("UTF-8");
        
        //Through a head Content-Type Tell the client what code table to use
        //response.setHeader("Content-Type", "text/html;charset=UTF-8");
        
        //The above two lines can be abbreviated as follows, Tomcat Auto set encoding
        response.setContentType("text/html;charset=UTF-8");
        
        PrintWriter writer = response.getWriter();
        //writer.write("hello response!!!");No need to consider coding when writing English
        writer.write("Hello");
        
    }

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

 

2. How to see the picture on the client page

Use byte stream, for example:

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 {

        // Use response Get byte output stream
        ServletOutputStream out = response.getOutputStream();

        // Get pictures on the server
        String realPath = this.getServletContext().getRealPath("a.jpg");
        InputStream in = new FileInputStream(realPath);

        // Byte array improves efficiency
        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);
    }
}

 

 

Next is the file download case:

Create download folder under WebContent directory and put various files for download

 

There are two ways to download:

1. < a > the tag points directly to the server resources, and the browser displays those that can be parsed and those that can't be parsed can be downloaded

This method only needs html code (new download.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Use a The label points to the resource on the server</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>Using server-side coding to download files</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>

 

2. Download files by server-side coding

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 {
        // Get the name of the file to download
        String filename = request.getParameter("filename");
        // Downloaded file type( MIME Type)
        response.setContentType(this.getServletContext().getMimeType(filename));
        // Tell the client that the file is not directly parsed but opened as an attachment(download)
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        // Get the absolute path of the file
        String path = this.getServletContext().getRealPath("download/" + filename);
        // Get the file input stream
        InputStream in = new FileInputStream(path);
        // adopt response Get output stream
        ServletOutputStream out = response.getOutputStream();
        // File copy
        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);
    }
}

Topics: Java Tomcat encoding