From springboot to proficient in springboot file download

springboot file download


1 Introduction
Initial experience of 2springboot
3springboot integrated jsp
4springboot attribute injection
5 springboot integration with mybatis
6springboot integrated lombok
7springboot integrated logback log
8springboot enable global hot deployment
9springboot aspect oriented programming
10springboot file upload
11springboot file download
12springboot custom interceptor
13 spring boot as war package
14 spring boot is published as jar package
15springboot custom banner
16springboot profile split
17 summary

Download via interface

Download is relatively simple. We just need to know the file name and storage path of the downloaded file. In the following example, all the files to be downloaded are saved in the download file under webapp
Below the folder, this path can be obtained by code, and the file name needs to be passed from the foreground.

public class TestController {
    public void uploadFile(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //Get the absolute path of the folder where the file is located
        String realPath = request.getServletContext().getRealPath("download");
        //Reading files from specified directory according to file name
        FileInputStream fileInputStream = new FileInputStream(new File(realPath, fileName));
        //Set the response header information and open it as an attachment. If it is not set, the file will be opened directly on the page. To prevent file names from having Chinese names, UTF-8 is added after them
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));
        //Create a response flow, and respond with the response flow
        ServletOutputStream outputStream = response.getOutputStream();
        //Copy the contents of the file stream to the response stream
        IOUtils.copy(fileInputStream, outputStream);
        //Close flow

jsp page core code

<h2>Download page</h2>
<a href="${basePath}test/download?fileName=test1.txt">test1.txt</a>
<a href="${basePath}test/download?fileName=test2.txt">test2.txt</a>
<a href="${basePath}test/download?fileName=test3.png">test3.png</a>

::: tip tip

  1. The download file must be added with a response header. Set the file to be opened as an attachment, or it will be opened directly on the page
  2. This is the application principle of the actual project, but the address of the file in the actual project may need to be read from the database first, and then passed to our interface, that is, our fileName parameter

Picture to page

Just add the download connection to the src of img in the foreground
< img SRC = "${basepath} test / download? Filename = test3. PNG" ALT = "test picture" style = "height: 300px; width:600px; >

Sample download address of this section:

::: warning note
In different platforms, the effect of document display is different. The best and most comprehensive viewing address is: springboot file download
Welcome to blog to learn more: java paradise

Tags: SpringBoot Spring Attribute JSP

Posted on Sun, 14 Jun 2020 01:32:09 -0400 by ADLE