繁体   English   中英

在Linux上使用Java CIFS客户端库发送文件

[英]Send file with Java CIFS Client Library on Linux

我试图将包含打印数据(来自Zebra打印机的数据)的文件从Linux机器发送到Windows机器上的共享打印机,但是它不起作用,我尝试了所有操作。 我的最后一个想法是首先尝试在Linux机器上通过命令行工作,然后在Java中执行相同的解决方案,结果是:它通过命令行工作,但在Java中不工作。

我在Linux上的命令行解决方案包括:

smbclient \\\\host\\printer_share -U 'domain/user%pass' -c "put file_name"

smbclient的解决方案可以完美工作,因此我考虑过在Java中使用jCIFS ,但在打印机中不起作用。 在同一主机的共享文件夹中,它可以工作,但在打印机共享中,则不行,但是通过smbclient进行命令行都可以。 有人对我要去哪里有任何想法吗?

我的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错误:

在此处输入图片说明

我对操作系统毫不关心,能够在@HieryNomus的帮助下解决了这个问题,他拥有一个完美的库来实现SMB。 Git链接: https : //github.com/hierynomus/smbj/

对于我的需要,我通过以下实现实现了(这只是我的测试代码):

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);
        }
    }
}

命令变量是发给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

如果命令不起作用:在命令末尾添加\\n

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM