广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java实现将图片上传到webapp路径下 路径获取方式
  • 579
分享到

Java实现将图片上传到webapp路径下 路径获取方式

2024-04-02 19:04:59 579人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录将图片上传到WEBapp路径下 路径获取方式spring java 获取webapp下文件路径将图片上传到webapp路径下 路径获取方式 此方法获取到工程webapp文件夹下

将图片上传到webapp路径下 路径获取方式

此方法获取到工程webapp文件夹下


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

获取IP地址端口号以及项目名称


<%
    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();
            //设置响应的报头信息(中文问题解决办法)
            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("下载图片出错");
            if (input != null) {
                IOUtils.closeQuietly(input);
            }
        }
        return null;
    }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Java实现将图片上传到webapp路径下 路径获取方式

本文链接: https://www.lsjlt.com/news/157157.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作