| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.management.*; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.lang.management.ManagementFactory; |
| | | import java.util.Set; |
| | | |
| | | /** |
| | | * 通用请求处理 |
| | |
| | | */ |
| | | @PostMapping("/common/upload") |
| | | public AjaxResult uploadFile(MultipartFile file) throws Exception { |
| | | int tomcatPort = getTomcatPort(); |
| | | |
| | | int tomcatPort1 = getTomcatPort(); |
| | | log.info("tomcatPort1的值为: {}", tomcatPort1); |
| | | //这个省人民是8099,所以直接写死 |
| | | int tomcatPort = 8099; |
| | | try { |
| | | // 上传文件路径 |
| | | String filePath = RuoYiConfig.getUploadPath(); |
| | | // 上传并返回新文件名称 |
| | | String fileName = FileUploadUtils.upload(filePath, file); |
| | | String url = serverConfig.getUrl() + fileName; |
| | | String url1 = serverConfig.getUrl(); |
| | | log.info("url1的值old :{}", url1); |
| | | //这里的8032不是tomcat的,所以要转成tomcat的 |
| | | url1 = url1.replace("8032", "8099"); |
| | | log.info("url1的值new :{}", url1); |
| | | String url = url1 + fileName; |
| | | AjaxResult ajax = AjaxResult.success(); |
| | | ajax.put("fileName", fileName); |
| | | ajax.put("url", url); |
| | |
| | | } |
| | | } |
| | | |
| | | private static int getTomcatPort() { |
| | | int port = 0; |
| | | Tomcat tomcat = new Tomcat(); |
| | | Connector connector = tomcat.getConnector(); |
| | | if (connector != null) { |
| | | AbstractProtocol protocol = (AbstractProtocol) connector.getProtocolHandler(); |
| | | port = protocol.getPort(); |
| | | } |
| | | return port; |
| | | } |
| | | |
| | | /** |
| | | * 本地资源通用下载 |
| | |
| | | log.error("下载文件失败", e); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | public int getTomcatPort() { |
| | | MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); |
| | | try { |
| | | QueryExp protocol = Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")); |
| | | ObjectName name = new ObjectName("*:type=Connector,*"); |
| | | Set<ObjectName> objectNames = beanServer.queryNames(name, protocol); |
| | | for (ObjectName objectName : objectNames) { |
| | | String catalina = objectName.getDomain(); |
| | | if ("Catalina".equals(catalina)) { |
| | | return Integer.parseInt(objectName.getKeyProperty("port")); |
| | | } |
| | | } |
| | | } catch (MalformedObjectNameException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return 0; |
| | | } |
| | | } |