欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式

 更新時(shí)間:2021年11月12日 11:26:40   作者:Architect_csdn  
這篇文章主要介紹了Java實(shí)現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

將圖片上傳到webapp路徑下 路徑獲取方式

此方法獲取到工程webapp文件夾下

String contexPath= request.getSession().getServletContext().getRealPath("/");

獲取IP地址端口號(hào)以及項(xiàng)目名稱

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

spring java 獲取webapp下文件路徑

   @RequestMapping("/act/worldcup_schedule_time/imgdownload")
    @ResponseBody
    public String scheduleDownload(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        response.setCharacterEncoding("UTF-8");
        String downLoadName = "worldcup.jpg";
        InputStream input = null;
        try {
            request.setCharacterEncoding("UTF-8");
            //獲取文件的路徑
//            String url = session.getServletContext().getRealPath("/") + "resources\\images\\act\\worldcup_merge\\worldcup720.png";
            String url = session.getServletContext().getRealPath("/") + "resources/images/act/worldcup_merge/worldcup720.png";
            System.out.println(url);
            File file = new File(url); 
 
            input = FileUtils.openInputStream(file);
            byte[] data = IOUtils.toByteArray(input); 
 
            //System.out.println("文件名:"+downLoadName);
            response.reset();
            //設(shè)置響應(yīng)的報(bào)頭信息(中文問題解決辦法)
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode(downLoadName, "UTF-8"));
            response.addHeader("Content-Length", "" + data.length);
            response.setContentType("image/png; charset=UTF-8"); 
 
            IOUtils.write(data, response.getOutputStream());
        } catch (Exception e) {
            logger.error("下載圖片出錯(cuò)");
            if (input != null) {
                IOUtils.closeQuietly(input);
            }
        }
        return null;
    }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論