文件下载实现

public void download(String testFilename, HttpServletResponse response,HttpServletRequest request) {
        String path = request.getContextPath();
        File file = new File(path + "download/" + "download.pdf");   
        response.setContentType("multipart/form-data");
        String downloadName = new String(testFilename.getBytes("UTF-8"), "ISO-8859-1"); 
        //文件名记得加上引号
        response.setHeader("Content-   Disposition", "attachment;fileName=\"" + downloadName + "\"");
        try {
            FileInputStream in = new FileInputStream(file);   
            OutputStream out = response.getOutputStream();
            FileCopyUtils.copy(in, out);
        } catch (IOException e) {

        }
    }
}

你可能感兴趣的:(文件下载实现)