解决方案

我直接放源码了。

原理就是:我虽然调用的是虚拟目录,但是会映射到对应路径的实际

第一步:(在tomcat的 server.xml中创建一个虚拟目录)

虚拟目录创建方式:

  1. <Context docBase="E:\liujinhua\img" path="/statics/uploadfiles" reloadable="true"/>
  1. docBase:文件实际存储路径
  1. pathTomcat中虚拟路径
  1. <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
  2.  
  3. <!-- SingleSignOn valve, share authentication between web applications
  4. Documentation at: /docs/config/valve.html -->
  5. <!--
  6. <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
  7. -->
  8.  
  9. <!-- Access log processes all example.
  10. Documentation at: /docs/config/valve.html
  11. Note: The pattern used is equivalent to using pattern="common" -->
  12. <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>
  13.  
  14. <!-- <Context docBase="E:/青鸟学习/SSM/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/" path="/smbms-liujinhua2/statics/uploadfiles/" reloadable="true" source="org.eclipse.jst.jee.server:smbms-liujinhua2"/> -->
  15. <Context docBase="E:\liujinhua\img" path="/statics/uploadfiles" reloadable="true"/>
  16. <Context docBase="smbms-liujinhua2" path="/smbms-liujinhua" reloadable="true" source="org.eclipse.jst.jee.server:smbms-liujinhua2"/></Host>

第二步:(改写存进数据库的路径。)

  1. /**
  2. * 实现供应商添加
  3. *
  4. * @param provider
  5. * @param session
  6. * @return
  7. */
  8. @RequestMapping(value = "/addProviderSava.html", method = RequestMethod.POST)
  9. public String addProviderSava(Provider provider, HttpSession session, HttpServletRequest request,
  10. @RequestParam(value = "attachs", required = false) MultipartFile[] attachs) {
  11. String companyLicPicPath = null;// 文件路径
  12. String orgCodePicPath = null;
  13. String errorInfo = null;
  14. boolean flag = true;
  15. //String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
  16. String path="E:\\liujinhua\\img";
  17. logger.info("uploadFile path ======================>" + path);
  18. for (int i = 0; i < attachs.length; i++) {
  19. MultipartFile attach = attachs[i];
  20.  
  21. if (!attach.isEmpty()) {// 判断文件是否为空,不为空则上传
  22. if (i == 0) {
  23. errorInfo = "uploadFileError";
  24. } else if (i == 1) {
  25. errorInfo = "uploadOcError";
  26. }
  27. String oldFileName = attach.getOriginalFilename();// 原文件名
  28. logger.info("uploadFile oldFileName ===================>" + oldFileName);
  29. String prefix = FilenameUtils.getExtension(oldFileName);// 原文件后缀
  30. logger.debug("uploadFile profix================>" + prefix);
  31. int filesize = 500000;
  32. logger.debug("uploadFile size===================>" + attach.getSize());
  33. if (attach.getSize() > filesize) {// 上传大小不得超过500KB
  34. request.setAttribute(errorInfo, "*上传大小不得超过 500KB");
  35. return "useradd";
  36. } else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
  37. || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")) {// 上传图片格式不正确
  38. String fileName = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "_Personal.jpg";// 重新定义的文件名
  39. logger.debug("new fileName==========" + attach.getName());
  40. File targetFile = new File(path, fileName);//将path(实际存储路径)放入文件类中if (!targetFile.exists()) {
  41. targetFile.mkdirs();
  42. }
  43. // 保存
  44. try {
  45. attach.transferTo(targetFile);
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. request.setAttribute(errorInfo, "*上传失败!");
  49. flag = false;
  50. }
  51. if (i == 0) {
  52. // companyLicPicPath = path + File.separator + fileName;
  53. companyLicPicPath = "/statics/uploadfiles/" + fileName;
  54. } else if (i == 1) {
  55. // orgCodePicPath = path + File.separator + fileName;
  56. orgCodePicPath = "/statics/uploadfiles/" + fileName;//此处使用虚拟路径加文件名
  57. }
  58. logger.debug("companyLicPicPath:" + companyLicPicPath);
  59. logger.debug("orgCodePicPath:" + orgCodePicPath);
  60. } else {
  61. request.setAttribute(errorInfo, "* 上传图片格式不正确");
  62. flag = false;
  63. }
  64. }
  65. }
  66. if (flag) {
  67. provider.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());// 创建人
  68. logger.debug("创建人----》" + ((User) session.getAttribute(Constants.USER_SESSION)).getId());
  69. provider.setCreationDate(new Date());// 创建时间
  70. provider.setCompanyLicPicPath(companyLicPicPath);
  71. provider.setOrgCodePicPath(orgCodePicPath);
  72. if (providerService.add(provider)) {
  73. return "redirect:/provider/providerlist.html";
  74. }
  75. }
  76. return "provideradd";
  77. }

