Posted at

[java tool]ipmsgで定時的に送信させるツール

以前仕事中によく使われていた共通メソッド

※Javaの開発


[java tool]ipmsgで定時送信ツール
Mail ⇒ バッチ(vbs/batch)で作成した。
■CatchInfo.java
package src.main.java;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CatchInfo {
private static final String path = "C:\installdir\IPMsg\ipmsg.exe";
private static final String file_1 = "C:\ipmsg\ipmsg.log";
private static final String file_2 = "C:\Users\abc\Documents\ipmsg.log";
private static final String space = " ";
private static final String head = " at ";
private static final String MOJI_CODE = "UTF-8";
private static final String Enter = "#Enter#";
private static final String Mark_1 = "\"";
public static void main(String[] args) {
String date[] = add(null, 0, 0,
Integer.valueOf(captureForFutrue("-1"), 16) - 2, 0, 0, 0, 0)
.getTime().toString().split(space);
String matchString = head + date[0] + space + date[1] + space + date[2]
+ space;
String batch = args[0];
String arg1 = path;
String arg2 = getA();
String arg3 = "Scanner Windows";
File file1 = new File(path);
File file2 = new File(batch);
try {
if (file1.exists() && file1.isFile() && file2.exists()
&& file2.isFile()) {
File file3 = new File(file_1);
File file4 = new File(file_2);
if (file3.exists() || file4.exists()) {
if (file3.exists()) {
arg3 = gettodaymsg(file_1, matchString);
} else if (file4.exists()) {
arg3 = gettodaymsg(file_2, matchString);
}
execute(batch, arg1, arg2, arg3);
}
}
} catch (IOException e1) {
try {
execute(batch, arg1, arg2, e1.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
} catch (InterruptedException e2) {
try {
execute(batch, arg1, arg2, e2.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
}
}
}
private static String gettodaymsg(String fileName, String matchString)
throws IOException {
String arg3 = Enter;
boolean startLine = false;
boolean onlyoncetime = true;
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), MOJI_CODE));
String inRecord = "";
while ((inRecord = br.readLine()) != null) {
if (onlyoncetime) {
if (inRecord.contains(matchString)) {
startLine = true;
onlyoncetime = false;
}
}
if (startLine) {
arg3 += inRecord + Enter;
}
}
return arg3;
}
private static void execute(String batch, String arg1, String arg2,
String arg3) throws IOException, InterruptedException {
Process ps = Runtime.getRuntime().exec(
new String[] {
batch,
arg1,
arg2,
Mark_1 + "###" + getNowTime() + "###" + c(arg3) + Enter
+ Mark_1 });
InputStream in = ps.getInputStream();
int c = Integer.MAX_VALUE;
while ((c = in.read()) != -1) {
}
in.close();
ps.waitFor();
}
private static String c(String arg3) {
String c = u(arg3);
int k = arg3.getBytes().length;
if (8000 < k)
return "CUT###" + d(c, 7994);
return c;
}
private static String u(String arg3) {
return arg3.replaceAll(Mark_1, "");
}
public static String d(String value, int length) {
try {
if (value.length() >= length)
return value.substring(value.length() - length);
else
return value.substring(1);
} catch (Exception e) {
return value;
}
}
public static final String SystemCdeefeeDefault = "0C0,0A8,065,0E6";
public static final String SystemCdeefeeDefault1 = "{0}.{1}.{2}.{3}";
public static String captureForFutrue(String target) {
char ch = 0;
String validStr = "";
for (int i = 0; i < target.length(); i++) {
ch = target.charAt(i);
if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f')
|| (ch >= 'A' && ch <= 'F')) {
validStr += ch;
}
}
if ("".equals(validStr)) {
validStr = "0";
}
return validStr;
}
public static Calendar add(Calendar cal, int addYera, int addMonth,
int addDate, int addHour, int addMinute, int addSecond,
int addMillisecond) {
if (cal == null) {
cal = Calendar.getInstance();
}
cal.add(Calendar.YEAR, addYera);
cal.add(Calendar.MONTH, addMonth);
cal.add(Calendar.DATE, addDate);
cal.add(Calendar.HOUR_OF_DAY, addHour);
cal.add(Calendar.MINUTE, addMinute);
cal.add(Calendar.SECOND, addSecond);
cal.add(Calendar.MILLISECOND, addMillisecond);
return cal;
}
public static String getA() {
String ipbefore[] = SystemCdeefeeDefault.split(",");
Object after[] = new Object[4];
for (int i = 0; i < ipbefore.length; i++) {
int s = Integer.valueOf(captureForFutrue(ipbefore[i]), 16);
after[i] = s;
}
return MessageFormat.format(SystemCdeefeeDefault1, after);
}
private static String getNowTime() {
return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
}
}