在java中实现对FORM的打印功能

在JAVA实现对FORM的打印功能

public void print(JFrame frame)
{
  Container container=frame.getContentPane();
  Properties prop=new Properties();

  PrintJob job = getToolkit().getPrintJob(frame,"test",prop);
  // PrintJob job = getToolkit().getPrintJob(this,"test",prop);
  if(job != null)
  {
  Graphics pg = job.getGraphics() ;
  if ( pg != null )
  {
  pg.translate((job.getPageDimension().width-getSize().width)/2,
  (job.getPageDimension().height-getSize().height)/2);
  //old code - container.print(pg);
  print(pg);
  pg.dispose() ;

  job.end();
  }
  }
  job=null;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java发送multipart/form-data类型的POST请求,可以通过使用Java的HttpURLConnection类或Apache的HttpClient库来实现。 使用HttpURLConnection类的示例代码如下: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class MultipartFormDataExample { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("http://example.com/upload"); // 打开连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为POST connection.setRequestMethod("POST"); // 设置Content-Type为multipart/form-data connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"); // 允许输入输出流 connection.setDoInput(true); connection.setDoOutput(true); // 创建请求体 String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW"; String CRLF = "\r\n"; // 回车换行符 // 设置请求参数 String data = "--" + boundary + CRLF + "Content-Disposition: form-data; name=\"file\"; filename=\"example.txt\"" + CRLF + "Content-Type: text/plain" + CRLF + CRLF + "This is the content of the file" + CRLF + "--" + boundary + "--" + CRLF; // 获取输出流 OutputStream outputStream = connection.getOutputStream(); // 写入请求体 outputStream.write(data.getBytes()); // 关闭输出流 outputStream.close(); // 获取响应状态码 int responseCode = connection.getResponseCode(); // 处理响应结果 if (responseCode == HttpURLConnection.HTTP_OK) { // 获取响应输入流 InputStream inputStream = connection.getInputStream(); // 读取响应内容 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { response.append(line); } // 关闭输入流 inputStream.close(); // 打印响应内容 System.out.println(response.toString()); } else { System.out.println("POST request failed with response code: " + responseCode); } // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } } ``` 注意,在代码需要替换URL、boundary、filename和请求体的内容为实际的值。 Apache HttpClient库也提供了发送multipart/form-data类型请求的功能,使用方法请参考相关文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

programlover

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值