(完成!!!)

tomcat中显示本地图片①(已解决)的更多相关文章

  1. tomcat中显示本地图片①(未解决)

    <本模块文仅作为学习过程中的自我总结,有需要可参看,欢迎指导与提出建议,很多地方可能断章取义,理解不到位,虚心求学.谢谢!> 资料查阅原因:2018/7/10(做项目中显示详情页面,从数据 ...

  2. 在InternetExplorer.Application中显示本地图片

    忘记了,喜欢一个人的感觉 Demon's Blog  »  程序设计  »  在InternetExplorer.Application中显示本地图片 « 对VBS效率的再思考——处理二进制数据 Wo ...

  3. Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/1873 ...

  4. Slog71_选取、上传和显示本地图片GET !(微信小程序之云开发-全栈时代3)

    ArthurSlog SLog-71 Year·1 Guangzhou·China Sep 12th 2018 ArthurSlog Page GitHub NPM Package Page 掘金主页 ...

  5. Android ImageView显示本地图片

    Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...

  6. Android 使用开源库StickyGridHeaders来实现带sections和headers的GridView显示本地图片效果

    大家好!过完年回来到现在差不多一个月没写文章了,一是觉得不知道写哪些方面的文章,没有好的题材来写,二是因为自己的一些私事给耽误了,所以过完年的第一篇文章到现在才发表出来,2014年我还是会继续在CSD ...

  7. Atitit. html 使用js显示本地图片的设计方案.doc

    Atitit. html 使用js显示本地图片的设计方案.doc 1.  Local mode  是可以的..web模式走有的不能兰.1 2. IE8.0 显示本地图片 img.src=本地图片路径无 ...

  8. Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案

    Atitit. IE8.0 显示本地图片预览解决方案 img.src=本地图片路径无效的解决方案 1. IE8.0 显示本地图片 img.src=本地图片路径无效的解决方案1 1.1. div来完成  ...

  9. Typora笔记上传到播客时图片不显示问题解决(已解决)

    前言: ​ 相信我们都遇到过,使用Typora做笔记是一件非常令人舒服的事,然而,它却有一个非常难受的地方,那就是我们在做完笔记想要将其上传到自己的博客时,复制粘贴的图片无法显示.因为Typora复制 ...

随机推荐

  1. Django---请求、响应

    Django的请求和响应 一.客户端向服务器的请求简介 二.响应对象构造 回到顶部 一.客户端向服务器的请求简介 1.c-->s的传参的4中途径 提取的URL的特定部分,如/天气/北京/ 201 ...

  2. python tkinter messagebox

    """messagebox消息框""" import tkinter as tk #导入messagebox import tkinter. ...

  3. windows共享文件夹至centos系统文件夹下

    1. window 共享文件夹 https://jingyan.baidu.com/article/358570f6633ba4ce4624fc48.html 2. 在访问Windows共享资料之前, ...

  4. 接口测试工具-fiddler

    1.fiddler拦截修改数据 命令介绍: bpu在请求开始时中断,bpafter在响应到达时中断,bps在特定http状态码时中断,bpv/bpm在特定请求method时中断. 提示:命令输入区域输 ...

  5. python编写接口初识一

    python编写接口这里用到的是他一个比较轻量级的框架 flask #!/usr/bin/python # -*- coding: UTF-8 -*- import flask,json server ...

  6. Backup &recovery备份和还原

    实践版本:MySQL5.7 备份类型(backup type)物理和逻辑备份(Physical Versus Logical Backup)        物理备份是指直接复制存储数据库内容的目录和文 ...

  7. Redis详解与常见问题解决方案

    Redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sor ...

  8. asyncio之Coroutines,Tasks and Future

    asyncio之Coroutines,Tasks and Future Coroutines and Tasks属于High-level APIs,也就是高级层的api. 本节概述用于协程和任务的高级 ...

  9. C# Json处理相关

    最近工作中遇到的Json问题确实很头大,主要是各种转义符的处理,想了一种通用的方式,来处理任意转移方式的Json字符串: /// <summary> /// 去除返回值中的转义符,返回js ...

  10. vue进阶--外卖商家页

    一.准备工作 1.vue特性:轻量级.简洁.高效.组件化.数据驱动 2.技术分析:使用vue- resource与后端交互(ajax通信,ie9+)  使用vue-router作为前端路由   bet ...