blog's People
blog's Issues
倒车
java Quartz 作业调度框架
java Quartz 作业调度框架
最近真是太忙了,没有时间更新,最近项目中需要定期发送内容给微信用户,
20150901-图片异步上传
今天基本实现了图片异步上传的基本类,使用的commons-fileupload,目前自己的配置文件还没有完成,理想情况下,是类似于百度ueditor那样的。后端上午就完成了,然后在前端那里卡住了,因为貌似Ajax上传不太好实现,后来百度之,找到了一段代码。还没有模块化。下一步继续做。下面是那段前端上传图片的代码,只能说自己前端太渣了
$.ajax({
type:'POST',
url:"../../do?action=util!AjaxUploadImageFile&f=imageFile",
data: formdata,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false
}).then(function (msg) {
alert(msg);
var result = JSON.parse(msg);
if(result.state=="ok"){
alert("上传成功!!");
$("#"+submitId).val(result.fileName);
}else if(result.state=="error") {
alert("上传失败");
}
}, function () {
//failCal
});
这段代码还没有好好看。
嗯,最近想自己写一个代理工具。抽时间研究一下。自己能用就行。
20150831-实现图片上传
实现图片上传
使用百度的ueditor,图片上传一直出错,所以想自己实现一个上传图片的通用类。
配合公司的现有框架。
如何实现一个科配置的,通用的文件上传框架?
而且现在的需求是一个表单会提交很多很多图片,如果是提交的话在后台一个个获取,会很影响效率,考虑是异步上传,这样可以一遍上传一遍继续编辑其他内容。目前还没有想好配置不同的上传路径怎么做(怎么通知一个文件上传的路径? )。
angularjs uiSelect 组件实现自动打开选择list
这个问题翻官方的github中的issue翻了一天,终于实现了。 解决之后最大的感想就是他妈的英语好是多么多么的重要!
app.directive('uiSelectWrapper', function($parse, $timeout) {
return {
link: function(scope, element, attrs) {
var uiSelectController = element.children().controller('uiSelect');
var open= $parse(attrs.uiSelectWrapper);
scope.$watch(open, function(value) {
if(value === true) {
$timeout(function() {
uiSelectController.activate(false, true);
}, 10);
}
})
}
}
});
使用的时候
<div ui-select-wrapper="productLine.showSelect">
<ui-select ng-model="productLine.selected" id="ui_select_{{key}}"
ng-change="selectAProduct(key, productLine)"
ng-click="testSelectClick(productLine)"
theme="select2" style="min-width: 300px;">
</ui-select>
</div>
如果{{productLine.showSelect}}
为true则自动打开
自己写的消息队列
自己写的消息队列
因为项目需要,自己写了一个消息队列,测试了一下,并发性不太好,但已经满足需求。
主要是需要delay时间处理任务,没想到好的解决办法。
package com.pabula.common.util;
import java.util.*;
import java.util.concurrent.*;
/**
* Created by sunsai on 2016/1/19.
*/
public class MessageQueue {
private ConcurrentMap<Long, Runnable> msgQueue;
private LinkedBlockingQueue<Long> keyQueue;
private Random random;
private int[] randomArray ;
private int randIndex = 0;
private Thread thread;
private static MessageQueue instance = null;
private MessageQueue() {
msgQueue = new ConcurrentHashMap<Long, Runnable>();
keyQueue = new LinkedBlockingQueue<Long>();
random = new Random();
initRandomArr();
thread = new Thread(new Runnable() {
@Override
public void run() {
loop();
}
});
thread.start();
}
private void initRandomArr() {
randomArray = new int[100];
for(int i = 0; i < 100; i++) {
randomArray[i] = i + 1;
}
int tmp = 0;
for (int i = 0; i < 100; i++) {
int next = random.nextInt(100 - i);
tmp = randomArray[next];
randomArray[next] = randomArray[99-i];
randomArray[99-i] = tmp;
}
}
private synchronized int getRandInt() {
if (randIndex > 99) {
randIndex = 0;
}
return randomArray[randIndex++];
}
public static MessageQueue getInstance() {
if (instance == null) {
synchronized (MessageQueue.class) {
if (instance == null) {
instance = new MessageQueue();
}
}
}
return instance;
}
private void loop() {
while (true) {
try {
long now = getNow();
long executeAt = keyQueue.take();
if (now > executeAt) {
Runnable runnable = take(executeAt);
if (runnable != null) {
runnable.run();
}
} else {
keyQueue.put(executeAt);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* @param runnable 执行单元
* @param delay 单位秒
*/
public void post(long delay, Runnable runnable) {
long executedAt = getKey(delay);
_post(runnable, executedAt);
}
/**
*
* @param runnable
* @param executeAt
*/
private void _post(Runnable runnable, long executeAt) {
if (runnable != null) {
addToQueue(runnable, executeAt);
}
}
private synchronized void addToQueue(Runnable runnable, long key) {
keyQueue.add(key);
msgQueue.put(key, runnable);
}
private Runnable take(long key) {
Runnable runnable = msgQueue.get(key);
msgQueue.remove(key);
return runnable;
}
/**
*
* @param runnable
*/
public void post(Runnable runnable) {
post(0,runnable);
}
private long getKey(long delay) {
long now = new Date().getTime();
now += delay * 1000;
now *= 100;
long rand = getRandInt();
now += rand;
return now;
}
private long getNow() {
long now = new Date().getTime();
now *= 100;
return now;
}
}
Error:duplicate files during packaging of APK
在android{
} 添加
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
20150828
嗯,发现用这个写博客真是太棒了,天生支持Markdown。之前想自己要重新早轮子,自己写一个markdown的解析,可是迟迟没有开始搞。后来就不在浏览器组了,以后搞?
在filter中对响应进行包装
给Android的接口,以前写的不规范,现在需要在结果数据上面包装一层对结果的描述信息,如正确或者出错的具体信息。
而且有可能在服务器这边控制表单的重复提交,所以在filter会返回一个唯一的key,用于下次提交表单的时候带上。
因为response里面的outputStream只能响应一次,所以,通过httpResponseWrapper包装进去一个新的outputStream,然后从包装的这个out流中取出数据,再放入到原先的那个请求的response里
package com.pabula.fw.filter;
import com.pabula.common.util.HttpClientUtil;
import com.pabula.common.util.JsonResultUtil;
import com.pabula.common.util.StrUtil;
import com.pabula.hh.ENV;
import org.omg.CORBA.StringHolder;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.*;
/**
* Created by sunsai on 2015/9/29.
*/
public class AndroidApiAuthFilter implements Filter {
private static final Set<String> apis = new HashSet<String>();
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse)response;
HttpServletRequest req = (HttpServletRequest)request;
HttpSession session = req.getSession();
String token = req.getParameter("token");
String action = req.getParameter("action");
System.out.println(action);
if (null == action || StrUtil.isNull(action) ) {
chain.doFilter(request,response);
System.out.println("jsp do not require token");
return;
}
if(!apis.contains(action)) {
WrapperResponse wrapperResponse = new WrapperResponse(res);
chain.doFilter(request, wrapperResponse);
/**
* 在执行之后,对结果进行过滤包装处理
*
*/
String resultTmp = wrapperResponse.getContent();
System.out.println("resultTmp" + resultTmp);
String result = null;
if("error".equals(resultTmp)) {
result = JsonResultUtil.instance(401,"error","error").json();
}else {
result = JsonResultUtil.instance(resultTmp).json();
}
res.setHeader("Content-Type","application/json;charset=gbk");
OutputStreamWriter out = new OutputStreamWriter(res.getOutputStream());
out.write(result);
out.flush();
out.close();
return;
}
System.out.println("api 权限验证");
if(null == token || StrUtil.isNull(token)) {
OutputStreamWriter out = new OutputStreamWriter(res.getOutputStream());
out.write("error");
out.flush();
out.close();
System.out.println("token为空");
return;
}
Map<String, String> para = new HashMap<String, String>();
para.put("token", token);
String authResult = HttpClientUtil.doPost(ENV.AUTH_SERVER + "do?action=auth!CAjaxCheckToken", para);
System.out.println(authResult);
if("error".equals(authResult)) {
OutputStreamWriter out = new OutputStreamWriter(res.getOutputStream());
out.write("error");
out.flush();
out.close();
System.out.println("token验证失败");
}else{
request.setAttribute("qingxiaPhone", authResult);
WrapperResponse wrapperResponse = new WrapperResponse(res);
chain.doFilter(request, wrapperResponse);
/**
* 在执行之后,对结果进行过滤包装处理
*
*/
String resultTmp = wrapperResponse.getContent();
String result = null;
if("error".equals(resultTmp)) {
result = JsonResultUtil.instance(401,"error","error").json();
}else {
result = JsonResultUtil.instance(resultTmp).json();
}
res.setHeader("Content-Type","application/json");
OutputStreamWriter out = new OutputStreamWriter(res.getOutputStream());
out.write(result);
out.flush();
out.close();
}
}
public static String getJspOutput(String jsppath, HttpServletRequest request, HttpServletResponse response)
throws Exception
{
WrapperResponse wrapperResponse = new WrapperResponse(response);
request.getRequestDispatcher(jsppath).include(request, wrapperResponse);
return wrapperResponse.getContent();
}
// public static void main(String[] args) {
// Map<String, String> para = new HashMap<String, String>();
//
// para.put("token", "12323234434343");
// String authResult = HttpClientUtil.doPost(ENV.AUTH_SERVER + "do?action=auth!CAjaxCheckToken", para);
//
// System.out.println(authResult);
// }
}
class WrapperResponse extends HttpServletResponseWrapper {
private MyPrintWriter tmpWriter;
private ByteArrayOutputStream output;
public WrapperResponse(HttpServletResponse httpServletResponse) {
super(httpServletResponse);
output = new ByteArrayOutputStream();
tmpWriter = new MyPrintWriter(output);
}
public void finalize() throws Throwable {
super.finalize();
output.close();
tmpWriter.close();
}
public String getContent() {
try {
tmpWriter.flush(); //刷新该流的缓冲,详看java.io.Writer.flush()
String s = tmpWriter.getByteArrayOutputStream().toString();
//此处可根据需要进行对输出流以及Writer的重置操作
//比如tmpWriter.getByteArrayOutputStream().reset()
return s;
} catch (Exception e) {
return "error";
}
}
//覆盖getWriter()方法,使用我们自己定义的Writer
public PrintWriter getWriter() throws IOException {
return tmpWriter;
}
public void close() throws IOException {
tmpWriter.close();
}
//自定义PrintWriter,为的是把response流写到自己指定的输入流当中
//而非默认的ServletOutputStream
private static class MyPrintWriter extends PrintWriter {
ByteArrayOutputStream myOutput; //此即为存放response输入流的对象
public MyPrintWriter(ByteArrayOutputStream output) {
super(output);
myOutput = output;
}
public ByteArrayOutputStream getByteArrayOutputStream() {
return myOutput;
}
}
}
关于框架
关于框架
现在公司这个自有的框架实在是太原始了吧,很多基础的需求都没有提供解决方案,比如即将会使用到的权限管理。哎。而且很不nice,也不是RUST风格。
上天因为ueditor上传没跑起来,就自己写了一个上传的接口,在框架里面是没有的,play!框架是有的,play写起来真是太爽了,不过,使用自有框架,啥都没有,就啥都需要自己去写,很锻炼人呀,虽然现在也没有啥进步的。但相信将来用户数上来,会遇到各种问题,希望将来可以hold住啊。
关于get请求中文乱码
前两天调用微信一些接口的时候,了解到了urlencode,想着是不是可以解决get请求的时候,中文乱码的问题,网上查资料,知道了java.net.URLEncoder.encode()编码 和java.net.URLDecoder.decode()解码
前端方面,使用js的encodeURIComponent()
因为浏览器可能会自动解码,所以需要编码两次
jQuery 相关记录
$("input[name=IS_SALE]:checked").val();
获得单选框选中的值
Intellij IDEA 常用快捷键
- Alt + Entry 导入包
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.