tomcat configure the virtual path to upload files, pictures

A, tomcat configuration

1. Find the conf directory (tomcat / conf / service.xml) in the tomcat installation directory, modify service.xml file:

path: virtual path to / beginning;

docBase: disk path (absolute path), Windows environment with a drive letter (D: / template) start, linux environment are as follows;

reloadable: web.xml is true when there are changes or class time will automatically reload does not need to restart the service;

<Context path="/template" docBase="/home/template" reloadable="true" />

 

NOTE: The added configuration need to be placed <Host> </ Host> the tag;

We do not recommend docBase path and tomcat together, first of all because the configuration server virtual path is the want of resources, the independent resources into the project or in respect tomcat and resources into the project as, secondly increases the load on the tomcat;

2, upload

Resources need to be uploaded to docBase (absolute path) path when uploading resources under

   @RequestMapping(value = "uploadImg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Object uploadImg(MultipartFile file, HttpSession session, HttpServletRequest request) {
        SessionContainer s = (SessionContainer) session.getAttribute("sc");
        ResultVo resultVo = new ResultVo();
        resultVo.setCode(ResultEnum.ERROR.getCode());
//        String call_path = Global.getProperty("VOUCHER_IMAGE_URL");
        String call_path = "/template";
//        String path = Global.getProperty("VOUCHER_IMAGE_DIR");
        String path = "/home/template/";
        try {
            String fileName = file.getOriginalFilename();
            String date = com.ronglian.bms.commons.utils.DateUtil.getDate("yyyyMMdd");
            String rand = RandomUtil.randomNumbers(3);
            String merchNo = s.getMerchNo();
            if (StringUtils.isBlank(merchNo)) {
                merchNo = Constants.ROOT_ORG_CODE;
            }
            String lastStr = fileName.substring(fileName.lastIndexOf("."));
            path = path + merchNo + "/";
            fileName = RAND + DATE + laststr; 
            File the dir = new new File (path, fileName);
             IF (! dir.exists ()) { 
                dir.mkdirs (); 
            } 
            dir.setWritable ( to true , to false ); 
            file.transferTo (the dir ); 
            // pictures physical path 
            String phy_path = dir.getPath ();
             // Image access path 
            String filePath = call_path + "/" + merchNo + "/" + fileName; 
            logger.info ( "picture the true path:" + phy_path ); 
            logger.info ("Uploaded successfully, template path:" + filePath); 
            resultVo.setCode (ResultEnum.SUCCESS.getCode ()); 
            resultVo.setMsg (filePath); 
        } the catch (IOException E) { 
            logger.error ( "Upload Error reasons:" , E); 
        } 
        return resultVo; 
    }

filePath: Picture echo path need to use virtual path, such as: http: // ip: port / template / pictures .jpg

3, save and echo

Save the image path to the database, save two, a real picture of the physical path, a path for the virtual image, or only keep a virtual path to the picture;

Note: The virtual path configuration of tomcat is not configured in the project name, project resource access generally have the project name, project name at this time needs to be removed or re-configured virtual path when the project name can be configured into tomcat.

 

Guess you like

Origin www.cnblogs.com/fatTmonkey/p/11571967.html