[英]Send file with Java CIFS Client Library on Linux
I tried to send a file with print data (data from a Zebra printer) from the Linux machine to a shared printer on the Windows machine, but it did not work, I tried everything. 我试图将包含打印数据(来自Zebra打印机的数据)的文件从Linux机器发送到Windows机器上的共享打印机,但是它不起作用,我尝试了所有操作。 My last idea was to first try to work by command line on the Linux machine and then do the same solution in Java, and the result was: it works by command line but not in Java.
我的最后一个想法是首先尝试在Linux机器上通过命令行工作,然后在Java中执行相同的解决方案,结果是:它通过命令行工作,但在Java中不工作。
My command line solution on Linux has: 我在Linux上的命令行解决方案包括:
smbclient \\\\host\\printer_share -U 'domain/user%pass' -c "put file_name"
The solution with smbclient works perfectly, so I thought about using jCIFS in Java, but it does not work in the printer. smbclient的解决方案可以完美工作,因此我考虑过在Java中使用jCIFS ,但在打印机中不起作用。 In a shared folder of the same host it works, but in the printer share no, however by command line with smbclient works both work.
在同一主机的共享文件夹中,它可以工作,但在打印机共享中,则不行,但是通过smbclient进行命令行都可以。 Anyone have any ideas where I'm going wrong?
有人对我要去哪里有任何想法吗?
My java code: 我的Java代码:
public static void sendFileToPrinter(String commandsToPrinter) {
String user = "user";
String pass = "pass";
String domain = "domain";
String path = "smb://host/printer_share/file_to_print";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain, user, pass);
SmbFile smbFile = new SmbFile(path, auth);
SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
smbfos.write(commandsToPrinter.getBytes());
System.out.println("Work");
}
Java error: Java错误:
Indifferently of the operational system i was able to solve the problem with the help of @HieryNomus, who has one library perfect to implement SMB. 我对操作系统毫不关心,能够在@HieryNomus的帮助下解决了这个问题,他拥有一个完美的库来实现SMB。 Git link: https://github.com/hierynomus/smbj/
Git链接: https : //github.com/hierynomus/smbj/
For my need, I achieved through the following implementation (this is only my test code): 对于我的需要,我通过以下实现实现了(这只是我的测试代码):
public static void sendCommandToZebraPrinter(String command) throws MalformedURLException, SmbException, IOException {
String username = "username";
String password = "password";
String domain = "mydomain";
String sharedDirectory = "PRINTER_SHARE";
String computerName = "MYCOMPUTER";
SMBClient client = new SMBClient();
try (Connection connection = client.connect(computerName)) {
AuthenticationContext ac = new AuthenticationContext(username, password.toCharArray(), domain);
Session session = connection.authenticate(ac);
try (PrinterShare share = (PrinterShare) session.connectShare(sharedDirectory)) {
InputStream stream = new ByteArrayInputStream(command.concat("\n").getBytes(StandardCharsets.UTF_8));
share.print(stream);
}
}
}
Command variable is the EPL command to Zebra printer (GC420t), such as: 命令变量是发给Zebra打印机(GC420t)的EPL命令,例如:
I8,A,001
Q104,024
q863
rN
S2
D11
ZT
JF
OD
R172,0
f100
N
75,33,D,h3,"1"
b363,39,D,h2,"TEST"
b198,33,D,h3,"TEST"
LO154,4,1,73
LO280,4,1,73
A149,27,2,2,1,1,N,"1"
A272,26,2,3,1,1,N,"TEST"
A425,26,2,3,1,1,N,"TEST"
P1
IF THE COMMAND DOES NOT WORK: Add \\n
at the end of the command. 如果命令不起作用:在命令末尾添加
\\n
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.