大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 使用SpringMVC文件下載功能

使用SpringMVC文件下載功能

更新時間:2021-09-10 10:15:35 來源:動力節點 瀏覽1876次

使用文件下載步驟

對請求處理方法使用void或null作為返回類型,并在方法中添加HttpServletResponse參數

將響應的內容類型設為文件的內容類型

添加一個名為Content-Disposition的HTTP響應標題,并賦值attachment; filename= fileName,這里的fileName是默認文件名,應該出現在File Download(文件下載)對話框中。它通常與文件同名,但是也并非一定如此。(可選)

例子:下載服務器上的pdf文件

指定用戶、密碼登錄表單之后,設置session,然后才可以下載文件:/WEB-INF/data/secret.pdf

Controller代碼

@Controller
public class ResourceController {
private static final Log logger = LogFactory.getLog(ResourceController.class);
/**
 * @param login @ModelAttribute 注解接受表單中的login對象
 * @param session
 * @param model
 * @return
 */
@RequestMapping(value="/login")
public String login(@ModelAttribute Login login, HttpSession session, Model model) {
    System.out.println(login);
    //與jsp中的標簽綁定
    model.addAttribute("login", new Login());
    if ("paul".equals(login.getUserName()) &&
            "secret".equals(login.getPassword())) {
        //設置session
        session.setAttribute("loggedIn", Boolean.TRUE);
        return "Main";
    } else {
        return "LoginForm";
    }
}
@RequestMapping(value="/resource_download")
public String downloadResource(HttpSession session, HttpServletRequest request,
        HttpServletResponse response) {
    if (session == null || 
            session.getAttribute("loggedIn") == null) {
        return "LoginForm";
    }
    String dataDirectory = request.
            getServletContext().getRealPath("/WEB-INF/data");
    System.out.println(dataDirectory);
    File file = new File(dataDirectory, "secret.pdf");
    if (file.exists()) {
        //設置響應類型,這里是下載pdf文件
        response.setContentType("application/pdf");
        //設置Content-Disposition,設置attachment,瀏覽器會激活文件下載框;filename指定下載后默認保存的文件名
        //不設置Content-Disposition的話,文件會在瀏覽器內打卡,比如txt、img文件
        response.addHeader("Content-Disposition",
                "attachment; filename=secret.pdf");
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        // if using Java 7, use try-with-resources
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (IOException ex) {
            // do something, 
            // probably forward to an Error page
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return null;
}
}

LoginForm.jsp

Main.jsp

結果

例子:瀏覽器內顯示圖片。圖片放在WEN-INF目錄下面,可以禁止直接請求

Controller代碼

@Controller
public class ImageController {
private static final Log logger = LogFactory.getLog(ImageController.class);
@RequestMapping(value="/image_get/{id}", method = RequestMethod.GET)
public void getImage(@PathVariable String id, 
        HttpServletRequest request, 
        HttpServletResponse response, 
        @RequestHeader String referer) {
    //referer記錄了該請求是從哪個鏈接過來的,可以用來判斷請求是否合法
    if (referer != null) {
        System.out.println(referer);
        String imageDirectory = request.getServletContext().
                getRealPath("/WEB-INF/image");
        File file = new File(imageDirectory, 
                id + ".jpg");
        if (file.exists()) {
            response.setContentType("image/jpg");
            //不設置Content-Disposition的,圖像在瀏覽器內打卡
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            // if you're using Java 7, use try-with-resources
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
            } catch (IOException ex) {
                // do something here
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {                        
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {                        
                    }
                }
            }
        }
    }
}
}

html文件

結果

直接請求html文件,html里面的img標簽的src記錄了請求地址,發出請求,后臺控制,將圖片的內容輸出到瀏覽器上

以上就是動力節點小編介紹的"使用SpringMVC文件下載功能",希望對大家有幫助,想了解更多可查看SpringMVC教程。動力節點在線學習教程,針對沒有任何Java基礎的讀者學習,讓你從入門到精通,主要介紹了一些Java基礎的核心知識,讓同學們更好更方便的學習和了解Java編程,感興趣的同學可以關注一下。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产片一区二区三区 | 91正在播放极品白嫩在线观看 | 在线看一区二区 | 国产午夜视频在线观看第四页 | 欧美香蕉在线 | 99er这里只有精品 | 亚洲天堂国产精品 | 91在线手机精品免费观看 | 日韩在线视频不卡 | 国产美女免费国产 | 狠狠狠色丁香婷婷综合久久五月 | 91久久九九精品国产综合 | 福利视频久久 | 日本精品一区二区三区在线 | 国产一区视频在线免费观看 | 国产欧美另类久久精品91 | 神马啪啪 | 亚洲精品国产福利一区二区三区 | 日韩三级| 不卡免费在线视频 | 青草视频在线观看免费资源 | 天天爆操 | 亚洲免费在线视频播放 | 亚洲国产一区二区三区 | 99热久久精里都是精品6 | 欧美日韩综合在线视频免费看 | 亚洲精品视频二区 | 99婷婷| 免费看日韩欧美一级毛片 | 99久久精品久久久久久清纯 | 日韩亚洲欧美在线观看 | 在线xxxx | 五月婷婷综合激情 | 一级特黄aa大片欧美小说 | 青草免费免费观看视频在线 | 美女视频很黄很暴黄是免费的 | 成人网影 | 免费成人高清视频 | 五月天中文字幕 | 久久久久久久一精品 | www.色黄|