java从web服务器读取文件


通过webURL地址才获取文件
因为不管web服务器安装在什么位置,其对外显示的URL地址总是不变的,如http://xx.xx.xx.xx/xx 那么要返回http://xx.xx.xx.xx/xx/xx.doc 文件的话,可以采用如下方式,没有做过实验,不过类似于网络爬虫中的文件获取。

URL url = new URL(urlString); 
URLConnection conn = url.openConnection();  
BufferedReader reader = new BufferedReader(new InputStreamReader(conn. 
getInputStream()));  
获取网络流后再返回文件流。

示例代码演示如何从web服务器读取文件并显示。示例代码如下

package ViewRemoteFile;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class ViewRemoteFile extends JApplet
    implements ActionListener {
  // Button to view the file
  private JButton jbtView = new JButton("View");

  // Text field to receive file name
  private JTextField jtfURL = new JTextField(12);

  // Text area to store file
  private JTextArea jtaFile = new JTextArea();

  // Label to display status
  private JLabel jlblStatus = new JLabel();

  /** Initialize the applet */
  public void init() {
    // Create a panel to hold a label, a text field, and a button
    JPanel p1 = new JPanel();
    p1.setLayout(new BorderLayout());
    p1.add(new JLabel("Filename"), BorderLayout.WEST);
    p1.add(jtfURL, BorderLayout.CENTER);
    p1.add(jbtView, BorderLayout.EAST);

    // Place text area and panel p to the applet
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(new JScrollPane(jtaFile),
      BorderLayout.CENTER);
    getContentPane().add(p1, BorderLayout.NORTH);
    getContentPane().add(jlblStatus, BorderLayout.SOUTH);

    // Register listener
    jbtView.addActionListener(this);
  }

  /** Handle the "View" button */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtView)
      showFile();
  }

  private void showFile() {
    // Declare buffered stream for reading text for the URL
    BufferedReader infile = null;
    URL url = null;

    try {
      // Obtain URL from the text field
      url = new URL(jtfURL.getText().trim());

      // Create a buffered stream
      InputStream is = url.openStream();
      infile = new BufferedReader(new InputStreamReader(is));

      // Get file name from the text field
      String inLine;

      // Read a line and append the line to the text area
      while ((inLine = infile.readLine()) != null) {
        jtaFile.append(inLine + '\n');
      }

      jlblStatus.setText("File loaded successfully");
    }
    catch (FileNotFoundException e) {
      jlblStatus.setText("URL " + url + " not found.");
    }
    catch (IOException e) {
      jlblStatus.setText(e.getMessage());
    }
    finally {
      try {
        if (infile != null) infile.close();
      }
      catch (IOException ex) {}
    }
  }

  /** Main method */
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("View File From a Web Server");

    // Create an instance of ViewRemoteFile
    ViewRemoteFile applet = new ViewRemoteFile();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}

运行结果如图所示

java从web服务器读取文件_第1张图片

swing提供了一个名为JEditorPane的GUI组件,它能自动的显示普通文本文件,HTML和TRF文件,利用它就不必专门编写从文件中读取数据的代码。他是JTextComponent的一个子类,示例用法如下:

package WebBrowser;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import javax.swing.event.*;
import java.io.*;

public class WebBrowser extends JApplet
    implements ActionListener, HyperlinkListener {
  // JEditor pane to view HTML files
  private JEditorPane jep = new JEditorPane();

  // Label for URL
  private JLabel jlblURL = new JLabel("URL");

  // Text field for entering URL
  private JTextField jtfURL = new JTextField();

  /** Initialize the applet */
  public void init() {
    // Create a panel jpURL to hold the label and text field
    JPanel jpURL = new JPanel();
    jpURL.setLayout(new BorderLayout());
    jpURL.add(jlblURL, BorderLayout.WEST);
    jpURL.add(jtfURL, BorderLayout.CENTER);

    // Create a scroll pane to hold JEditorPane
    JScrollPane jspViewer = new JScrollPane();
    jspViewer.getViewport().add(jep, null);

    // Place jpURL and jspViewer in the applet
    this.getContentPane().add(jspViewer, BorderLayout.CENTER);
    this.getContentPane().add(jpURL, BorderLayout.NORTH);

    // Set jep noneditable
    jep.setEditable(false);

    // Register listener
    jep.addHyperlinkListener(this);
    jtfURL.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e) {
    try {
      // Get the URL from text field
      URL url = new URL(jtfURL.getText().trim());

      // Display the HTML file
      jep.setPage(url);
    }
    catch (IOException ex) {
      System.out.println(ex);
    }
  }

  public void hyperlinkUpdate(HyperlinkEvent e) {
    try {
      jep.setPage(e.getURL());
    }
    catch (IOException ex) {
      System.out.println(ex);
    }
  }

  /** Main method */
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("Web Browser");

    // Create an instance of ViewRemoteFile
    WebBrowser applet = new WebBrowser();

    // Add the applet instance to the frame
    frame.getContentPane().add(applet, BorderLayout.CENTER);

    // Invoke init() and start()
    applet.init();
    applet.start();

    // Display the frame
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}


示例结果如图:

java从web服务器读取文件_第2张图片

你可能感兴趣的:(java,web服务器)