我编写了这个小型java程序,将文件直接发送到要打印的打印机:
public static void main(String args[]) throws IOException
{
try{
InputStream in= new FileInputStream(new File("C:\\example.txt"));
OutputStream out=new FileOutputStream(new File("\\\\path\\printer\\example.txt"));
// Transfer bytes from in to out
byte[] buf=new byte[1024];
int len;
while ((len=in.read(buf)) > 0) {
out.write(buf,0,len);
}
in.close();
out.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
这在Windows中运行良好,但不适用于Mac。
任何想法我如何在Mac Os X上做同样的事情?
请注意,我尝试将文件从一个文件夹复制到mac机器本地的另一个文件夹并且可以正常工作,但是当FileOutputStream获取远程文件夹时却没有。
答案 0 :(得分:0)
对于文件共享,您可以将类似的UNC路径转换为类似
的路径smb://server/folder/file.txt
但我不相信打印机份额是可能的;我认为它涉及更多。 SMB打印机URL看起来像
smb://username.password@workgroup/computer/printer
在任何情况下,FileOutputStream
都无法写入网址,因此无法以这种方式工作。
Java有一个完整的打印API,并不是很难使用;如果你掌握它,你可以编写一个便携式程序,在每个平台上解决这个问题。