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

作者:Architect_csdn 时间:2023-07-10 12:44:13 

将图片上传到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;
   }

来源:https://blog.csdn.net/Architect_CSDN/article/details/89222872

标签:Java,图片上传,webapp,路径
0
投稿

猜你喜欢

  • Android 高德地图之poi搜索功能的实现代码

    2022-03-07 01:36:52
  • Spring Cache简单介绍和使用大全

    2023-11-25 04:46:46
  • Unity实现鼠标或者手指点击模型播放动画

    2023-11-30 23:06:01
  • Java使用arthas修改日志级别详解

    2023-02-04 23:09:26
  • Android禁止EditText自动弹出软键盘的方法及遇到问题

    2021-07-18 06:35:55
  • C#单例模式(Singleton Pattern)详解

    2021-12-30 05:55:03
  • Java class文件格式之数据类型(二)_动力节点Java学院整理

    2022-06-26 02:15:45
  • Android实现页面跳转的全过程记录

    2023-08-16 21:06:08
  • Java应用开源框架实现简易web搜索引擎

    2023-08-22 20:20:54
  • Android自定义弹出框dialog效果

    2023-06-29 14:14:32
  • java返回集合为null还是空集合及空集合的三种写法小结

    2021-08-18 05:37:48
  • 详解docker镜像centos7配置Java运行环境

    2022-03-14 04:44:37
  • java 实现截取字符串并按字节分别输出实例代码

    2021-08-28 08:10:44
  • 利用json2POJO with Lombok 插件自动生成java类的操作

    2023-07-12 09:31:28
  • Android仿美团淘宝实现多级下拉列表菜单功能

    2022-07-24 18:42:18
  • java 中死锁问题的实例详解

    2022-08-18 02:22:39
  • c# 服务器上传木马监控代码(包含可疑文件)

    2022-07-08 10:02:45
  • Unity实现全屏截图以及QQ截图

    2023-07-25 17:12:22
  • C#的十种语法糖介绍

    2022-07-24 04:55:49
  • c#设计模式之单例模式的实现方式

    2021-12-30 19:46:33
  • asp之家 软件编程 m.aspxhome.com