Resive world

Come world to Record life


  • Home

  • Tags

  • Categories

  • Archives

  • Sitemap

  • Search

openvpn生成密钥对

Posted on 2016-06-09 | In 网络

静态公私鈅对生成

Read more »

Servlet3.0实现上传文件的获取

Posted on 2016-06-05 | In Web

据说在以前的Servlet版本中,如果想要处理form中post过来的文件数据的话,那么还必须下载第三方的包,比如commons-fileupload等,很是麻烦。但是现在的Servlet版本中已经可以支持对file数据的直接处理,这里姑且记录下简单的用法。

Html端

前端中只要写一个正常提交的表单即可,比如下面这样:

1
2
3
4
5
<form action="Display" method="post" enctype="multipart/form-data">
<label>上传文件:<label>
<input type="file" name="file">
<input type="submit" value="上传">
</form>

当然也可以用ajax的form来提交,这都无妨。

Servlet端

首先需要在Servlet类定义前加上@MultipartConfig标注,然后在doPost方法里这么写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置字符集
request.setCharacterEncoding("utf-8");
//用parts来接收数据并保存到本地路径中(考虑到可能有多个文件,因此循环读入)
Collection<Part> parts = request.getParts();
for (Part part : parts) {
part.write("tmp.txt");
//对tmp.txt做点什么
}
//返回信息
PrintWriter out = response.getWriter();
out.println("上传成功");
out.flush();
out.close();
}

这样就可以将客户上传的文件在本地保存下来了,非常简单方便(但是不知道的话还真的很麻烦)。

通常这样就足够了,但是有时候我们还需要知道文件的名字,这就稍微麻烦一点了,还得解析协议的header来获得文件名:

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
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置字符集
request.setCharacterEncoding("utf-8");

Part part = request.getPart("file");//通过表单file控件(<input type="file" name="file">)的名字直接获取Part对象

String header = part.getHeader("content-disposition");//获取请求头,请求头的格式:form-data; name="file"; filename="snmp4j--api.zip"

String fileName = getFileName(header);//获取文件名

part.write(fileName);

//返回信息
PrintWriter out = response.getWriter();
out.println("上传成功");
out.flush();
out.close();
}

public String getFileName(String header) {
/**
* String[] tempArr1 = header.split(";");代码执行完之后,在不同的浏览器下,tempArr1数组里面的内容稍有区别
* 火狐或者google浏览器下:tempArr1={form-data,name="file",filename="snmp4j--api.zip"}
* IE浏览器下:tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}
*/
String[] tempArr1 = header.split(";");
/**
*火狐或者google浏览器下:tempArr2={filename,"snmp4j--api.zip"}
*IE浏览器下:tempArr2={filename,"E:\snmp4j--api.zip"}
*/
String[] tempArr2 = tempArr1[2].split("=");
/获取文件名,兼容各种浏览器的写法
String fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\")+1).replaceAll("\"", "");
return fileName;
}

参考

使用Servlet3.0提供的API实现文件上传

利用jQuery异步上传文件的插件

Posted on 2016-06-04 | In Web

现在想实现用ajax来上传文件的功能,但是却发现Jquery自带的ajax方法只能上传文件名,而不能上传文件;用form提交虽然能够上传文件,但是却要刷新页面。。。多方查找下找到了一个可用的jQuery插件,刚好可以满足异步上传文件的要求。

代码

jquery.form.js

用法

这个插件是基于表单提交的,我们只要正常的写一段提交文件的表单,如:

1
2
3
4
<form id="myForm" action="comment.php" method="post" enctype="multipart/form-data">
<input type="file" name="name" />
<input type="submit" value="Submit Comment" />
</form>

然后在js中加上如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head>
<script src="jquery.js"></script>
<script src="jquery.form.js"></script>
<script>
$(document).ready(function() {
$('#myForm').ajaxForm(function(data) {
alert(data);
});
});
</script>
</head>

这样就可以监听表单的提交事件,把它变成ajax传送到后台,然后将后台返回的信息从data中获取。如此一来就可以用ajax通信来传输文件了。

