解决方案

我直接放源码了。

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

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

虚拟目录创建方式:

 <Context docBase="E:\liujinhua\img" path="/statics/uploadfiles" reloadable="true"/>
docBase:文件实际存储路径
path:Tomcat中虚拟路径
 <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <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"/>

       <!-- <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"/> -->
       <Context docBase="E:\liujinhua\img" path="/statics/uploadfiles" reloadable="true"/>
       <Context docBase="smbms-liujinhua2" path="/smbms-liujinhua" reloadable="true" source="org.eclipse.jst.jee.server:smbms-liujinhua2"/></Host>

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

/**
     * 实现供应商添加
     *
     * @param provider
     * @param session
     * @return
     */
    @RequestMapping(value = "/addProviderSava.html", method = RequestMethod.POST)
    public String addProviderSava(Provider provider, HttpSession session, HttpServletRequest request,
            @RequestParam(value = "attachs", required = false) MultipartFile[] attachs) {
        String companyLicPicPath = null;// 文件路径
        String orgCodePicPath = null;
        String errorInfo = null;
        boolean flag = true;
        //String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
        String path="E:\\liujinhua\\img";
        logger.info("uploadFile path ======================>" + path);
        for (int i = 0; i < attachs.length; i++) {
            MultipartFile attach = attachs[i];

            if (!attach.isEmpty()) {// 判断文件是否为空,不为空则上传
                if (i == 0) {
                    errorInfo = "uploadFileError";
                } else if (i == 1) {
                    errorInfo = "uploadOcError";
                }
                String oldFileName = attach.getOriginalFilename();// 原文件名
                logger.info("uploadFile oldFileName ===================>" + oldFileName);
                String prefix = FilenameUtils.getExtension(oldFileName);// 原文件后缀
                logger.debug("uploadFile profix================>" + prefix);
                int filesize = 500000;
                logger.debug("uploadFile size===================>" + attach.getSize());
                if (attach.getSize() > filesize) {// 上传大小不得超过500KB
                    request.setAttribute(errorInfo, "*上传大小不得超过 500KB");
                    return "useradd";
                } else if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                        || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")) {// 上传图片格式不正确
                    String fileName = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "_Personal.jpg";// 重新定义的文件名
                    logger.debug("new fileName==========" + attach.getName());
                    File targetFile = new File(path, fileName);//将path(实际存储路径)放入文件类中if (!targetFile.exists()) {
                        targetFile.mkdirs();
                    }
                    // 保存
                    try {
                        attach.transferTo(targetFile);
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute(errorInfo, "*上传失败!");
                        flag = false;
                    }
                    if (i == 0) {
//                        companyLicPicPath = path + File.separator + fileName;
                        companyLicPicPath = "/statics/uploadfiles/" + fileName;
                    } else if (i == 1) {
//                        orgCodePicPath = path + File.separator + fileName;
                        orgCodePicPath = "/statics/uploadfiles/" + fileName;//此处使用虚拟路径加文件名
                    }
                    logger.debug("companyLicPicPath:" + companyLicPicPath);
                    logger.debug("orgCodePicPath:" + orgCodePicPath);
                } else {
                    request.setAttribute(errorInfo, "* 上传图片格式不正确");
                    flag = false;
                }
            }
        }
        if (flag) {
            provider.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());// 创建人
            logger.debug("创建人----》" + ((User) session.getAttribute(Constants.USER_SESSION)).getId());
            provider.setCreationDate(new Date());// 创建时间
            provider.setCompanyLicPicPath(companyLicPicPath);
            provider.setOrgCodePicPath(orgCodePicPath);
            if (providerService.add(provider)) {
                return "redirect:/provider/providerlist.html";
            }
        }
        return "provideradd";
    }

(完成!!!)

最新文章

  1. 如何在elasticsearch中查看Logstash打到elasticsearch的数据
  2. Android Studio 简单功能介绍
  3. [转]Rapid Reporter——轻量级ET测试记录工具
  4. instanceof、==号、Objetc类
  5. [CTO]创业团队CTO应具备的素质
  6. nova service-list
  7. EXCEL 建立工作薄与工作表
  8. Java基础知识强化之IO流笔记30:字节流4种方式复制mp4并测试效率
  9. Shell工具:jsondiff.sh
  10. 2013年全球IT公司市值排行榜
  11. MFC使用SQLite 学习系列 一: SQLITE_MISUSE错误
  12. 实现基于Keepalived高可用集群网站架构的多种方法
  13. bzoj 3242: [Noi2013]快餐店
  14. LOJ6436 [PKUSC2018] 神仙的游戏 【FFT】
  15. MYSQL数据库字母数字混合字段排序问题
  16. Django中使用mysql数据库并使用原生sql语句操作
  17. C++指针一
  18. Log4Net使用指南之用log4net记录日志到数据库(含有自定义属性)------附Demo例子源代码
  19. WiX: uninstall older version of the application
  20. 表访问方式----&gt;通过ROWID访问表(table access by ROWID)

热门文章

  1. .net EF之CodeFirst代码先行(转)
  2. 用html5实现音频播放器
  3. Hadoop集群故障诊断
  4. 关于信号打断正在读取终端的read与select来监视0文件描述符的问题
  5. Unityd外发光Shader Lab
  6. 【Spark-SQL学习之三】 UDF、UDAF、开窗函数
  7. 20175208 《Java程序设计》第六周学习总结
  8. Appium(一)---环境搭建的一些问题
  9. window 10 删除带有管理员权限的Oracle文件夹
  10. 常用VIM插件配置