Gowhich

Durban's Blog

1
2
3
4
5
6
7
8
9
10
11
12
13
//第一步:声明一个button
private Button button;
//实例化这个button
button = (Button) this.findViewById(R.id.button);
//给这个button添加onclick事件
button.setOnclickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
intent.putExtra("key","value");
startActivity(intent);
}
})

想要知道自己的年龄,出生日期和性别,或者是别人的,给我个身份证号,我就可以知道,看下面代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
static validateIdNumberToAgeYear(str){
let date = new Date();
let currentYear = date.getFullYear();
let currentMonth = date.getMonth() + 1;
let currentDate = date.getDate();

let idxSexStart = str.length == 18 ? 16 : 14;
let birthYearSpan = str.length == 18 ? 4 : 2;

let year;
let month;
let day;
let sex;
let birthday;
let age;

//性别
let idxSex = 1 - str.substr(idxSexStart, 1) % 2;
sex = idxSex == '1' ? '女' : '男';
//生日
year = (birthYearSpan == 2 ? '19' : '') + str.substr(6, birthYearSpan);
month = str.substr(6 + birthYearSpan, 2);
day = str.substr(8 + birthYearSpan, 2);
birthday = year + '-' + month + '-' + day;
//年龄
let monthFloor = (currentMonth < parseInt(month,10) || (currentMonth == parseInt(month,10) && currentDate < parseInt(day,10))) ? 1 : 0;
age = currentYear - parseInt(year,10) - monthFloor;

// console.log("我的出生日期是"+year+"年"+month+"月"+day+"日"+",今年"+age+"岁了"+",性别是"+sex);

if(age >= 18){
return true;
}

return false;
}

我这里只是做了一个年龄的判断。

最近服务器出现问题了,error.log日志里面多了很多的(110: Connection timed)这个错误。

开始以为是Nodejs的脚本有问题,再请求的时候会有超时的问题,但是检查了一下,并没有发现问题,因为已经对出现问题的错误做了sysError的日志记录,但是在日志里面并没有找到对应的错误信息,很奇怪。也是google下找到了对应的解决方案。

参考:http://stackoverflow.com/questions/10395807/nginx-close-upstream-connection-after-request

1
2
3
4
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
}

本来我的upstream中只加了server这段信息的,现在参考了这里的话,也加了下keepalive;

1
2
3
4
upstream backend {
server 127.0.0.1:2222;
keepalive 128;
}

然后重启以下nginx;

1
sudo nginx -s reload

这个命令执行完,似乎没有立刻起作用,于是

1
sudo nginx -s reopen

这样就可以了。

