RequestMapping处理PathVariable参数特殊字符

import org.springframework.web.servlet.HandlerMapping;    	
import java.net.URLDecoder;

/**
 * 根据路径下载(pathVariable方式不能用/\这些特殊字符,所以特殊处理一下)
 * @param req
 * @param res
 */
 @GetMapping("/download/**")
 public void downloadByPathVariable(HttpServletRequest req, HttpServletResponse res) {
     try {
		String path = (String) req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
		path = path.replace("/file/download", "");
		// 前端调用需要编码两次,处理#等特殊字符
		path = URLDecoder.decode(path, "UTF-8");
     } catch (IOException e) {
         e.printStackTrace();
     }
 }

你可能感兴趣的:(Java,Java)