1、内网穿透
内网穿透的目的是:让外网能访问你本地的应用
ngrok是一个反向代理,通过在公共的端点和本地运行的Web服务器之间建立一个安全的通道。
ngrok使用go语言开发,源代码分为客户端与服务器端。
国内免费服务器:http://ngrok.ciqiuwl.cn/
1,下载windows版本的客户端,解压到你喜欢的目录
2,在命令行下进入到ngrok客户端目录下
3,执行 ngrok -config=ngrok.cfg -subdomain xxx 80 //(xxx 是你自定义的域名前缀)
4,如果开启成功 你就可以使用 xxx.ngrok.xiaomiqiu.cn 来访问你本机的 127.0.0.1:80 的服务啦
5,如果你自己有顶级域名,想通过自己的域名来访问本机的项目,那么先将自己的顶级域名解析到120.78.180.104(域名需要已备案哦),然后执行 ngrok -config=ngrok.cfg -hostname xxx.xxx.xxx 80 //(xxx.xxx.xxx是你自定义的顶级域名)
6,如果开启成功 你就可以使用你的顶级域名来访问你本机的 127.0.0.1:80 的服务啦
这个是做个临时的服务,如果要长期的,需要自己搭建服务器。
2、读取文件的最后一行
File file = new File(“F:/2”);
RandomAccessFile accessFile = new RandomAccessFile(file, “r”);
long len = accessFile.length();
long length = len - 1;
long pos = length;
while (pos > 0) {
accessFile.seek(pos);
byte b = accessFile.readByte();
if (b == ‘\n’) {
accessFile.seek(pos + 1);
break;
}
pos–;
}
if (pos == 0) accessFile.seek(pos);
byte[] m = new byte[(int) (length - pos)];
accessFile.read(m);
String s = new String(m, Charset.forName(“utf-8”));
System.out.println(s);
accessFile.close();
最后一行为空和最后一行不为空也有些差别,按具体情况处理。
3、xml解析
XML的解析方式分为四种:1、DOM解析;2、SAX解析;3、JDOM解析;4、DOM4J解析。其中前两种属于基础方法,是官方提供的平台无关的解析方式;后两种属于扩展方法,它们是在基础的方法上扩展出来的,只适用于java平台。
book.xml
NodeList list = document.getElementsByTagName("book");
int len = list.getLength();
System.out.println("一共" + len + "本书");
for (int i = 0; i < len; i++) {
Node node = list.item(i);
NodeList childList = node.getChildNodes();
int v = childList.getLength();
for (int j = 0; j < v; j++) {
Node m = childList.item(j);
if (m.getNodeType() == node.ELEMENT_NODE)
System.out.println(m.getNodeName() + ":" + m.getTextContent() + "\t");
}
}
2)SAX的全称是Simple APIs for XML:编码比较麻烦,很难同时访问XML文件中的多处不同数据
public class SAXParserHandler extends DefaultHandler{
String value = null;
Book book = null;
int bookIndex = 0;
private ArrayList<Book> bookList = new ArrayList<Book>();
public ArrayList<Book> getBookList() {
return bookList;
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(uri, localName, qName, attributes);
if (qName.equals("book")) {
bookIndex++;
book = new Book();
int num = attributes.getLength();
for (int i = 0; i < num; i++) {
System.out.println(attributes.getQName(i) + ":"
+ attributes.getValue(i));
if (attributes.getQName(i).equals("id")) {
book.setId(attributes.getValue(i));
}
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if (qName.equals("book")) {
bookList.add(book);
book = null;
} else if (qName.equals("name")) {
book.setName(value);
}
else if (qName.equals("author")) {
book.setAuthor(value);
}
else if (qName.equals("year")) {
book.setYear(value);
}
else if (qName.equals("price")) {
book.setPrice(value);
}
else if (qName.equals("language")) {
book.setLanguage(value);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if (ch != null) {
value = new String(ch, start, length);
if (!value.trim().equals("")) {
System.out.println("节点值是:" + value);
}
}
}
}
执行方法
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parse = factory.newSAXParser();
InputStream stream = Test6.class.getResourceAsStream(“book.xml”);
SAXParserHandler handler = new SAXParserHandler();
parse.parse(stream, handler);
for (Book book : handler.getBookList()) {
System.out.println(book.getId());
System.out.println(book.getName());
System.out.println(book.getAuthor());
System.out.println(book.getYear());
System.out.println(book.getPrice());
System.out.println(book.getLanguage());
System.out.println("----finish----");
}
3)JDom解析
private static ArrayList booksList = new ArrayList();
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder saxBuilder = new SAXBuilder();
InputStream in;
in = Test7.class.getResourceAsStream(“book.xml”);
Document document = saxBuilder.build(in);
Element element = document.getRootElement();
List list = element.getChildren();
int len = list.size();
for (int i = 0; i < len; i++) {
Book bookEntity = new Book();
Element ele = list.get(i);
List attributesList = ele.getAttributes();
for (Attribute attr : attributesList) {
String attrName = attr.getName();
String attrValue = attr.getValue();
if (attrName.equals(“id”)) {
bookEntity.setId(attrValue);
}
}
List bookChilds = ele.getChildren();
for (Element child : bookChilds) {
System.out.println(“节点名:” + child.getName() + “----节点值:”
+ child.getValue());
if (child.getName().equals(“name”)) {
bookEntity.setName(child.getValue());
}
else if (child.getName().equals(“author”)) {
bookEntity.setAuthor(child.getValue());
}
else if (child.getName().equals(“year”)) {
bookEntity.setYear(child.getValue());
}
else if (child.getName().equals(“price”)) {
bookEntity.setPrice(child.getValue());
}
else if (child.getName().equals(“language”)) {
bookEntity.setLanguage(child.getValue());
}
}
booksList.add(bookEntity);
bookEntity = null;
System.out.println(booksList.size());
System.out.println(booksList.get(0).getId());
System.out.println(booksList.get(0).getName());
}
}
4)DOM4J性能最好
SAXReader reader = new SAXReader();
InputStream input = Test8.class.getResourceAsStream(“book.xml”);
Document document = reader.read(input);
Element bookStore = document.getRootElement();
Iterator it = bookStore.elementIterator();
while (it.hasNext()) {
Element book = (Element) it.next();
List bookAttrs = book.attributes();
for (Attribute attr : bookAttrs) {
System.out.println(“属性名:” + attr.getName() + “–属性值:”
+ attr.getValue());
}
Iterator itt = book.elementIterator();
while (itt.hasNext()) {
Element bookChild = (Element) itt.next();
System.out.println(“节点名:” + bookChild.getName() + “–节点值:” + bookChild.getStringValue());
}
System.out.println("=结束遍历某一本书=");
}
3、DecimalFormat用法
“0” 指定位置不存在数字则显示为0
“#” 指定位置不存在数字则不显示
DecimalFormat df1 = new DecimalFormat(“0.0”);
DecimalFormat df2 = new DecimalFormat("#.#");
DecimalFormat df3 = new DecimalFormat(“000.000”);
DecimalFormat df4 = new DecimalFormat("###.###");
System.out.println(df1.format(12.34));
System.out.println(df2.format(12.34));
System.out.println(df3.format(12.34));
System.out.println(df4.format(12.34));
结果
12.3
12.3
012.340
12.34
4、http访问页面资源
需要的包commons-cli.jar、commons-codec.jar、commons-logging.jar、httpcore.jar、httpclient.jar
Get方法访问
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(“http://www.baidu.com”);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
System.out.println("响应状态:" + response.getStatusLine());
if (entity != null) {
System.out.println("响应内容:" + EntityUtils.toString(entity));
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Post方法
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
StringBuilder sb = new StringBuilder();
sb.append("username=zhangsan&password=123&address=北京");
HttpPost httpPost = new HttpPost("http://localhost:8084/user/getInfo?" + sb.toString());
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
System.out.println("返回状态:" + response.getStatusLine());
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
System.out.println("响应内容:" + EntityUtils.toString(httpEntity));
}
} catch (IOException e) {
e.printStackTrace();
}
5、加密
Base64用于网络中传输的数据进行编码,严格意义上属于编码的格式,有64个字符的对应的编码,Base64就是将内容按照该格式进行编码。可以对数据编码和解码,是可逆的,安全度较低,不过,也可以作为最基础最简单的加密算法用于加密要求较弱的情况
String str = “今天天气真好,123abcdef”;
Encoder base = Base64.getEncoder();
String s = base.encodeToString(str.getBytes());
System.out.println("加密后:" + s);
Decoder decoder = Base64.getDecoder();
byte[] b = decoder.decode(s);
System.out.println("解密后:" + new String(b));
摘要算法主要分为MD,SHA和Hmac算法,摘要算法其实是用于效验数据完整性的
MessageDigest digest = MessageDigest.getInstance(“MD5”);
String str = “加密字段abc”;
byte[] b = str.getBytes();
digest.update(b);
byte[] f = digest.digest();
char[] hexDigits = {‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘a’,‘b’,‘c’,‘d’,‘e’,‘f’};
char[] resultCharArray = new char[f.length*2];
int index = 0;
for(byte m : f){
resultCharArray[index++] = hexDigits[m>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[m& 0xf];
}
System.out.println(“加密后:” + new String(resultCharArray));
MessageDigest digest = MessageDigest.getInstance(“SHA1”);
String str = “加密字段abc”;
byte[] b = str.getBytes();
digest.update(b);
byte[] f = digest.digest();
char[] hexDigits = {‘0’,‘1’,‘2’,‘3’,‘4’,‘5’,‘6’,‘7’,‘8’,‘9’,‘a’,‘b’,‘c’,‘d’,‘e’,‘f’};
char[] resultCharArray = new char[f.length*2];
int index = 0;
for(byte m : f){
resultCharArray[index++] = hexDigits[m>>> 4 & 0xf];
resultCharArray[index++] = hexDigits[m& 0xf];
}
System.out.println(“加密后:” + new String(resultCharArray));