解压缩base64 压缩文件,稍微解释一下,比如你有一个pdf文件,使用软件压缩成了.gz格式的文件,然后再把这个文件做成了basa64 String 传输给某个人,比如这个人就是我,好吧,问题来了,我们要实现一个过程,就是反解这个文件,将base64 string 转成 .gz文件,然后再把.gz文件解压。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function actionPdf(){
$pdf_base64 = BASE64_DATA_PATH;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen (PDF_FILE_PATH,'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);

// This input should be from somewhere else, hard-coded in this example
$file_name = PDF_FILE_PATH;

// Raising this value may increase performance
$buffer_size = 4096; // read 4kb at a time
$out_file_name = str_replace('.gz', '', $file_name);

// Open our files (in binary mode)
$file = gzopen($file_name, 'rb');
$out_file = fopen($out_file_name, 'wb');

// Keep repeating until the end of the input file
while(!gzeof($file)) {
// Read buffer-size bytes
// Both fwrite and gzread and binary-safe
fwrite($out_file, gzread($file, $buffer_size));
}

// Files are done, close files
fclose($out_file);
gzclose($file);


// $base64Data = file_get_contents(BASE64_DATA_PATH);
// $data = base64_decode($base64Data);
// file_put_contents(PDF_FILE_PATH,$data);
}

哈哈,参考java版本重写,还有node版本的。

PS:

每个人都是从生到死,但是活法不一样,就比如这个方法,从开始到最后做完了自己改做的事情。选择一个语言走下去吧【选择一个活法,直到死去】。

我们都想在自己的一生过好多种不同的活法,但是事实上,不可能呀。来来学点编程,体会一下,不同的人生【语言】带给你的不同体验。

Express框架下载文件的方法,我想已经有人已经知道了。

这里说下Koajs的方法。

首先设置Content-disposition

1
2
let filename = 'xxxx';
ctx.set('Content-disposition', 'attachment; filename=' + filename + '.pdf');//attachment

或者

1
ctx.set('Content-disposition', 'inline; filename=' + filename + '.pdf');//inline

以上两种的区别是一个是attachment,意思就是附件,还有一种是inline,意思就是内附。区别就是attachment打开的时候可以下载文件,inline有时候可以下载,有时候可以直接浏览,好像跟浏览器有关。

然后设置下文件类型

1
ctx.set('Content-type', 'application/pdf');

给body赋值,这里是一个Buffer。

然后把文件内容读出来

let gReadData = new Buffer();//这里自己根据自己的具体情况去实现就好了。

在函数最后

1
return ctx.body = gReadData;

整体代码大概如下:

1
2
3
4
5
6
module.exports.downloadPdf = async (ctx,next) => {
let filename = 'xxxx';
ctx.set('Content-disposition', 'attachment; filename=' + filename + '.pdf');
ctx.set('Content-type', 'application/pdf');
return ctx.body = gReadData;
}

Intellij IDEA,这个编辑器今天在做Base64转pdf的过程中遇到了奇怪的问题:“常量字符串太长”

搜索问答思路:

  1. 我搜索了soft wrap的配置,把他们都设为取消:没用;

  2. Google问题,得到jetbrains答案:vim插件,卸载之,没用;

  3. 无奈之下,求助与熟练操作intellij idea的朋友,答曰“大概是jdk的问题“。摸索之,改之,无用;

最终的答案:

最后修改了Java compiler下的Use compiler为Eclipse,成功。

这里使用了com.alibaba.fastjson这个包

maven【很不错的包管理器】安装方式:

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>

使用方法

1
import com.alibaba.fastjson.JSON;

//字符串转换为可使用的对象【Map】

1
2
3
4
5
6
7
8
9
10
11
12
13
String str = "";//这里就是一个json 字符串
JSON.parseObject(str);
HashMap<String, String> jsonMap = JSON.parseObject(str, new HashMap<String, String>().getClass());
//去除里面的值
for(String key : jsonMap.keySet()) {
String str = jsonMap.get(key);
System.out.println(key + ":" + str);
}
//将 对象 【HashMap】转化为json 字符串
HashMap<String, String> map = new HashMap<>();
map.put("data","data");
map.put("sign","sign");
String jsonString = JSON.toJSONString(map);

这里设置路由跟请求回调方法

1
2
3
4
get("/jsontest", "application/json", (req, res) -> {
//实现结果的代码,比如上面生成的结果 jsonString
//return jsonString
});

========================================

==Spark Framework - A tiny Java web framework==

========================================

这里使用了com.github.kevinsawicki.http这个包

maven【很不错的包管理器】安装方式:

1
2
3
4
5
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>6.0</version>
</dependency>

使用方法

1
import com.github.kevinsawicki.http.HttpRequest;

//这里只演示post提交的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
HttpRequest request = HttpRequest.post("http://xxxx.xxxx.xxxx.xxxx:8895/xxxxxx/general2/xxxx/xxxxxx.html");
request.part("data", "data");
request.part("sign", "sign");
if (request.ok()) {
BufferedReader reader = request.bufferedReader();
StringBuffer strBuffer = new StringBuffer();
String inputLine;
try {
while ((inputLine = reader.readLine()) != null) {
strBuffer.append(inputLine);
}
} catch (Exception e) {
System.out.println(e);
}
reader.close();
//到这里就把数据解出来了,针对于java,还是有点小麻烦,
HashMap<String, String> jsonMap = JSON.parseObject(strBuffer.toString(), new HashMap<String, String>().getClass());
System.out.println(jsonMap);
for (String key : jsonMap.keySet()) {
String str = jsonMap.get(key);
System.out.println(key + ":" + str);
}
System.out.println("Status was updated");
}

//好了到这里就可以了,说实话java是底层语言吧,是的,我们用的php是的估计都是人家用c封装好的了,直接一个curl就能得到结果了,你也可以用java写个curl,也不是啥问题

========================================

=Spark Framework - A tiny Java web framework==

========================================

0%