Java Regex

示例

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
Pattern imgPattern = Pattern.compile("<img (.+?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Pattern videoPattern = Pattern.compile("<video (.+?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Pattern filePattern = Pattern.compile("<a (.+?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

Pattern urlPattern = Pattern.compile("(http(s?):\\/\\/|www\\.)(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

private String keyword = "cms-download";

String content = "<img class=\"cms-download\" src=\"http://abc.com\">abc";

Matcher imgMatcher = imgPattern.matcher(content);
List<String> oldUrl = new ArrayList<>();
List<String> newUrl = new ArrayList<>();
while (imgMatcher.find()) {
String imgTag = content.substring(imgMatcher.start(1), imgMatcher.end());
if (imgTag.contains(keyword)) {
Matcher urlMatcher = urlPattern.matcher(imgTag);
if (urlMatcher.find()) {
String url = imgTag.substring(urlMatcher.start(1), urlMatcher.end());
oldUrl.add(url);
newUrl.add(downloadImage(url, site));
}
}
}
for (int i = 0; i < oldUrl.size(); i++) {
content = content.replace(oldUrl.get(i), newUrl.get(i));
}
return content;

Java 安装

OpenJDK 下载

Java 8 下载

补充:官方不提供Windows下的zip版本下载

Linux 下安装

1.7

1
2
3
4
5
6
7
8
9
10
11
12
13
cd ~/download
wget jdk-7u80-linux-x64.tar.gz
mkdir /usr/local/java && cd /usr/local/java
tar xzvf ~/download/jdk-7u80-linux-x64.tar.gz
mv jdk1.7.0_80/ jdk1.7

vi ~/.bash_profile

export JAVA_HOME=/usr/local/java/jdk1.7
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

source ~/.bash_profile

1.8

1
2
3
4
5
6
7
8
9
10
11
12
13
cd ~/download
wget jdk-8u231-linux-x64.tar.gz
mkdir /usr/local/java && cd /usr/local/java
tar xzvf ~/download/jdk-8u231-linux-x64.tar.gz
mv jdk1.8.0_231/ jdk1.8

vi ~/.bash_profile

export JAVA_HOME=/usr/local/java/jdk1.8
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

source ~/.bash_profile

Java HTTP

响应

JSON Response

1
2
3
4
5
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("[]");
out.flush();

上传文件

HttpClient

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
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(postUrl);
MultipartEntity mpEntity = new MultipartEntity();
Iterator<Map.Entry<String, Object>> iter = formParams.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Object> entry = iter.next();
String inputName = entry.getKey();
String inputValue = entry.getValue().toString();
mpEntity.addPart(inputName, new StringBody(inputValue, Charset.defaultCharset()));
}
mpEntity.addPart("file", new FileBody(file, "image/jpeg"));
httppost.setEntity(mpEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response2 = null;
response2 = httpclient.execute(httppost);

HttpEntity resEntity = response2.getEntity();
System.out.println(response2.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
httpclient.getConnectionManager().shutdown();
} catch (IOException e) {
e.printStackTrace();
}

下载文件

Java IO

1
2
3
4
5
6
7
8
9
10
try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream());
FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) {
byte dataBuffer[] = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
fileOutputStream.write(dataBuffer, 0, bytesRead);
}
} catch (IOException e) {
// handle exception
}

Java 7

1
2
InputStream in = new URL(FILE_URL).openStream();
Files.copy(in, Paths.get(FILE_NAME), StandardCopyOption.REPLACE_EXISTING);

Read contents as String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static String URLReader(URL url) throws IOException {
StringBuilder sb = new StringBuilder();
String line;

InputStream in = url.openStream();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
} finally {
in.close();
}

return sb.toString();
}

Java NIO

The transferTo() and transferFrom() methods are more efficient than simply reading from a stream using a buffer. Depending on the underlying operating system, the data can be transferred directly from the filesystem cache to our file without copying any bytes into the application memory.

On Linux and UNIX systems, these methods use the zero-copy technique that reduces the number of context switches between the kernel mode and user mode.

1
2
3
4
URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Apache Commons IO

1
FileUtils.copyURLToFile(new URL(FILE_URL), new File(FILE_NAME), CONNECT_TIMEOUT, READ_TIMEOUT);

判断文件类型

Java IO & NIO - Files.probeContentType()

1
2
3
4
path = Files.createTempFile("test-file", ".html");
System.out.println("Probe Content Type for file: " + path);
type = Files.probeContentType(path);
System.out.println("File Content type: " + type);

Determining a File Type In Java

MimetypesFileTypeMap.getContentType(String)

1
2
final MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
fileTypeMap.getContentType(fileName);

URLConnection.getContentType()

1
2
3
final URL url = new URL("file://" + fileName);
final URLConnection connection = url.openConnection();
fileType = connection.getContentType();

Apache Tika

1
2
3
Tika tika = new Tika(); 
tika.detect(istream);
tika.detect(file);

Maven

查看信息

  • 查看java系统属性和环境变量 mvn help:system
  • 查看有效settings文件信息 mvn help:effective-settings

命令行生成骨架项目

java项目

1
mvn archetype:generate -DgroupId=cc.aneat.example -DartifactId=example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

java web项目

1
mvn archetype:generate -DgroupId=com.travelight.framework -DartifactId=core -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

pom文件结构

Project Object Model

名称
modelVersion pom的版本只能是4.0.0
groupId 组id
artifactId 模块id
version 版本号
packaging 打包方式 (可选)
name 名称 (可选)
url 可选

Dependency Mechanism

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html