Spring MVC提供http接口供下载文件

废话不多说,直接上源码:

@Controller
public class DownLoadController {
    @RequestMapping(value="/zyg/download/lemmainfo")
    public void downloadResource(@RequestParam(value = "fileName", required = true) String fileName, HttpServletResponse response) {
        String dataDirectory = "/data/denglinjie/everydayLemmaInfo/";
        Path file = Paths.get(dataDirectory, fileName);
        if (Files.exists(file)) {
            response.setContentType("application/x-gzip");
            try {
                response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
                Files.copy(file, response.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}


你可能感兴趣的:(Java,Spring)