tomcat中显示本地图片①(已解决)

解决方案

我直接放源码了。

实际就是我虽然调用的是虚拟目录,但是会将路径转到实际的文件目录中去

第一步:(在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";
    }

 (完成!!!)

猜你喜欢

转载自www.cnblogs.com/liujinhua1221/p/9291362.html