js高级与面向对象之原型

Posted on 2016-06-04 | In javascript

原型的基本概念

在构造函数创建出来的时候,系统会默认的帮构造函数创建并且关联一个空对象,这个对象就成为原型

Read more »

路径匹配之单向距离OWD算法

Posted on 2016-06-02 | In Trajectory Similarity

简述

** OWD(One Way Distance)**算法也是一种描述两个路径之间相似度的方法,最早大概提出于06年左右。最朴素的OWD算法的思路也非常简单,就是把路径之间的距离转化为点到路径的距离再加以处理。这里只对这种算法做简要介绍,至于深层次的理论有空再研究论文。

定义

在定义路径间的距离$D_{owd}$之前,我们先定义点到路径的距离$D_{point}$:

对于点$p$和一个由多个点组成的路径$T$,定义他们之间的距离为$$D_{point}(p,T)=min_{q \in T} D_{Euclid}(p,q)$$

其中$D_{Euclid}(p,q)$表示$p.q$之间的欧式距离。

然后,我们定义路径$T_1$到路径$T_2$的单向距离$D_{owd}(T_1,T_2)$为:

$$D_{owd}(T_1,T_2)=\frac1{|T_1|}(\sum_{p\in T_1}D_{point}(p,T_2))$$

(对于非离散的路径,我们可以把他看成是一个积分过程)

很容易看出来,这个单向距离不具有对称性,即$D_{owd}(T_1,T_2)$与$D_{owd}(T_2,T_1)$不一定相等。那么为了得到一个对称的结果,我们定义一个新的度量标准:

$$D(T_1,T_2)=\frac12(D_{owd}(T_1,T_2)+D_{owd}(T_2,T_1))$$

这就是OWD距离中最终用来判定路径相似度的标准。

小结

从OWD距离计算的方式就可以看出,他能够很好的对不同长度的路径间距离进行归一化,而且对于噪声敏感度比较低。

参考

Bin Lin, Jianwen Su, One Way Distance: For Shape Based Similarity Search of Moving Object Trajectories. In Geoinformatica (2008)

基于Swing的简单文本编辑器

Posted on 2016-06-02 | In Java

Java小作业,任务是写一个有改字体颜色大小的文本编辑器。其实相比windows自带的记事本功能还要弱,不过还是拿来练练手了。这里主要也就实现了简单的文件读写和字体等更改操作,还是非常简易的。

实现代码

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class TextEditer extends JFrame {
private static final long serialVersionUID = 1L;
private JScrollPane scrollPane;
public JTextArea textArea;
private JMenuBar menubar;
private TextEditer editer;
public Font font;
private Color fgColor, bgColor;
private JMenu menuFile, menuEdit;
private JMenuItem menuFileNew, menuFileOpen, menuFileSave, menuFileExit;
private JMenuItem menuEditFont, menuEditFgColor, menuEditBgColor;

public TextEditer() {
setDefault();
setMenubar();
setListener();

}

private void setDefault() {
editer = this;
this.setBounds(100, 100, 100, 100);
this.setSize(720, 540);
this.setLocationRelativeTo(null);// 居中
this.setTitle("文本编辑器");
font = new Font("宋体", Font.PLAIN, 28);
textArea = new JTextArea();
textArea.setFont(font);
fgColor = new Color(0, 0, 0);
bgColor = new Color(255, 255, 255);
textArea.setForeground(fgColor);
textArea.setBackground(bgColor);
textArea.setLineWrap(true);// 超出边界自动换行
scrollPane = new JScrollPane(textArea);// 加入滚动条
this.add(scrollPane);

}

private void setMenubar() {
menubar = new JMenuBar();
menuFile = new JMenu("文件(F)");
menuEdit = new JMenu("编辑(E)");
menubar.add(menuFile);
menubar.add(menuEdit);

menuFileNew = new JMenuItem("新建(N)");
menuFileOpen = new JMenuItem("打开(O)");
menuFileSave = new JMenuItem("保存(S)");
menuFileExit = new JMenuItem("退出(X)");
menuFile.add(menuFileNew);
menuFile.add(menuFileOpen);
menuFile.add(menuFileSave);
menuFile.add(menuFileExit);
// 设置alt+X快捷键
menuFile.setMnemonic('F');
menuFileNew.setMnemonic('N');
menuFileOpen.setMnemonic('O');
menuFileSave.setMnemonic('S');
menuFileExit.setMnemonic('X');
// 设置ctrl+X快捷键
menuFileNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
menuFileOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
menuFileSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));

