Commit 817deae4296e6a3842d19409725474d88cffe3ab
1 parent
86c4c18a
添加通用上传方法,添加websocket
Showing
11 changed files
with
257 additions
and
103 deletions
pom.xml
... | ... | @@ -65,6 +65,12 @@ |
65 | 65 | <artifactId>spring-boot-starter-undertow</artifactId> |
66 | 66 | </dependency> |
67 | 67 | |
68 | + <dependency> | |
69 | + <groupId>org.springframework.boot</groupId> | |
70 | + <artifactId>spring-boot-starter-websocket</artifactId> | |
71 | + <version>2.1.3.RELEASE</version> | |
72 | + </dependency> | |
73 | + | |
68 | 74 | <!-- SpringBoot 测试 --> |
69 | 75 | <dependency> |
70 | 76 | <groupId>org.springframework.boot</groupId> |
... | ... |
src/main/java/com/huaheng/common/config/ServerConfig.java
0 → 100644
1 | +package com.huaheng.common.config; | |
2 | + | |
3 | +import com.huaheng.common.utils.ServletUtils; | |
4 | +import org.springframework.stereotype.Component; | |
5 | + | |
6 | +import javax.servlet.http.HttpServletRequest; | |
7 | + | |
8 | +/** | |
9 | + * 服务相关配置 | |
10 | + */ | |
11 | +@Component | |
12 | +public class ServerConfig { | |
13 | + | |
14 | + /** | |
15 | + * 获取完整的请求路径,包括:域名,端口,上下文访问路径 | |
16 | + * | |
17 | + * @return 服务地址 | |
18 | + */ | |
19 | + public String getUrl() { | |
20 | + HttpServletRequest request = ServletUtils.getRequest(); | |
21 | + return getDomain(request); | |
22 | + } | |
23 | + | |
24 | + public static String getDomain(HttpServletRequest request) { | |
25 | + StringBuffer url = request.getRequestURL(); | |
26 | + String contextPath = request.getServletContext().getContextPath(); | |
27 | + return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString(); | |
28 | + } | |
29 | +} | |
... | ... |
src/main/java/com/huaheng/framework/config/CustomizationBean.java
0 → 100644
1 | +package com.huaheng.framework.config; | |
2 | + | |
3 | +import io.undertow.server.DefaultByteBufferPool; | |
4 | +import io.undertow.websockets.jsr.WebSocketDeploymentInfo; | |
5 | +import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory; | |
6 | +import org.springframework.boot.web.server.WebServerFactoryCustomizer; | |
7 | +import org.springframework.stereotype.Component; | |
8 | + | |
9 | +/** | |
10 | + * @author mahua | |
11 | + */ | |
12 | +@Component | |
13 | +public class CustomizationBean implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> { | |
14 | + | |
15 | + @Override | |
16 | + public void customize(UndertowServletWebServerFactory factory) { | |
17 | + factory.addDeploymentInfoCustomizers(deploymentInfo -> { | |
18 | + WebSocketDeploymentInfo webSocketDeploymentInfo = new WebSocketDeploymentInfo(); | |
19 | + webSocketDeploymentInfo.setBuffers(new DefaultByteBufferPool(false, 1024)); | |
20 | + deploymentInfo.addServletContextAttribute("io.undertow.websockets.jsr.WebSocketDeploymentInfo", webSocketDeploymentInfo); | |
21 | + }); | |
22 | + } | |
23 | +} | |
... | ... |
src/main/java/com/huaheng/framework/config/WebSocketConfig.java
0 → 100644
1 | +package com.huaheng.framework.config; | |
2 | + | |
3 | +import org.springframework.context.annotation.Bean; | |
4 | +import org.springframework.context.annotation.Configuration; | |
5 | +import org.springframework.web.socket.server.standard.ServerEndpointExporter; | |
6 | + | |
7 | +/** | |
8 | + * webSocket配置 | |
9 | + * @author mahua | |
10 | + */ | |
11 | +@Configuration | |
12 | +public class WebSocketConfig { | |
13 | + | |
14 | + @Bean | |
15 | + public ServerEndpointExporter serverEndpointExporter() { | |
16 | + return new ServerEndpointExporter(); | |
17 | + } | |
18 | +} | |
... | ... |
src/main/java/com/huaheng/framework/web/service/WebSocketServer.java
0 → 100644
1 | +package com.huaheng.framework.web.service; | |
2 | + | |
3 | +import com.alibaba.fastjson.JSON; | |
4 | +import com.alibaba.fastjson.JSONObject; | |
5 | +import org.apache.commons.lang.StringUtils; | |
6 | +import org.springframework.stereotype.Component; | |
7 | + | |
8 | +import javax.websocket.*; | |
9 | +import javax.websocket.server.PathParam; | |
10 | +import javax.websocket.server.ServerEndpoint; | |
11 | +import java.io.IOException; | |
12 | +import java.util.concurrent.ConcurrentHashMap; | |
13 | + | |
14 | +/** | |
15 | + * WebSocket服务 | |
16 | + * @author mahua | |
17 | + * @date 2020/5/17 | |
18 | + */ | |
19 | +@ServerEndpoint("/imserver/{userId}") | |
20 | +@Component | |
21 | +public class WebSocketServer { | |
22 | + | |
23 | + /**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/ | |
24 | + private static int onlineCount = 0; | |
25 | + /**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/ | |
26 | + private static ConcurrentHashMap<String,WebSocketServer> webSocketMap = new ConcurrentHashMap<>(); | |
27 | + /**与某个客户端的连接会话,需要通过它来给客户端发送数据*/ | |
28 | + private Session session; | |
29 | + /**接收userId*/ | |
30 | + private String userId=""; | |
31 | + | |
32 | + /** | |
33 | + * 连接建立成功调用的方法*/ | |
34 | + @OnOpen | |
35 | + public void onOpen(Session session,@PathParam("userId") String userId) { | |
36 | + this.session = session; | |
37 | + this.userId=userId; | |
38 | + if(webSocketMap.containsKey(userId)){ | |
39 | + webSocketMap.remove(userId); | |
40 | + webSocketMap.put(userId,this); | |
41 | + //加入set中 | |
42 | + }else{ | |
43 | + webSocketMap.put(userId,this); | |
44 | + //加入set中 | |
45 | + addOnlineCount(); | |
46 | + //在线数加1 | |
47 | + } | |
48 | + | |
49 | + System.out.println("用户连接:"+userId+",当前在线人数为:" + getOnlineCount()); | |
50 | + try { | |
51 | + sendMessage("连接成功"); | |
52 | + } catch (IOException e) { | |
53 | + System.out.println("用户:"+userId+",网络异常!!!!!!"); | |
54 | + } | |
55 | + } | |
56 | + | |
57 | + /** | |
58 | + * 连接关闭调用的方法 | |
59 | + */ | |
60 | + @OnClose | |
61 | + public void onClose() { | |
62 | + if(webSocketMap.containsKey(userId)){ | |
63 | + webSocketMap.remove(userId); | |
64 | + //从set中删除 | |
65 | + subOnlineCount(); | |
66 | + } | |
67 | + System.out.println("用户退出:"+userId+",当前在线人数为:" + getOnlineCount()); | |
68 | + } | |
69 | + | |
70 | + /** | |
71 | + * 收到客户端消息后调用的方法 | |
72 | + * | |
73 | + * @param message 客户端发送过来的消息*/ | |
74 | + @OnMessage | |
75 | + public void onMessage(String message, Session session) { | |
76 | + //可以群发消息 | |
77 | + //消息保存到数据库、redis | |
78 | + if(StringUtils.isNotBlank(message)){ | |
79 | + try { | |
80 | + //解析发送的报文 | |
81 | + JSONObject jsonObject = JSON.parseObject(message); | |
82 | + //追加发送人(防止串改) | |
83 | + jsonObject.put("fromUserId",this.userId); | |
84 | + String toUserId=jsonObject.getString("toUserId"); | |
85 | + //传送给对应toUserId用户的websocket | |
86 | + if(StringUtils.isNotBlank(toUserId)&&webSocketMap.containsKey(toUserId)){ | |
87 | + webSocketMap.get(toUserId).sendMessage(jsonObject.toJSONString()); | |
88 | + }else{ | |
89 | + System.out.println("请求的userId:"+toUserId+"不在该服务器上"); | |
90 | + //否则不在这个服务器上,发送到mysql或者redis | |
91 | + } | |
92 | + }catch (Exception e){ | |
93 | + e.printStackTrace(); | |
94 | + } | |
95 | + } | |
96 | + } | |
97 | + | |
98 | + /** | |
99 | + * | |
100 | + * @param session | |
101 | + * @param error | |
102 | + */ | |
103 | + @OnError | |
104 | + public void onError(Session session, Throwable error) { | |
105 | + System.out.println("用户错误:"+this.userId+",原因:"+error.getMessage()); | |
106 | + error.printStackTrace(); | |
107 | + } | |
108 | + | |
109 | + /** | |
110 | + * 实现服务器主动推送 | |
111 | + */ | |
112 | + public void sendMessage(String message) throws IOException { | |
113 | + this.session.getBasicRemote().sendText(message); | |
114 | + } | |
115 | + | |
116 | + | |
117 | + /** | |
118 | + * 发送自定义消息 | |
119 | + * */ | |
120 | + public static void sendInfo(String message,@PathParam("userId") String userId) throws IOException { | |
121 | + System.out.println("发送消息到:"+userId+",报文:"+message); | |
122 | + if(StringUtils.isNotBlank(userId)&&webSocketMap.containsKey(userId)){ | |
123 | + webSocketMap.get(userId).sendMessage(message); | |
124 | + }else{ | |
125 | + System.out.println("发送消息到:"+userId+",报文:"+message); | |
126 | + } | |
127 | + } | |
128 | + | |
129 | + public static synchronized int getOnlineCount() { | |
130 | + return onlineCount; | |
131 | + } | |
132 | + | |
133 | + public static synchronized void addOnlineCount() { | |
134 | + WebSocketServer.onlineCount++; | |
135 | + } | |
136 | + | |
137 | + public static synchronized void subOnlineCount() { | |
138 | + WebSocketServer.onlineCount--; | |
139 | + } | |
140 | + | |
141 | +} | |
... | ... |
src/main/java/com/huaheng/pc/common/CommonController.java
... | ... | @@ -3,6 +3,9 @@ package com.huaheng.pc.common; |
3 | 3 | import java.io.IOException; |
4 | 4 | import java.io.UnsupportedEncodingException; |
5 | 5 | import java.net.URLEncoder; |
6 | +import java.util.HashMap; | |
7 | +import java.util.Map; | |
8 | +import javax.annotation.Resource; | |
6 | 9 | import javax.servlet.http.HttpServletRequest; |
7 | 10 | import javax.servlet.http.HttpServletResponse; |
8 | 11 | |
... | ... | @@ -10,19 +13,22 @@ import com.huaheng.common.config.Global; |
10 | 13 | |
11 | 14 | import com.google.zxing.WriterException; |
12 | 15 | import com.huaheng.common.config.Global; |
16 | +import com.huaheng.common.config.ServerConfig; | |
13 | 17 | import com.huaheng.common.utils.QRCodeGenerator; |
18 | +import com.huaheng.common.utils.file.FileUploadUtils; | |
19 | +import com.huaheng.framework.web.domain.AjaxResult; | |
14 | 20 | import org.slf4j.Logger; |
15 | 21 | import org.slf4j.LoggerFactory; |
22 | +import org.springframework.beans.factory.annotation.Autowired; | |
16 | 23 | import org.springframework.http.HttpHeaders; |
17 | 24 | import org.springframework.http.HttpStatus; |
18 | 25 | import org.springframework.http.MediaType; |
19 | 26 | import org.springframework.http.ResponseEntity; |
20 | 27 | import org.springframework.stereotype.Controller; |
21 | 28 | import org.springframework.util.ResourceUtils; |
22 | -import org.springframework.web.bind.annotation.GetMapping; | |
23 | -import org.springframework.web.bind.annotation.PathVariable; | |
24 | -import org.springframework.web.bind.annotation.RequestMapping; | |
29 | +import org.springframework.web.bind.annotation.*; | |
25 | 30 | import com.huaheng.common.utils.file.FileUtils; |
31 | +import org.springframework.web.multipart.MultipartFile; | |
26 | 32 | |
27 | 33 | /** |
28 | 34 | * 通用请求处理 |
... | ... | @@ -34,6 +40,9 @@ public class CommonController |
34 | 40 | { |
35 | 41 | private static final Logger log = LoggerFactory.getLogger(CommonController.class); |
36 | 42 | |
43 | + @Resource | |
44 | + private ServerConfig serverConfig; | |
45 | + | |
37 | 46 | @RequestMapping("common/download") |
38 | 47 | public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) { |
39 | 48 | |
... | ... | @@ -100,4 +109,32 @@ public class CommonController |
100 | 109 | headers.setContentType(MediaType.IMAGE_PNG); |
101 | 110 | return new ResponseEntity<byte[]>(qrcode, headers, HttpStatus.CREATED); |
102 | 111 | } |
112 | + | |
113 | + /** | |
114 | + * 通用上传请求 | |
115 | + */ | |
116 | + @PostMapping("/common/upload") | |
117 | + @ResponseBody | |
118 | + public AjaxResult uploadFile(MultipartFile file) throws Exception | |
119 | + { | |
120 | + try | |
121 | + { | |
122 | + // 上传文件路径 | |
123 | + String filePath = "D:/Huaheng/uploadPath/"; | |
124 | + // 上传并返回新文件名称 | |
125 | + String fileName = FileUploadUtils.upload(filePath, file); | |
126 | + String url = serverConfig.getUrl() + "/" + fileName; | |
127 | + AjaxResult ajax = AjaxResult.success(); | |
128 | + Map<String, Object> map = new HashMap<>(); | |
129 | + | |
130 | + map.put("fileName", fileName); | |
131 | + map.put("url", url); | |
132 | + ajax.setData(map); | |
133 | + return ajax; | |
134 | + } | |
135 | + catch (Exception e) | |
136 | + { | |
137 | + return AjaxResult.error(e.getMessage()); | |
138 | + } | |
139 | + } | |
103 | 140 | } |
... | ... |
src/main/java/com/huaheng/pc/config/transfer/controller/TransferController.java deleted
1 | -package com.huaheng.pc.config.transfer.controller; | |
2 | - | |
3 | - | |
4 | -import org.springframework.stereotype.Controller; | |
5 | -import org.springframework.web.bind.annotation.RequestMapping; | |
6 | - | |
7 | - | |
8 | -/** | |
9 | - * 移库配置 | |
10 | - * 配置闲时时间段, | |
11 | - * 闲时移库热门物料到靠近出口的位置 | |
12 | - * | |
13 | - *需要首先收集热门物料的信息。 | |
14 | - * 或者查询最近一根约的出入单,确定物料热度 | |
15 | - * | |
16 | - * | |
17 | - * */ | |
18 | -@Controller | |
19 | -@RequestMapping("/config/transfer") | |
20 | -public class TransferController { | |
21 | - | |
22 | - | |
23 | - | |
24 | - | |
25 | - | |
26 | - | |
27 | -} |
src/main/java/com/huaheng/pc/config/transfer/domain/Transfer.java deleted
1 | -package com.huaheng.pc.config.transfer.domain; | |
2 | - | |
3 | - | |
4 | -import com.baomidou.mybatisplus.annotation.TableName; | |
5 | -import lombok.Data; | |
6 | - | |
7 | -import java.io.Serializable; | |
8 | - | |
9 | -@Data | |
10 | -//@TableName(value = "") | |
11 | -public class Transfer implements Serializable { | |
12 | - | |
13 | - | |
14 | - | |
15 | - | |
16 | - | |
17 | - | |
18 | - | |
19 | -} |
src/main/java/com/huaheng/pc/config/transfer/mapper/TransferMapper.java deleted
src/main/java/com/huaheng/pc/config/transfer/service/TransferService.java deleted
src/main/java/com/huaheng/pc/config/transfer/service/TransferServiceImpl.java deleted
1 | -package com.huaheng.pc.config.transfer.service; | |
2 | - | |
3 | -import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |
4 | -import com.huaheng.pc.config.transfer.domain.Transfer; | |
5 | -import com.huaheng.pc.config.transfer.mapper.TransferMapper; | |
6 | -import org.springframework.stereotype.Service; | |
7 | - | |
8 | - | |
9 | -/** | |
10 | - * 移库配置 | |
11 | - * */ | |
12 | -@Service("TransferService") | |
13 | -public class TransferServiceImpl extends ServiceImpl<TransferMapper, Transfer> implements TransferService { | |
14 | - | |
15 | - | |
16 | - | |
17 | - | |
18 | - | |
19 | - | |
20 | - | |
21 | - | |
22 | - | |
23 | - | |
24 | - | |
25 | - | |
26 | - | |
27 | - | |
28 | - | |
29 | - | |
30 | -} |