こんにちは、良い一日を。
Web アプリを開発していて、ドット マトリックス プリンターを使用する必要があります。プリンターは USB ポートを使用していますが、この手順に従えば生データをプリンターに正常に送信できます。
コードは次のとおりです。
package print;
import java.io.*;
public class lpt {
public static void main(String[] argv) {
try {
FileOutputStream os = new FileOutputStream("LPT1");
PrintStream ps = new PrintStream(os);
String text = "this is a test -- second line here -- end";
text = text.toUpperCase();
String[] row = text.split("--");
for (int i = 0; i < row.length; i++) {
int size = row[i].length();
String line = row[i];
if (line.endsWith("--")) {
line = line.substring(0, size - 1);
}
ps.println(line);
}
ps.print("\f");
ps.close();
} catch (Exception e) {
System.out.println("Exception occurred: " + e);
}
}
}
このコードは単独で完全に機能しますが、移行 (一部のコードを変更) してアプレットとして使用する場合。
package print;
import java.applet.Applet;
import java.io.*;
public class lpt extends Applet {
public void init() {
try {
FileOutputStream os = new FileOutputStream("LPT1");
PrintStream ps = new PrintStream(os);
String text = "this is a test -- second line here -- end";
text = text.toUpperCase();
String[] row = text.split("--");
for (int i = 0; i < row.length; i++) {
int size = row[i].length();
String line = row[i];
if (line.endsWith("--")) {
line = line.substring(0, size - 1);
}
ps.println(line);
}
ps.print("\f");
ps.close();
} catch (Exception e) {
System.out.println("Exception occurred: " + e);
}
}
}
Web 上では機能しません。jnlp を deployJava.js で使用するように構成しましたが、このコードでは実行されません。Web アプリ (PHP で作成) にこのアプレットが必要で、コンプロバントとチケット (はい、販売システム)。まだ printerJob と Graphics クラスを使用していましたが、印刷がゆがんでしまい、以前のソリューションの方が優れていて高速です。