menuEditFont = new JMenuItem("字体(T)");
menuEditFgColor = new JMenuItem("前景色(F)");
menuEditBgColor = new JMenuItem("背景色(B)");
menuEdit.add(menuEditFont);
menuEdit.add(menuEditFgColor);
menuEdit.add(menuEditBgColor);
menuEdit.setMnemonic('E');
menuEditFont.setMnemonic('T');
menuEditFgColor.setMnemonic('F');
menuEditBgColor.setMnemonic('B');

this.setJMenuBar(menubar);
}

private void setListener() {
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

// 设置文本字体,调用之前写的FontChooser类
menuEditFont.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
FontChooser.showDialog(editer, "选择字体", true, textArea.getFont());
textArea.setFont(FontChooser.getResFont());
}
});

// 设置文本前景色
menuEditFgColor.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(editer, "选择前景色", textArea.getForeground());
textArea.setForeground(color);
}
});

// 设置文本背景色
menuEditBgColor.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Color color = JColorChooser.showDialog(editer, "选择背景色", textArea.getBackground());
textArea.setBackground(color);
}
});

// 清空文本区
menuFileNew.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setText("");
}
});

// 选择打开文件
menuFileOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(editer);
File file = chooser.getSelectedFile();
if (file == null)
return;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String line = "";
String ans = "";
while ((line = reader.readLine()) != null) {
ans += line;
}
textArea.setText(ans);
reader.close();
} catch (IOException e1) {
System.out.println("File open failure.");
e1.printStackTrace();
}
}
});

// 保存文件
menuFileSave.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(editer);
File file = chooser.getSelectedFile();
if (file == null)
return;
FileWriter writer;
try {
writer = new FileWriter(file);
writer.write(textArea.getText());
writer.flush();
writer.close();
} catch (IOException e1) {
System.out.println("File open failure.");
e1.printStackTrace();
}
}
});

// 退出
menuFileExit.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

}

public static void main(String[] args) {
TextEditer editer = new TextEditer();
editer.setVisible(true);
}

}

运行截图

基于Swing的FontChooser对话框

Posted on 2016-06-01 | In Java

不知道为什么,原生的Swing有JFileChooser,JColorChooser但是却没有JFontChooser。虽然网上有很多类似的实现,但是鉴于是作业,我还是自己写了一个简单的FontChooser来练练手。而且真正写起来也不是那么的顺畅,果然还是发现了很多的坑。。。

功能分析

  1. 既然是字体选择器,那么至少得能够选择“字体”,“样式”,“大小”。
  2. 这类的对话框一般得是“阻塞“的,即弹出该对话框后,本来的对话框应该是点不动的直到弹出的对话框结束。
  3. 仿照JFileChooser和JColorChooser,这类选择工具类的对话框设计为静态的比较好。
  4. 对话框默认的字体一般是待修改的字体。
  5. 选择成功得返回选择后的结果,选择退出得返回原先的结果。

