大战熟女丰满人妻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編程,感興趣的同學可以關注一下。

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

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 免费黄色小视频在线观看 | 亚洲成色综合一区二区三区四区 | 一级毛片私人影院 | 亚洲网视频 | 日日摸夜夜 | 精品国产线拍大陆久久尤物 | 欧美成人精品福利在线视频 | 亚洲精品视频在线观看免费 | 国产剧情自拍 | 欧美另类jizzhd | 欧美性猛交ⅹxxx乱大交按摩 | 久操这里只有精品 | 免费观看黄色网 | m3u8久久国产精品影院 | 欧美一级毛片无遮 | 久久999精品| 日韩国产成人精品视频 | 国产永久免费高清动作片www | 久久精品国产视频 | 中文字幕视频不卡 | 一级毛片免费观看不收费 | 377p亚洲欧洲日本大胆色噜噜 | 亚洲国产精品欧美综合 | 国产二三区 | 久久精品天天中文字幕人 | 日日干日日干 | 国产小视频在线观看 | 一区二区在线免费观看 | 多色视频 | xxxxx日本59| 国产中文字幕视频在线观看 | 九色视频极品论坛区 | 成人看片黄a免费看视频 | 亚洲欧美小视频 | 国产一区二区三区播放 | 四虎影视库永久在线地址 | 色偷偷88888欧美精品久久久 | 亚洲在线成人 | 亚洲欧美日韩在线中文一 | 99综合之综合久久伊人 | 国产成人精品2021欧美日韩 |