融么么俱乐部微信项目总结
2016年7月11日开始项目动员开会,27号布署上线还有加夜班改页面,在东哥的加持下,和龙洞小分队终于完成了搜融俱乐部的1.0版本。
以下是在开发过程中出现过的问题,自我觉得很有必要记录下来。
1. 读取properties得到输入流
ClassLoader
this.pro = new Properties();
//不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从ClassPath根下获取,其实质还是由ClassLoader获取资源
InputStream input = XXX.class.getResourceAsStream(resource);
pro.load(new InputStreamReader(input, "UTF-8"));
input.close();
2. 将字符串内容写入本地文件
PrintWriter类
PrintWriter pw = new PrintWriter(file);
pw.write(content);
pw.flush();
pw.close();
3. 删除Cookie
//先将要删除的cookie找出来
...
cookie.setMaxAge(0);
response.addCookie(cookie);
4. 时间判断
Date类方法after()、before()
5. 前后端分离传json时日期类型的局部注解处理
使用spring的@ResponseBody注解输出json串时,可以在pojo类的get方法上添加以下注解:
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm") @JsonFormat(pattern="yyyy-MM-dd HH:mm",timezone = "GMT+8")
使用Fastjson主动转成json串时,可以在pojo类的属性上添加注解:
@JSONField (format="yyyy年M月d日H时m分")
6. 已定义过滤器,后台接收仍为乱码
tomcat默认全部都是用ISO-8859-1编码,不管你页面用什么显示,Tomcat最终还是会替你将所有字符转做ISO-8859-1。
new String(request.getParameter("editorValue").getBytes(
"iso-8859-1"), "utf-8")
7. JUnit对Spring的service类进行测试
//JUnit配置
@RunWith(SpringJUnit4ClassRunner.class)
//spring配置文件引入
@ContextConfiguration(locations = {"classpath:/spring/applicationContext-*.xml"})
public class TestXXXService {
//日志打印
private static Logger logger = Logger.getLogger(TestXXXService.class);
@Resource
private ActivityService activityService = null;
@Test
public void testXXXX() {
...
}
}
8. 被重复请求后台的措施
- 数据库查询,拒绝重复增删操作
- 严格按逻辑在分支执行后写上return语句
9. 不被spring管理的类调用service层方法
工具类:
//全局范围spring容器工具
public class SpringCtxUtils implements ApplicationContextAware {
private static ClassPathXmlApplicationContext ctx;
private static ApplicationContext webCtx; //Web环境Spring应用上下文环境
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
webCtx = applicationContext;
}
public static void init(String[] springConfigs) {
ctx = new ClassPathXmlApplicationContext(springConfigs);
}
public static void start() {
ctx.start();
}
public static void close() {
ctx.close();
}
//加载service
public static <T> T getBean(Class<T> requiredType) {
if (ctx != null) {
return ctx.getBean(requiredType);
} else {
return webCtx.getBean(requiredType);
}
}
public static <T> T getBean(String beanName, Class<T> requiredType) {
if (ctx != null) {
return ctx.getBean(beanName, requiredType);
} else {
return webCtx.getBean(beanName, requiredType);
}
}
public static Object getBean(String name) throws BeansException {
if (ctx != null) {
return ctx.getBean(name);
} else {
return webCtx.getBean(name);
}
}
}
spring配置:
//配置在service中
<bean class="util.SpringCtxUtils"></bean>
使用:
XXXService service = (XXXService) SpringCtxUtils
.getBean("xxxService");
10. 被spring管理的非controller类(测试类、拦截器)调用service层方法
@ContextConfiguration(locations = { "classpath:/spring/applicationContext-*.xml" })
@Resource
private XXXService service = null;
11. 微信授权登录获取用户信息
- 在公众号对应的接口配置中配置回调域名
- 将要调用的控制层访问路径URL编码后和其它参数一起拼接成微信授权页面的链接
- 在控制类或servlet中获取微信给的code参数
- 通过code调用接口获取用户信息
12. Spring接收由form表单上传图片
@RequestMapping(value = "访问路径", method = RequestMethod.POST)
public @ResponseBody void xxx(HttpServletRequest request,
HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 获取上传的图片
CommonsMultipartFile imgFile = (CommonsMultipartFile) multipartRequest
.getFile("表单图片项的名");
if (imgFile != null && imgFile.getSize() > 0) {
// 获取路径
String ctxPath = configUtil.getValue("存储父路径");
// 获得源文件名
String realFileName = imgFile.getOriginalFilename();
// 新文件名
String newRealFileName = UUID.randomUUID() + realFileName.substring(realFileName.indexOf("."));
// 创建文件
File dirPath = new File(ctxPath);
if (!dirPath.exists()) {
dirPath.mkdir();
}
File uploadFile = new File(ctxPath + newRealFileName);
// 文件内容复制实现上传
FileCopyUtils.copy(imgFile.getBytes(), uploadFile);
}
}
13. 图片尺寸压缩
//获取源文件尺寸
Image img = ImageIO.read(new File(filename));
int width= img.getWidth(null);
int height= img.getHeight(null);
//获取图片格式
String format = newFilename.substring(newFilename.lastIndexOf(".") + 1,
newFilename.length());
//新RGB图片
BufferedImage bi = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
//将原图按新尺寸画到新图片上
bi.getGraphics().drawImage(img, 0, 0, newWidth, newHeight, null);
//存取图片
ImageIO.write(bi, format, new File(newFilename));
14. Nginx下配置tomcat应用出现静态文件失效
原来的nginx另外配了静态文件的缓存,在清除了缓存的配置代码后问题消失。
15. 百度编辑器上传文件的保存路径自定义
在jsp/config.json中的文件保存自定义功能有限,不能保存到另一盘符。若要实现自定义到任意位置,可通过个性源码来实现。
源码的大概流程为:
* jsp/controller.jsp是入口,调用ActionEnter类
* ActionEnter类根据actionCode参数判断调用上传/截屏/批量上传类
* Uploader类自动根据是否要解析base64编码调用上传类
* 修改上传类中physicalPath,即可实现上传的文件指定为任意盘下
(瞎bb)
这是我第一次使用svn,第一次和多人进行项目开发。没有svn的时候总是要自已去整合代码,而且由于没有统一存放代码的地方,每个人手中的代码版本都不一样,不能总是拿到最新的代码。虽然真切地感受到了版本管理的重要性,但对svn这方面的知识实在是比较捉急。还不会配置svn,不会用分支版本,后来甚至因为svn更新时报错所以最终加夜班时没有用svn进行管理。到后来,大家有修改的基本都直接放上了服务器。
另外对nginx的概念也相当地陌生,毕竟之前学习后台用得都是tomcat,前端的伙伴则说他用nginx用得很多,这可能就是nginx和tomcat的区别吧?(^^)