springMVC下载文件

需要引入的类:

import org.apache.commons.io.FileUtils;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.http.MediaType;

@RequestMapping("/downloadFile")
    public ResponseEntity<byte[]> downloadFile(String filePath) throws IOException {  
        HttpHeaders headers = new HttpHeaders();    
        File file = new File(filePath);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);    
        String[] filePaths = filePath.split("/");
        String filename = new String(filePaths[filePaths.length-1].getBytes("gbk"),"iso8859-1");
        headers.add("Content-Disposition", "attachment;filename="+filename);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
                                              headers, HttpStatus.OK);    
    } 

猜你喜欢

转载自blog.csdn.net/z793397795/article/details/84369170
今日推荐