实现代码

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class FontChooser extends JDialog {

private static final long serialVersionUID = 1L;
private static JDialog dlg;
private static Font font, rawFont;
private static JPanel panel;
private static JComboBox<String> comboBoxFont, comboBoxStyle, comboBoxSize;
private static JLabel text;
private static JButton button;

// 向外部提供的静态方法,可阻塞
public static void showDialog(JFrame parent, String title, boolean modal, Font rawfont) {
dlg = new JDialog(parent, title, modal);
rawFont = rawfont;
font = new Font(rawFont.getFontName(), rawFont.getStyle(), rawFont.getSize());
setDefault();
setComboBox();
dlg.setVisible(true);
}

// 获得取得的字体
public static Font getResFont() {
return font;
}

private static void setDefault() {
dlg.setSize(550, 280);
dlg.setTitle("选择字体");
dlg.setResizable(false);
dlg.setLocationRelativeTo(null);
comboBoxFont = new JComboBox<String>();
comboBoxStyle = new JComboBox<String>();
comboBoxSize = new JComboBox<String>();
panel = new JPanel();
dlg.setContentPane(panel);
panel.setLayout(null);

// 关闭窗口,则不更新字体
dlg.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
font = rawFont;
dlg.dispose();
}
});

JPanel panelFont = new JPanel();
panelFont.add(new JLabel("字体:"));
panelFont.add(comboBoxFont);
panel.add(panelFont);
panelFont.setBounds(10, 10, 150, 60);

JPanel panelStyle = new JPanel();
panelStyle.add(new JLabel("风格:"));
panelStyle.add(comboBoxStyle);
panel.add(panelStyle);
panelStyle.setBounds(10, 70, 150, 60);

JPanel panelSize = new JPanel();
panelSize.add(new JLabel("大小:"));
panelSize.add(comboBoxSize);
panel.add(panelSize);
panelSize.setBounds(10, 130, 150, 60);

button = new JButton("确定");
panel.add(button);
button.setBounds(20, 190, 140, 30);
// 点确定,则关闭窗口,保留结果
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dlg.dispose();
}
});

JPanel panelText = new JPanel();
text = new JLabel();
text.setText("Hello World!");
text.setFont(font);
text.setBorder(BorderFactory.createLoweredBevelBorder());
panelText.setLayout(new BorderLayout());
panelText.add(text);

panelText.setBounds(180, 30, 340, 200);
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setVerticalAlignment(SwingConstants.CENTER);
panel.add(panelText);

}

private static void setComboBox() {
// 获得系统提供的字体
String[] fontnames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for (int i = 0; i < fontnames.length; i++) {
comboBoxFont.addItem(fontnames[i]);
}
comboBoxFont.setPreferredSize(new Dimension(100, 30));
comboBoxFont.setSelectedItem(font.getFontName());
comboBoxFont.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
String s = (String) arg0.getItem();
font = new Font(s, font.getStyle(), font.getSize());
text.setFont(font);
}
}
});

comboBoxStyle.addItem("普通");
comboBoxStyle.addItem("加粗");
comboBoxStyle.addItem("倾斜");
comboBoxStyle.addItem("加粗倾斜");
if (font.getStyle() == Font.BOLD) {
comboBoxStyle.setSelectedItem("加粗");
} else if (font.getStyle() == Font.PLAIN) {
comboBoxStyle.setSelectedItem("普通");
} else if (font.getStyle() == Font.ITALIC) {
comboBoxStyle.setSelectedItem("倾斜");
} else {
comboBoxStyle.setSelectedItem("加粗倾斜");
}

comboBoxStyle.setPreferredSize(new Dimension(100, 30));
comboBoxStyle.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
String s = (String) arg0.getItem();
if (s.equals("普通")) {
font = new Font(font.getFontName(), Font.PLAIN, font.getSize());
} else if (s.equals("加粗")) {
font = new Font(font.getFontName(), Font.BOLD, font.getSize());
} else if (s.equals("倾斜")) {
font = new Font(font.getFontName(), Font.ITALIC, font.getSize());
} else {
font = new Font(font.getFontName(), Font.BOLD | Font.ITALIC, font.getSize());
}
text.setFont(font);
}
}
});

for (int i = 5; i <= 50; i++) {
comboBoxSize.addItem(i + "px");
}
comboBoxSize.setSelectedItem(font.getSize() + "px");
comboBoxSize.setPreferredSize(new Dimension(100, 30));
comboBoxSize.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
String s = (String) arg0.getItem();
s = s.substring(0, s.length() - 2);
font = new Font(font.getFontName(), font.getStyle(), Integer.parseInt(s));
text.setFont(font);
}
}
});
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(720, 540);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
FontChooser.showDialog(frame, "title", true, new Font("宋体", Font.BOLD, 20));
}
}

