SpringBoot 文件下载(doc)

版权声明:帅气Dee海绵宝宝 https://blog.csdn.net/xyjcfucdi128/article/details/83860853

application.properties 配置路径 以便于修改

web.upload-path=E:/content
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}

 接下来是controller层代码

@Value("${web.upload-path}")
	private String uploadPath;

@RequestMapping("/down")
    public ResponseEntity<FileSystemResource> down()throws UnsupportedEncodingException{
		
		/*String realimgurl=uploadPath+"/templte/template.xlsx";*/
		String realimgurl=uploadPath+"/scDoc/Self-service analysis tool _ user manual.doc";
		//System.out.println(realimgurl);
		
        File file = new File(realimgurl);
        if (file == null){
            return null;
        }
        
        String suffixType =realimgurl.substring(realimgurl.lastIndexOf("."));
        String newfilename ="自助分析工具操作手册"+suffixType;
        
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=" +  URLEncoder.encode(newfilename, "UTF-8"));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new FileSystemResource(file));
    
		
		
	}

猜你喜欢

转载自blog.csdn.net/xyjcfucdi128/article/details/83860853