SWUST--Java实验(八)网络编程

xiaoxiao2021-03-01  12

import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.ScrollPaneConstants; import javax.swing.JButton; public class MyURL extends JFrame { private JPanel contentPane; private static JTextField jf; static JTextArea jta; /** * Launch the application. * * @throws IOException */ public static void main(String[] args) throws IOException { new MyURL(); } /** * Create the frame. */ public MyURL() { setTitle("URL\u7A0B\u5E8F"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 293, 353); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("URL\u8DEF\u5F84:"); lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 18)); lblNewLabel.setBounds(10, 21, 73, 26); contentPane.add(lblNewLabel); jf = new JTextField(); jf.setFont(new Font("微软雅黑", Font.PLAIN, 16)); jf.setBounds(10, 57, 257, 33); contentPane.add(jf); jf.setColumns(10); JPanel panel = new JPanel(); panel.setBounds(10, 123, 257, 192); contentPane.add(panel); panel.setLayout(new BorderLayout(0, 0)); JScrollPane scrollPane = new JScrollPane(); panel.add(scrollPane, BorderLayout.CENTER); jta = new JTextArea(); jta.setLineWrap(true); jta.setEditable(false); scrollPane.setViewportView(jta); JLabel lblNewLabel_1 = new JLabel("HTML\u6E90\u7801:"); lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 14)); lblNewLabel_1.setBounds(10, 91, 73, 33); contentPane.add(lblNewLabel_1); JButton bt = new JButton("\u83B7\u53D6"); bt.setFont(new Font("宋体", Font.PLAIN, 14)); bt.setBounds(171, 22, 81, 26); contentPane.add(bt); bt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { URL url = new URL(jf.getText()); URLConnection ulc = url.openConnection(); HttpURLConnection huc = (HttpURLConnection) ulc;//可加可不加 int responseCode = huc.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { System.err.println("成功"); FileWriter fos = new FileWriter("D:\\index.html"); BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream())); String str = null; while ((str = br.readLine()) != null) { jta.append(str+'\n'); fos.write(str+'\n'); } fos.close(); br.close(); huc.disconnect(); }else { System.err.println("失败!"); } } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }); setVisible(true); } }

 

转载请注明原文地址: https://www.6miu.com/read-4149924.html

最新回复(0)