Java如何调用系统打印机打印文件
4个回答
展开全部
1、Desktop.getDesktop().print(new File("c:/123.jpg"));
2、private void printFileAction()
{
//构造一个文件选择器,默认为当前目录
JFileChooser fileChooser = new JFileChooser(SystemProperties.USER_DIR);
int state = fileChooser.showOpenDialog(this);//弹出文件选择对话框
if (state == fileChooser.APPROVE_OPTION)//如果用户选定了文件
{
File file = fileChooser.getSelectedFile();//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null)
{
try
{
DocPrintJob job = service.createPrintJob();//创建打印作业
FileInputStream fis = new FileInputStream(file);//构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);//建立打印文件格式
job.print(doc, pras);//进行文件的打印
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
在上面的示例中,因尚未确定文件的类型,所以将指定文件的打印格式定义为DocFlavor.INPUT_STREAM.AUTOSENSE。事实上,如果在进行打印之前,就已确定地知道文件的格式,如为GIF,就应定义为DocFlavor.INPUT_STREAM.GIF ;如为PDF,就应该定义为DocFlavor.INPUT_STREAM.PDF;如为纯ASCII文件,就可以定义为 DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII。等等。jdk1.4的javax.print.DocFlavor提供了极为丰富的文件流类型,你可以根据具体的应用需求进行合适的选择。
2、private void printFileAction()
{
//构造一个文件选择器,默认为当前目录
JFileChooser fileChooser = new JFileChooser(SystemProperties.USER_DIR);
int state = fileChooser.showOpenDialog(this);//弹出文件选择对话框
if (state == fileChooser.APPROVE_OPTION)//如果用户选定了文件
{
File file = fileChooser.getSelectedFile();//获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null)
{
try
{
DocPrintJob job = service.createPrintJob();//创建打印作业
FileInputStream fis = new FileInputStream(file);//构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);//建立打印文件格式
job.print(doc, pras);//进行文件的打印
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
在上面的示例中,因尚未确定文件的类型,所以将指定文件的打印格式定义为DocFlavor.INPUT_STREAM.AUTOSENSE。事实上,如果在进行打印之前,就已确定地知道文件的格式,如为GIF,就应定义为DocFlavor.INPUT_STREAM.GIF ;如为PDF,就应该定义为DocFlavor.INPUT_STREAM.PDF;如为纯ASCII文件,就可以定义为 DocFlavor.INPUT_STREAM.TEXT_HTML_US_ASCII。等等。jdk1.4的javax.print.DocFlavor提供了极为丰富的文件流类型,你可以根据具体的应用需求进行合适的选择。
推荐于2016-10-31
展开全部
如下:
public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost(url);
httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
public static String do_get(String url) throws ClientProtocolException, IOException {
String body = "{}";
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
body = EntityUtils.toString(entity);
} finally {
httpclient.getConnectionManager().shutdown();
}
return body;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
具体步骤如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.JFileChooser;
public class PrintDemo {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser(); // 创建打印作业
File file = new File("f:/111.txt"); // 获取选择的文件
// 构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
InputStream fis = null;
try {
DocPrintJob job = defaultService.createPrintJob(); // 创建打印作业
fis = new FileInputStream(file); // 构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.JFileChooser;
public class PrintDemo {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser(); // 创建打印作业
File file = new File("f:/111.txt"); // 获取选择的文件
// 构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
InputStream fis = null;
try {
DocPrintJob job = defaultService.createPrintJob(); // 创建打印作业
fis = new FileInputStream(file); // 构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
本人印证:运行后弹窗选取本地需打印的文件;稍等一会儿,系统会自动调用本地打印程序待确认,然后就跟平时打印一样了。
import java.io.File;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.JFileChooser;
public class TestController {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();//创建打印作业
int state = fileChooser.showOpenDialog(null);
if(state == fileChooser.APPROVE_OPTION){
//File file = new File("D:/haha.txt");//获取选择的文件
File file = fileChooser.getSelectedFile();//获取选择的文件
//构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService,defaultService, flavor, pras);
if(service != null){
try {
DocPrintJob job = service.createPrintJob();//创建打印作业
FileInputStream fis = new FileInputStream(file);//构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.swing.JFileChooser;
public class TestController {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser();//创建打印作业
int state = fileChooser.showOpenDialog(null);
if(state == fileChooser.APPROVE_OPTION){
//File file = new File("D:/haha.txt");//获取选择的文件
File file = fileChooser.getSelectedFile();//获取选择的文件
//构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService,defaultService, flavor, pras);
if(service != null){
try {
DocPrintJob job = service.createPrintJob();//创建打印作业
FileInputStream fis = new FileInputStream(file);//构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询