运行截图

Eclipse下WebService的发布和使用

Posted on 2016-05-26 | In Java

前言

书上和网上有很多介绍WebService、WSDL、SOAP、UDDI概念的内容,大都说的云里雾里。尤其是书上介绍了WSDL、SOAP、UDDI的写法规范,写的天花乱坠,更是让人光看看就不想去了解这个东西了。我觉得这种东西还得实践一下才能知道WebService真正的意义以及WSDL等规范的存在价值。

OK,下面就是本人参照网上各种版本的教程捣鼓出来的WebService的编写和使用方法,不过由于我服务器上没有配置Tomcat、本机又没有域名,所以就没有在UDDI中进行注册。

开发环境

JDK1.8、Tomcat v8.0、Eclipse J2EE  Mars.2

服务端

WebService的服务端其实就是一个普通的Java程序,不过需要注意的是新建项目的时候一定要选择Dynamic Web Project,文档树如下图所示:

我新建的项目名叫MyService,只有一个类,写了如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService {
public String getTime(){
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
public static void main(String[] args) {
MyService ms=new MyService();
System.out.println(ms.getTime());
}
}

输出:2016-05-25 21:47:49

应作业要求我就写了一个显示本机时间的方法,这都不重要了。

到现在,整个项目就是一个简单的网页项目,下面我们就要用他来生成WebService。

新建项目,找到WebService:

点击Next弹出下面的选框:

注意左边这两个滚动条,第一个滑动条表示我要启动这个服务;第二个滑动条表示我要顺便生成一个与他对应的客户端(当然也可以暂时不生成而是等会用WSDL文件来生成)。然后在上面的Service Implementation里选择想要发布的类的名称,我这里就用上面的那个MyService类,最后下面勾上public the Web Service的选框表示我要用UDDI把它发布出去。

然后一路Next,最后选择Start Server。最后他就会弹出UDDI的注册界面让我们来注册这个服务:

先不管了,反正知道有这么个东西,等部署到服务器上的时候再来弄。

这样子我们的服务端的配置就算搞定了,那么问题来了,其他人该怎么来调用我写的这个类呢?

其实在上一步配置完WebService后,我们可以看到在WEB-INF目录下会生成一个子目录叫wsdl/,这个文件夹下有一个文件叫MyService.wsdl。没错,这个就是WSDL。用Eclipse打开会默认在Design界面显示成类似UML的东西(不知道是不是):


当然也可以在Source界面显示成xml代码的形式:

没错,这个就是书上介绍的恶心的WSDL文档,其实这东西完全不用自己写,都是可以由eclipse 帮我们生成。

最后,我们也可以中浏览器中打开这个文件:”http://localhost:8080/MyService/services/MyService?wsdl"。这就意味这只要本机能作为服务器,那么因特网上的任何一台主机都可以访问这个url,并且利用这个内容调用服务器中的服务。

下面就来介绍怎么远程调用这个服务。

客户端

新建项目,选择Web Service Client项目:

在框框中输入需要调用的WSDL文件。我这里调用的就是上面生成的那个URL。

不过需要注意的是我们需要新建一个Dynamic web project,并用他来替换箭头中指向的项目,即把客户端安装到这个项目中。

一路next就可以在目标项目中生成下面的一堆文件:

这就是我们获得的客户端文件了,打开看看才知道,这些东西其实是用RMI写的。。。。。。

然后我们在这个项目中新建一个测试类Test:

1
2
3
4
5
6
7
8
9
10
package test;
import java.rmi.RemoteException;
import DefaultNamespace.MyService;
import DefaultNamespace.MyServiceProxy;
public class Test {
public static void main(String[] args) throws RemoteException {
MyService ms=new MyServiceProxy();
System.out.println(ms.getTime());
}
}

这样就可以像在本机调用MyService类一样的调用那个getTime函数了,最终也返回了正确的结果。

参考资料

使用eclipse开发webService很简单
Java WebService 简单实例

Julia集的win32+GDI演示

Posted on 2016-05-25 | In Others

虽然不是第一次win32来写窗口程序,但是最近python和java用惯了,还真用不惯win api繁琐的调用方法,光是一个模版就好难理解。

事实上,那些模版的玩意写上去就好了,我们只要在他的消息循环的处理里添加绘图的函数即可。

至于绘图,我用的是GDI库最简单的用法,不加缓冲直接逐像素点打印。效率很低,而且会出现刷屏的现象。正确的做法应该是在内存中创建一张Image,向这个里面写再一次性输出。(这样弄效率高但是在网上找了半天没找到傻瓜式的代码模版。。。)

代码

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <windows.h>
#include <gdiplus.h>
#include<time.h>
#include<stdlib.h>
//添加GDI的库,当然也可以直接在属性->连接器->输入->附加依赖项里加这个库名
#pragma comment(lib,"gdiplus.lib")
using namespace Gdiplus;
struct Complex//自定义一个复数类,据说用自带的complex会很慢
{
double real;
double img;
};
void OnPaint(HDC hdc){//每次屏幕重绘时都重新输出一张图
int B[256],G[256],R[256];
srand((unsigned)time(0));//随机生成Julia集的初始状态
Complex c;
c.real=(rand()%20000*1.0-10000)/10000;
c.img=(rand()%20000*1.0-10000)/10000;
int t1=rand()%1000;double t2=rand()%10/10.0;
for(int i=0;i<256;i++)//随机生成配色方案
{
B[i]=i*t1%256;
G[i]=(int)(t2*255)%256;
R[i]=(int)(255.0*(1.0-i/255.0*i/255.0
/1.2)+256)%256;}double dx=5.0/720;

//下面是逃逸时间算法
double dy=5.0/540;
int its=256;
double size=4.0;
for(int row =0;row<540;row++){
for(int col=0;col<720;col++){
int color=0;
Complex z;
z.real=col*dx-2.5;
z.img=row*dy-2.5;
while((color<its)&&((z.img*z.img+z.real*z.real)<size)){
double tmp=z.real*z.real-z.img*z.img+c.real;
z.img=z.img*z.real+z.real*z.img+c.img;
z.real=tmp;color++;
}
if(color>=its)
color=255;
color%=256;
//直接SetPixel绘图会比较简单,但是效率却十分低下
SetPixel(hdc,col,row,RGB(B[color],G[color],R[color]));
}
}
}
//处理消息循环
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
PAINTSTRUCT ps;
switch(message){
case WM_PAINT:
BeginPaint(hWnd,&ps);
OnPaint(ps.hdc);
EndPaint(hWnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
}
//win32程序的入口函数
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,PSTR,INT iCmdShow){
HWND hWnd;//窗口句柄
MSG msg;//消息
WNDCLASS wndClass;
GdiplusStartupInput gdiplusStartupInput;ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
wndClass.style= CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc= WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = TEXT("Julia");
RegisterClass(&wndClass);
//创建窗口
hWnd = CreateWindow(
TEXT("Julia"),
TEXT("Julia"),
WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX,//窗口风格,这里禁用了最大化按钮可伸缩边框
CW_USEDEFAULT,
CW_USEDEFAULT,
725, //窗口宽度
560, //窗口高度
NULL,
NULL,
hInstance,//传入句柄
NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))//处理消息循环
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(gdiplusToken);//释放GDI资源
return msg.wParam;
}

效果图



基于Hexo+github搭建静态博客

Posted on 2016-05-23 | In Hexo博客

开始之前

在安装hexo之前,必须确认你已经安装了Node.js和Git。

Read more »
1…414243…58

574 posts
69 categories
286 tags
© 2024 Companyd
Powered by Hexo
|
Theme — NexT.Muse v5.1.4