提交信息
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/main/java/com/update/update/UpdateApplication.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/main/java/com/update/update/UpdateApplication.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| package com.update.update; | ||||
|  | ||||
|  | ||||
| import org.mybatis.spring.annotation.MapperScan; | ||||
| import org.springframework.boot.SpringApplication; | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
|  | ||||
| @SpringBootApplication | ||||
| @MapperScan("com.update.update.dao.freshMapper") | ||||
|  | ||||
| public class UpdateApplication { | ||||
|  | ||||
|     public static void main(String[] args) { | ||||
|         SpringApplication.run(UpdateApplication.class, args); | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										31
									
								
								src/main/java/com/update/update/bean/fresh.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/main/java/com/update/update/bean/fresh.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | ||||
| package com.update.update.bean; | ||||
|  | ||||
| public class fresh { | ||||
|     public String VersionId; | ||||
|     public String apkUrl; | ||||
|     public String updateDescription; | ||||
|  | ||||
|     public String getVersionId() { | ||||
|         return VersionId; | ||||
|     } | ||||
|  | ||||
|     public void setVersionId(String versionId) { | ||||
|         VersionId = versionId; | ||||
|     } | ||||
|  | ||||
|     public String getApkUrl() { | ||||
|         return apkUrl; | ||||
|     } | ||||
|  | ||||
|     public void setApkUrl(String apkUrl) { | ||||
|         this.apkUrl = apkUrl; | ||||
|     } | ||||
|  | ||||
|     public String getUpdateDescription() { | ||||
|         return updateDescription; | ||||
|     } | ||||
|  | ||||
|     public void setUpdateDescription(String updateDescription) { | ||||
|         this.updateDescription = updateDescription; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,67 @@ | ||||
| package com.update.update.controller; | ||||
|  | ||||
|  | ||||
| import com.update.update.bean.fresh; | ||||
| import com.update.update.util.GJson; | ||||
| import org.json.JSONException; | ||||
| import org.json.JSONObject; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.web.bind.annotation.RequestMapping; | ||||
| import org.springframework.web.bind.annotation.RequestMethod; | ||||
| import com.update.update.service.impl.freshServiceImpl; | ||||
| import com.update.update.service.impl.refreshServiceImpl; | ||||
| import com.update.update.util.ServletUtil; | ||||
| import com.update.update.util.StringUtil; | ||||
| import org.springframework.web.bind.annotation.RestController; | ||||
|  | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
|  | ||||
| @RestController | ||||
| public class freshController { | ||||
|     @Autowired | ||||
|     freshServiceImpl freshS = new freshServiceImpl(); | ||||
|  | ||||
|     @RequestMapping(value = "/add", method = RequestMethod.POST) | ||||
|     public void addVersion(HttpServletRequest request, HttpServletResponse response) throws JSONException { | ||||
|         JSONObject result = new JSONObject(); | ||||
|         String VersionId = request.getParameter("VersionId"); | ||||
|         String apkUrl = request.getParameter("apkUrl"); | ||||
|         String updateDescription = request.getParameter("updateDescription"); | ||||
|         if (StringUtil.isNull(VersionId)) { | ||||
|             result.put("message", "不能为空!"); | ||||
|             result.put("flag", false); | ||||
|             ServletUtil.createSuccessResponse(200, result, response); | ||||
|             return; | ||||
|         } | ||||
|         if (StringUtil.isNull(apkUrl)) { | ||||
|             result.put("message", "不能为空!"); | ||||
|             result.put("flag", false); | ||||
|             ServletUtil.createSuccessResponse(200, result, response); | ||||
|             return; | ||||
|         } | ||||
|         if (StringUtil.isNull(updateDescription)) { | ||||
|             result.put("message", "不能为空!"); | ||||
|             result.put("flag", false); | ||||
|             ServletUtil.createSuccessResponse(200, result, response); | ||||
|             return; | ||||
|         } | ||||
|         fresh f = new fresh(); | ||||
|         f.setVersionId(VersionId); | ||||
|         f.setApkUrl(apkUrl); | ||||
|         f.setUpdateDescription(updateDescription); | ||||
|         int index = freshS.add(f); | ||||
|         if (index > 0) { | ||||
|             result.put("message", "信息添加成功!"); | ||||
|             result.put("flag", true); | ||||
|         } else { | ||||
|             result.put("message", "信息添加失败!"); | ||||
|             result.put("flag", false); | ||||
|         } | ||||
|         ServletUtil.createSuccessResponse(200, result, response); | ||||
|     } | ||||
| @RequestMapping(value = "/updating",method = RequestMethod.POST) | ||||
|     public void updating(HttpServletRequest request, HttpServletResponse response) throws Exception { | ||||
|         ServletUtil.createSuccessResponse(200, GJson.toJson(new refreshServiceImpl().new_version()),response); | ||||
| } | ||||
| } | ||||
							
								
								
									
										29
									
								
								src/main/java/com/update/update/dao/freshMapper.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/main/java/com/update/update/dao/freshMapper.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| package com.update.update.dao; | ||||
|  | ||||
| import com.update.update.bean.fresh; | ||||
| import org.apache.ibatis.annotations.*; | ||||
|  | ||||
|  | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
|  | ||||
| @Component | ||||
| @Mapper | ||||
| public interface freshMapper { | ||||
|     @Insert("insert into fresh(VersionId,apkUrl,updateDescription) values(#{VersionId},#{apkUrl},#{updateDescription})") | ||||
|     int add(fresh learnResouce); | ||||
|  | ||||
|  | ||||
|     @DeleteProvider(type = fresh.class, method = "deleteByids") | ||||
|     int deleteByIds(@Param("ids") String[] ids); | ||||
|  | ||||
|  | ||||
|     @Select("SELECT * FROM fresh ORDER BY id DESC LIMIT 1") | ||||
|     @Results(value = { | ||||
|             @Result(column = "VersionId", property = "VersionId", javaType = String.class), | ||||
|             @Result(property = "apkUrl", column = "apkUrl", javaType = String.class), | ||||
|             @Result(property = "updateDescription", column = "updateDescription", javaType = String.class) | ||||
|     }) | ||||
|     fresh query(); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,9 @@ | ||||
| package com.update.update.service; | ||||
|  | ||||
| import com.update.update.bean.fresh; | ||||
|  | ||||
| public interface freshService { | ||||
|     int add(fresh version); | ||||
|     fresh query(); | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,25 @@ | ||||
| package com.update.update.service.impl; | ||||
|  | ||||
|  | ||||
| import com.update.update.bean.fresh; | ||||
| import com.update.update.dao.freshMapper; | ||||
| import com.update.update.service.freshService; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Service; | ||||
|  | ||||
| @Service | ||||
| public class freshServiceImpl implements freshService { | ||||
|  | ||||
|     freshMapper f; | ||||
|     @Override | ||||
|     public int add(fresh newOne) { | ||||
|         return this.f.add(newOne); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public fresh query() { | ||||
|         return this.f.query(); | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
| @@ -0,0 +1,48 @@ | ||||
| package com.update.update.service.impl; | ||||
|  | ||||
|  | ||||
| import android.app.Application; | ||||
|  | ||||
| import android.content.Context; | ||||
| import com.update.update.bean.fresh; | ||||
| import com.update.update.service.refreshService; | ||||
| import com.update.update.util.APKVersionCodeUtils; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Service; | ||||
|  | ||||
| @Service | ||||
| public class refreshServiceImpl implements refreshService { | ||||
|  | ||||
|     freshServiceImpl ser=new freshServiceImpl(); | ||||
|     //version1大返回一个正数,小返回一个负数,相等返回0 | ||||
|     public static int compareVersion(String version1, String version2) throws Exception { | ||||
|         if (version1 == null || version2 == null) { | ||||
|             throw new Exception("compareVersion error:illegal params."); | ||||
|         } | ||||
|         String[] versionArray1 = version1.split("\\."); | ||||
|         String[] versionArray2 = version2.split("\\."); | ||||
|         int idx = 0; | ||||
|         int minLength = Math.min(versionArray1.length, versionArray2.length); | ||||
|         int diff = 0; | ||||
|         while (idx < minLength | ||||
|                 && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0 | ||||
|                 && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) { | ||||
|             ++idx; | ||||
|         } | ||||
|         //如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大; | ||||
|         diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; | ||||
|         return diff; | ||||
|     } | ||||
|  | ||||
|     Application application = new Application(); | ||||
|     Context ass = application.getApplicationContext(); | ||||
|     String oldVersion = APKVersionCodeUtils.getVerName(ass); | ||||
|     String newVersion = ser.query().VersionId; | ||||
|     @Override | ||||
|     public fresh new_version() throws Exception { | ||||
|         if (compareVersion(oldVersion, newVersion) < 0){ | ||||
|             return ser.query(); | ||||
|         } | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,8 @@ | ||||
| package com.update.update.service; | ||||
|  | ||||
| import com.update.update.bean.fresh; | ||||
|  | ||||
| public interface refreshService { | ||||
|     //判断是否需要更新,比对是否有新的版本 | ||||
|     public fresh new_version() throws Exception; | ||||
| } | ||||
| @@ -0,0 +1,24 @@ | ||||
| package com.update.update.util; | ||||
| import android.content.Context; | ||||
|  | ||||
| import android.content.pm.PackageManager; | ||||
| public class APKVersionCodeUtils { | ||||
|  | ||||
|     /** | ||||
|      * 获取版本号名称 | ||||
|      * | ||||
|      * @param context 上下文 | ||||
|      * @return | ||||
|      */ | ||||
|     public static String getVerName(Context context) { | ||||
|         String verName = ""; | ||||
|         try { | ||||
|             verName = context.getPackageManager(). | ||||
|                     getPackageInfo(context.getPackageName(), 0).versionName; | ||||
|         } catch (PackageManager.NameNotFoundException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|         return verName; | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										57
									
								
								src/main/java/com/update/update/util/ErrorCode.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/main/java/com/update/update/util/ErrorCode.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| package com.update.update.util; | ||||
|  | ||||
| public class ErrorCode { | ||||
|  | ||||
|  | ||||
|  | ||||
|     // 成员变量 | ||||
|     private int httpStatus; | ||||
|     private String code; | ||||
|     private String message; | ||||
|     private int res_code; | ||||
|  | ||||
|     // 构造方法 | ||||
|     private ErrorCode(int httpStatus, String code, String message) { | ||||
|         this.setHttpStatus(200); | ||||
|         this.setRes_code(httpStatus); | ||||
|         this.setCode(code); | ||||
|         this.setMessage(message); | ||||
|     } | ||||
|     private ErrorCode() { | ||||
|         this.setHttpStatus(httpStatus); | ||||
|         this.setCode(code); | ||||
|         this.setMessage(message); | ||||
|     } | ||||
|     public int getHttpStatus() { | ||||
|         return httpStatus; | ||||
|     } | ||||
|  | ||||
|     public void setHttpStatus(int httpStatus) { | ||||
|         this.httpStatus = httpStatus; | ||||
|     } | ||||
|  | ||||
|     public String getCode() { | ||||
|         return code; | ||||
|     } | ||||
|  | ||||
|     public void setCode(String code) { | ||||
|         this.code = code; | ||||
|     } | ||||
|  | ||||
|     public String getMessage() { | ||||
|         return message; | ||||
|     } | ||||
|  | ||||
|     public void setMessage(String message) { | ||||
|         this.message = message; | ||||
|     } | ||||
|  | ||||
|     public int getRes_code() { | ||||
|         return res_code; | ||||
|     } | ||||
|  | ||||
|     public void setRes_code(int res_code) { | ||||
|         this.res_code = res_code; | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										28
									
								
								src/main/java/com/update/update/util/GJson.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/main/java/com/update/update/util/GJson.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| package com.update.update.util; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import com.google.gson.JsonSyntaxException; | ||||
| import com.google.gson.internal.Primitives; | ||||
|  | ||||
|  | ||||
| import java.lang.reflect.Type; | ||||
|  | ||||
|  | ||||
| public class GJson { | ||||
|     /** | ||||
|      * @description: 进行各种字符串转换 | ||||
|      **/ | ||||
|     public static <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException { | ||||
|         return Primitives.wrap(classOfT).cast(new Gson().fromJson(json, classOfT)); | ||||
|     } | ||||
|  | ||||
|     public static <T> T fromJson(String json, Type typeOf) { | ||||
|         return new Gson().fromJson(json, typeOf); | ||||
|     } | ||||
|  | ||||
|     public static String toJson(Object src) { | ||||
|         return new Gson().toJson(src); | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
							
								
								
									
										370
									
								
								src/main/java/com/update/update/util/ServletUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										370
									
								
								src/main/java/com/update/update/util/ServletUtil.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,370 @@ | ||||
| package com.update.update.util; | ||||
|  | ||||
| import com.alibaba.fastjson.JSON; | ||||
| import com.alibaba.fastjson.JSONObject; | ||||
| import com.alibaba.fastjson.serializer.SerializeConfig; | ||||
| import com.alibaba.fastjson.serializer.SerializeFilter; | ||||
| import com.alibaba.fastjson.serializer.SerializerFeature; | ||||
| import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; | ||||
| import org.apache.log4j.Logger; | ||||
|  | ||||
|  | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import java.io.PrintWriter; | ||||
| import java.io.UnsupportedEncodingException; | ||||
| import java.net.InetAddress; | ||||
| import java.net.UnknownHostException; | ||||
| import java.util.Date; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.regex.Matcher; | ||||
| import java.util.regex.Pattern; | ||||
|  | ||||
|  | ||||
| public class ServletUtil { | ||||
|  | ||||
|     //服务器标识 | ||||
|     private static String hostName = ""; | ||||
|  | ||||
|     //响应的ContentType内容 | ||||
|     private static final String RESPONSE_CONTENTTYPE = "application/json"; | ||||
|  | ||||
|     //响应编码 | ||||
|     private static final String RESPONSE_CHARACTERENCODING = "utf-8"; | ||||
|  | ||||
|     //业务名称的缩写 | ||||
|     private static final String BIZ_NAME = ""; | ||||
|  | ||||
|     private static Logger log = Logger.getLogger(ServletUtil.class); | ||||
|  | ||||
|     static{ | ||||
|         try { | ||||
|             InetAddress netAddress = InetAddress.getLocalHost(); | ||||
|             hostName = netAddress.getHostName(); | ||||
|         } catch (UnknownHostException e) { | ||||
|             log.error("netAddress.getHostName failed", e); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成系统异常错误报文 | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createSysErrorResponse(HttpServletResponse response){ | ||||
|         final String code = "INTERNAL_SERVER_ERROR"; | ||||
|         String message = "服务器内部错误"; | ||||
|         return createErrorResponse(500,500,code, message,response); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成参数不正确报文 | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createParamErrorResponse(HttpServletResponse response) { | ||||
|         final String code = "REQUIRE_ARGUMENT"; | ||||
|         String message = "缺少参数"; | ||||
|         return createErrorResponse(400,400,code,message,response); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成参数不正确报文 | ||||
|      * @param param 缺少的参数名称 | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createParamErrorResponse(String param,HttpServletResponse response) { | ||||
|         final String code = "REQUIRE_ARGUMENT"; | ||||
|         String message = "缺少参数:" + param; | ||||
|         return createErrorResponse(400,400,code,message,response); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 授权失败 | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createAuthorizeErrorResponse(HttpServletResponse response) { | ||||
|         final String code = "AUTH_DENIED"; | ||||
|         String message = "请求失败,没有访问或操作该资源的权限!"; | ||||
|         return createErrorResponse(403,403,code, message,response); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * 生成错误报文 | ||||
|      * @param errorCode | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createErrorResponse(ErrorCode errorCode, | ||||
|                                              HttpServletResponse response){ | ||||
|         return createErrorResponse(errorCode.getHttpStatus(),errorCode.getRes_code(),errorCode.getCode(), | ||||
|                 errorCode.getMessage(),response); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成错误报文 | ||||
|      * @param httpStatus | ||||
|      * @param code | ||||
|      * @param message | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createErrorResponse(Integer httpStatus, Object code, | ||||
|                                              String message, HttpServletResponse response){ | ||||
|         code = BIZ_NAME + code; | ||||
|         PrintWriter printWriter = null; | ||||
|         String jsonString = ""; | ||||
|         try { | ||||
|             response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); | ||||
|             response.setContentType(RESPONSE_CONTENTTYPE); | ||||
|             response.setStatus(httpStatus); | ||||
|  | ||||
|             Map<String, Object> map = new HashMap<String, Object>(); | ||||
|             map.put("code", code); | ||||
|             map.put("message", message); | ||||
|  | ||||
|  | ||||
|             printWriter = response.getWriter(); | ||||
|             jsonString = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue); | ||||
|             printWriter.write(jsonString); | ||||
|             printWriter.flush(); | ||||
|         } catch (Exception e) { | ||||
|             log.error("createResponse failed", e); | ||||
|         } finally { | ||||
|             if(null!=printWriter)printWriter.close(); | ||||
|         } | ||||
|  | ||||
|         return jsonString; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 生成错误报文 | ||||
|      * @param httpStatus | ||||
|      * @param res_code | ||||
|      * @param code | ||||
|      * @param message | ||||
|      * @param response | ||||
|      * @return | ||||
|      */ | ||||
|     public static String createErrorResponse(Integer httpStatus,Integer res_code, Object code, | ||||
|                                              String message, HttpServletResponse response){ | ||||
|         code = BIZ_NAME + code; | ||||
|         PrintWriter printWriter = null; | ||||
|         String jsonString = ""; | ||||
|         try { | ||||
|             response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); | ||||
|             response.setContentType(RESPONSE_CONTENTTYPE); | ||||
|             response.setStatus(httpStatus); | ||||
|  | ||||
|             Map<String, Object> map = new HashMap<String, Object>(); | ||||
|             map.put("code", code); | ||||
|             map.put("message", message); | ||||
|             //map.put("request_id", requestId==null?"":requestId); | ||||
|             //map.put("host_id", hostName); | ||||
|             map.put("res_code", res_code); | ||||
|  | ||||
|             printWriter = response.getWriter(); | ||||
|             jsonString = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue); | ||||
|             printWriter.write(jsonString); | ||||
|             printWriter.flush(); | ||||
|         } catch (Exception e) { | ||||
|             log.error("createResponse failed", e); | ||||
|         } finally { | ||||
|             if(null!=printWriter)printWriter.close(); | ||||
|         } | ||||
|  | ||||
|         return jsonString; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * 生成成功报文 | ||||
|      * @param httpCode | ||||
|      * @param result | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createSuccessResponse(Integer httpCode, Object result, HttpServletResponse response){ | ||||
|  | ||||
|         return createSuccessResponse(httpCode, result, SerializerFeature.WriteMapNullValue,null,response); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public static String createSuccessResponse(Integer httpCode,String message,Object result,HttpServletResponse response){ | ||||
|  | ||||
|         return createSuccessResponse(httpCode,message,result, SerializerFeature.WriteMapNullValue,null,response); | ||||
|  | ||||
|     } | ||||
|     /** | ||||
|      * 生成登录传成功报文 | ||||
|      * @param httpCode | ||||
|      * @param result | ||||
|      * @param response | ||||
|      */ | ||||
|     public static String createLoginSuccessResponse(Integer httpCode, Object result, HttpServletResponse response){ | ||||
| 		/*response.setHeader("Access-Control-Allow-Origin", "*"); | ||||
| 		response.setHeader("Access-Control-Allow-Methods","POST, GET, PATCH, DELETE, PUT"); | ||||
| 		response.setHeader("Access-Control-Allow-Headers",request.getHeader("Access-Control-Request-Headers"));*/ | ||||
|         return createSuccessResponse(httpCode, result, SerializerFeature.WriteMapNullValue,null,response); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public static String createSuccessResponse(Integer httpCode, Object result, SerializeFilter filter, HttpServletResponse response){ | ||||
|  | ||||
|         return createSuccessResponse(httpCode, result, SerializerFeature.PrettyFormat,filter,response); | ||||
|  | ||||
|     } | ||||
|     public static String createSuccessResponse(Integer httpCode, Object result, SerializerFeature serializerFeature, HttpServletResponse response){ | ||||
|  | ||||
|         return createSuccessResponse(httpCode, result,serializerFeature,null,response); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public static String createSuccessResponse(Integer httpCode, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){ | ||||
|         PrintWriter printWriter = null; | ||||
|         String jsonString = ""; | ||||
|         try { | ||||
|             response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); | ||||
|             response.setContentType(RESPONSE_CONTENTTYPE); | ||||
|             response.setStatus(httpCode); | ||||
|             printWriter = response.getWriter(); | ||||
|             if(null != result){ | ||||
|                 if(null!=filter){ | ||||
|                     jsonString = JSONObject.toJSONString(result,filter,serializerFeature); | ||||
|                 }else{ | ||||
| //					jsonString = JSONObject.toJSONString(result, serializerFeature); | ||||
|                     jsonString = JSONObject.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:ss:mm",serializerFeature); | ||||
|                 } | ||||
|                 printWriter.write(jsonString); | ||||
|             } | ||||
|             printWriter.flush(); | ||||
|  | ||||
|         } catch (Exception e) { | ||||
|             log.error("createResponse failed", e); | ||||
|         } finally { | ||||
|             if(null!=printWriter)printWriter.close(); | ||||
|         } | ||||
|         return jsonString; | ||||
|     } | ||||
|  | ||||
|     public static String createSuccessResponse(Integer httpCode, String message, Object result, SerializerFeature serializerFeature, SerializeFilter filter, HttpServletResponse response){ | ||||
|         PrintWriter printWriter = null; | ||||
|         String jsonString = ""; | ||||
|         try { | ||||
|  | ||||
|             response.setCharacterEncoding(RESPONSE_CHARACTERENCODING); | ||||
|             response.setContentType(RESPONSE_CONTENTTYPE); | ||||
|             response.setStatus(httpCode); | ||||
|             printWriter = response.getWriter(); | ||||
|             SerializeConfig config = new SerializeConfig(); | ||||
|             config.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); | ||||
|             Map<String, Object> map = new HashMap<String, Object>(); | ||||
|             if(null != result){ | ||||
|                 map.put("res_code", httpCode); | ||||
|                 map.put("message", message); | ||||
|                 map.put("data",result); | ||||
|                 if(null!=filter){ | ||||
|                     jsonString = JSONObject.toJSONString(map,filter,serializerFeature); | ||||
|                 }else{ | ||||
| //					jsonString = JSONObject.toJSONString(map,config,serializerFeature); | ||||
|                     jsonString = JSONObject.toJSONStringWithDateFormat(map,"yyyy-MM-dd"); | ||||
|  | ||||
|                 } | ||||
|                 printWriter.write(jsonString); | ||||
|             } | ||||
|             printWriter.flush(); | ||||
|  | ||||
|         } catch (Exception e) { | ||||
|             log.error("createResponse failed", e); | ||||
|         } finally { | ||||
|             if(null!=printWriter)printWriter.close(); | ||||
|         } | ||||
|         return jsonString; | ||||
|     } | ||||
|     /** | ||||
|      * 获取报文IP | ||||
|      * @param request | ||||
|      * @return | ||||
|      */ | ||||
|     public static String getRemortIP(HttpServletRequest request) { | ||||
|         String ip = request.getHeader("x-forwarded-for"); | ||||
|         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { | ||||
|             ip = request.getRemoteAddr(); | ||||
|         } | ||||
|  | ||||
|         if(ip.startsWith(",")){ | ||||
|             ip = ip.substring(1, ip.length()); | ||||
|         } | ||||
|  | ||||
|         return ip; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取带参数的URL串 | ||||
|      */ | ||||
|     public static String getUrlWithParams(HttpServletRequest request) { | ||||
|         String url = request.getRequestURI(); | ||||
|  | ||||
|         if (!StringUtil.isNull(request.getQueryString())) { | ||||
|             url = url + "?" + request.getQueryString(); | ||||
|         } | ||||
|  | ||||
|         return url; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 获取AccessToken | ||||
|      * @param request | ||||
|      * @return | ||||
|      */ | ||||
|     public static String getAccessToken(HttpServletRequest request){ | ||||
|         String accessToken = null; | ||||
|  | ||||
|         String authorization = request.getHeader("Authorization"); | ||||
|         if(StringUtil.isNull(authorization)){ | ||||
|             return accessToken; | ||||
|         } | ||||
|  | ||||
|         if(authorization.startsWith("MAC")){ | ||||
|             Pattern p = Pattern.compile("MAC id=\"(.*)\",nonce=\"(.*)\",mac=\"(.*)\""); | ||||
|             Matcher m = p.matcher(authorization); | ||||
|             if(m.find() && !StringUtil.isNull(m.group(1))){ | ||||
|                 return m.group(1); | ||||
|             } | ||||
|         }else if(authorization.startsWith("Bearer")){ | ||||
|             Pattern p = Pattern.compile("Bearer \"(.*)\""); | ||||
|             Matcher m = p.matcher(authorization); | ||||
|             if(m.find() && !StringUtil.isNull(m.group(1))){ | ||||
|                 return m.group(1); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         return accessToken; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 判断是否是Mac Token | ||||
|      * @param request | ||||
|      * @return | ||||
|      */ | ||||
|     public static boolean isExistMacToken(HttpServletRequest request){ | ||||
|         String authorization = request.getHeader("Authorization"); | ||||
|         if(!StringUtil.isNull(authorization) && authorization.startsWith("MAC id=")){ | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         return false; | ||||
|     } | ||||
|     /** | ||||
|      * 设置让浏览器弹出下载对话框的Header. | ||||
|      * 根据浏览器的不同设置不同的编码格式  防止中文乱码 | ||||
|      * @param fileName 下载后的文件名. | ||||
|      */ | ||||
|     public static void setFileDownloadHeader(HttpServletRequest request, HttpServletResponse response, String fileName) { | ||||
|         try { | ||||
|             //中文文件名支持 | ||||
|             String encodedfileName = java.net.URLEncoder.encode(fileName,"UTF-8"); | ||||
|             response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedfileName + "\""); | ||||
|             response.setContentType("application/force-download"); | ||||
|         } catch (UnsupportedEncodingException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										55
									
								
								src/main/java/com/update/update/util/StringUtil.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								src/main/java/com/update/update/util/StringUtil.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| package com.update.update.util; | ||||
|  | ||||
| import java.util.regex.Matcher; | ||||
| import java.util.regex.Pattern; | ||||
|  | ||||
| public class StringUtil { | ||||
|  | ||||
|     /** | ||||
|      * 判断字符串是否为null、“ ”、“null” | ||||
|      * @param obj | ||||
|      * @return | ||||
|      */ | ||||
|     public static boolean isNull(String obj) { | ||||
|         if (obj == null){ | ||||
|             return true; | ||||
|         }else if (obj.toString().trim().equals("")){ | ||||
|             return true; | ||||
|         }else if(obj.toString().trim().toLowerCase().equals("null")){ | ||||
|             return true; | ||||
|         } | ||||
|  | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 正则验证是否是数字 | ||||
|      * @param str | ||||
|      * @return | ||||
|      */ | ||||
|     public static boolean isNumber(String str) { | ||||
|         Pattern pattern = Pattern.compile("[+-]?[0-9]+[0-9]*(\\.[0-9]+)?"); | ||||
|         Matcher match = pattern.matcher(str); | ||||
|  | ||||
|         return match.matches(); | ||||
|     } | ||||
|     /** | ||||
|      * 将一个长整数转换位字节数组(8个字节),b[0]存储高位字符,大端 | ||||
|      * | ||||
|      * @param l | ||||
|      *            长整数 | ||||
|      * @return 代表长整数的字节数组 | ||||
|      */ | ||||
|     public static byte[] longToBytes(long l) { | ||||
|         byte[] b = new byte[8]; | ||||
|         b[0] = (byte) (l >>> 56); | ||||
|         b[1] = (byte) (l >>> 48); | ||||
|         b[2] = (byte) (l >>> 40); | ||||
|         b[3] = (byte) (l >>> 32); | ||||
|         b[4] = (byte) (l >>> 24); | ||||
|         b[5] = (byte) (l >>> 16); | ||||
|         b[6] = (byte) (l >>> 8); | ||||
|         b[7] = (byte) (l); | ||||
|         return b; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user