JAVAWEB file download

Posted by nassertx on Tue, 10 Dec 2019 11:37:40 +0100

 

Use: file download in Java Web Environment

Front end page code:

<a class="formControls col-sm-5" id="view_filePath" ></a>

jquery code (data is the data obtained from the background by making ajax request)
$('#view_filePath').html(data.filePath);
$('#view_filePath').attr('href','/ctl-web/file/downFile?filePath='+data.filePath);

Background code:

@Controller
@RequestMapping(value = "/file")
public class CtlFileController {
    @RequestMapping(value = "/downFile",params = "filePath"
    )
    public void downloadFile(@RequestParam(value="filePath",required = true)  String filePath, HttpServletResponse response) {
        File downFile = new File(filePath);
        response.setContentType("multipart/form-data");
        InputStream ins = null;
        response.setHeader("Content-Disposition", "attachment;filename=" + downFile.getName());
        try {
            ins = new FileInputStream(downFile);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = ins.read(b)) > 0) {
                os.write(b, 0, len);
            }
            os.flush();
            os.close();
            ins.close();
        } catch (FileNotFoundException ffe) {
            ffe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
//Just follow the above method.

In the previous test, the controller can be downloaded by directly accessing it with a browser locally, but it can't be downloaded by using $. Get ('/ CTL Web / file / downfile', {'filepath': data. Filepath}, function() {}). This is because of the response reason. Generally, the requesting browser will process the response output from the server, such as generating png, downloading files, etc., but the ajax request is just a "character type" ”The content of the request is stored in text type. The file is downloaded in binary form. Although the returned response can be read, it is only read and cannot be executed. js cannot call the browser's download processing mechanism and program.
--------------------- 
Author: fan510988896
Source: CSDN
Original: https://blog.csdn.net/fan510988896/article/details/71520390
Copyright notice: This is the original article of the blogger. Please attach the link of the blog! In addition, when testing the background, it is found that if the interface is written in restful style, it cannot be found.

The code is as follows:

package com.pay.ctl.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Controller
@RequestMapping(value = "/file")
public class CtlFileController {
    @RequestMapping(value = "/{filePath}/downFile",params = "filePath"
    )
    public void downloadFile(@PathVarible("filePath")  String filePath, HttpServletResponse response) {
        File downFile = new File(filePath);
        /* Set the content type of the file, and then the download file type will be determined automatically */
        response.setContentType("multipart/form-data");
        /* Set file header: the last parameter is to set the download file name */
        InputStream ins = null;
        response.setHeader("Content-Disposition", "attachment;filename=" + downFile.getName());
        try {
            ins = new FileInputStream(downFile);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = ins.read(b)) > 0) {
                os.write(b, 0, len);
            }
            os.flush();
            os.close();
            ins.close();
        } catch (FileNotFoundException ffe) {
            ffe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

 

  

Topics: Programming Java JQuery