java公司网站源码
‘壹’ 急求一段简单的java源代码(用户名、密码操作界面)
下面的程序可以直接通过编译运行,自己寻找要用到的代码段。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class UserLogin extends JPanel implements ActionListener{
JTextField userjt=null;//用户输入框
JPasswordField pwdjt=null;
JTextField sysUserjt=null;//系统显示用户名输入框
JTextField sysPwdjt=null;
public UserLogin(){
super(new GridLayout(1,2));
JPanel userPanel=new JPanel();//用户界面,左边
userPanel.setLayout(new BoxLayout(userPanel,BoxLayout.Y_AXIS));
this.add(userPanel);
JPanel userUpPanel=new JPanel();//用户界面上半部分
userPanel.add(userUpPanel);
JPanel userDownPanel=new JPanel();//用户界面下半部分
userPanel.add(userDownPanel);
JPanel sysPanel=new JPanel();//系统界面,右边
sysPanel.setLayout(new BoxLayout(sysPanel,BoxLayout.Y_AXIS));
this.add(sysPanel);
JPanel sysUserPanel=new JPanel();//系统界面上半部分
sysPanel.add(sysUserPanel);
JPanel sysPwdPanel=new JPanel();//系统界面下半部分
sysPanel.add(sysPwdPanel);
userjt=new JTextField(5);
userjt.setText("用户名");
userUpPanel.add(userjt);
pwdjt=new JPasswordField(5);
pwdjt.setText("密码");
pwdjt.setEchoChar('\0');
userDownPanel.add(pwdjt);
JLabel sysUserjl=new JLabel("用户名为:");
sysUserPanel.add(sysUserjl);
sysUserjt=new JTextField(5);
sysUserPanel.add(sysUserjt);
JLabel sysPwdjl=new JLabel("密码为:");
sysPwdPanel.add(sysPwdjl);
sysPwdjt=new JTextField(5);
sysPwdPanel.add(sysPwdjt);
userjt.addActionListener(this);
pwdjt.addActionListener(this);
userjt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
if(userjt.getText().equals("用户名"))
userjt.setText("");
}
public void focusLost(FocusEvent e) {
if(userjt.getText().equals(""))
userjt.setText("用户名");
}});
pwdjt.addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
if(new String(pwdjt.getPassword()).equals("密码")){
pwdjt.setText("");
pwdjt.setEchoChar('*');
}
}
public void focusLost(FocusEvent e) {
if(new String(pwdjt.getPassword()).equals("")){
pwdjt.setText("密码");
pwdjt.setEchoChar('\0');
}
}});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(userjt)){
pwdjt.requestFocus();
}else{
if(new String(pwdjt.getPassword()).equals("")||userjt.getText().equals("")||userjt.getText().equals("用户名")) return;
sysUserjt.setText(userjt.getText());
sysPwdjt.setText(new String(pwdjt.getPassword()));
try {
writetoFile();
} catch (IOException e1) {
System.out.println("写入文件发生异常!");
e1.printStackTrace();
}
}
}
private void writetoFile() throws IOException{
File f=new File("User_Psd.txt");
// if(!f.exists()) f.createNewFile();
RandomAccessFile accessFile=new RandomAccessFile(f, "rw");
accessFile.seek(accessFile.length());
accessFile.write(("user:"+userjt.getText()+"\r\npassword:"+new String(pwdjt.getPassword())+"\r\n\r\n").getBytes());
}
public static void main(String args[]){
JFrame jf=new JFrame("用户登陆模块测试");
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.add(new UserLogin());
jf.setBounds(400,300,280,150);
jf.setVisible(true);
}
}
‘贰’ java 获取网页源代码---有效防止乱码
前段时间做过这类功能,如何有效防止乱码,我们必须先知道一个网页的编码方式,是utf-8,还是gbk。
1.HttpURLConnection.getContentType();直接读取,效率高,但有很多时候读不到。只是text/html就完事了,没有charset.
2.使用第三方的HttpClient,执行效率较高。但读取网页头header也只适用部分站,很多网站服务段不设置,结果就读成了null.
3.最没有效率的判断方法就是使用inputStreamReader先把正页的html源码读取出来,之后截取charset后面编码。得到编码之后重新再读取一遍。但是效率很低。
做个总结:
/**
* 取得页面编码
*
* @param url
* @return
*/
public String getCharset(String url) throws Exception {
// log.info("进入读页面的关键词:" + keyword);
String charset = "";
int c;
HttpURLConnection httpurlcon = null;
// log.info("url:"+url);
// log.info("charset:"+charset);
log.info("url:" + url);
URL httpurl = new URL(url);
// System.out.println(url+str);
httpurlcon = (HttpURLConnection) httpurl.openConnection();
// google需要身份
httpurlcon.setRequestProperty("User-agent", "Mozilla/4.0");
charset = httpurlcon.getContentType();
log.info("charset1:" + charset);
// 如果可以找到
if (charset.indexOf("charset=") != -1)
charset = charset.substring(charset.indexOf("charset=")
+ "charset=".length(), charset.length());
// 否则读取response.Header头
else {
charset = this.getContentCharset();
log.info("charset2:" + charset);
}
// 如果charset还是为空,那么直接读网页来截取
if (charset == null) {
charset = this.readPageCharset(url);
log.info("charset31:" + charset);
}
return charset;
}
‘叁’ 速求用JAVA语言写聊天室的源代码
【ClientSocketDemo.java 客户端Java源代码】
import java.net.*;
import java.io.*;
public class ClientSocketDemo
{
//声明客户端Socket对象socket
Socket socket = null;
//声明客户器端数据输入输出流
DataInputStream in;
DataOutputStream out;
//声明字符串数组对象response,用于存储从服务器接收到的信息
String response[];
//执行过程中,没有参数时的构造方法,本地服务器在本地,取默认端口10745
public ClientSocketDemo()
{
try
{
//创建客户端socket,服务器地址取本地,端口号为10745
socket = new Socket("localhost",10745);
//创建客户端数据输入输出流,用于对服务器端发送或接收数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//获取客户端地址及端口号
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
//向服务器发送数据
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
//从服务器接收数据
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//执行过程中,有一个参数时的构造方法,参数指定服务器地址,取默认端口10745
public ClientSocketDemo(String hostname)
{
try
{
//创建客户端socket,hostname参数指定服务器地址,端口号为10745
socket = new Socket(hostname,10745);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
//执行过程中,有两个个参数时的构造方法,第一个参数hostname指定服务器地址
//第一个参数serverPort指定服务器端口号
public ClientSocketDemo(String hostname,String serverPort)
{
try
{
socket = new Socket(hostname,Integer.parseInt(serverPort));
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
String ip = String.valueOf(socket.getLocalAddress());
String port = String.valueOf(socket.getLocalPort());
out.writeUTF("Hello Server.This connection is from client.");
out.writeUTF(ip);
out.writeUTF(port);
response = new String[3];
for (int i = 0; i < response.length; i++)
{
response[i] = in.readUTF();
System.out.println(response[i]);
}
}
catch(UnknownHostException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
String comd[] = args;
if(comd.length == 0)
{
System.out.println("Use localhost(127.0.0.1) and default port");
ClientSocketDemo demo = new ClientSocketDemo();
}
else if(comd.length == 1)
{
System.out.println("Use default port");
ClientSocketDemo demo = new ClientSocketDemo(args[0]);
}
else if(comd.length == 2)
{
System.out.println("Hostname and port are named by user");
ClientSocketDemo demo = new ClientSocketDemo(args[0],args[1]);
}
else System.out.println("ERROR");
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
【ServerSocketDemo.java 服务器端Java源代码】
import java.net.*;
import java.io.*;
public class ServerSocketDemo
{
//声明ServerSocket类对象
ServerSocket serverSocket;
//声明并初始化服务器端监听端口号常量
public static final int PORT = 10745;
//声明服务器端数据输入输出流
DataInputStream in;
DataOutputStream out;
//声明InetAddress类对象ip,用于获取服务器地址及端口号等信息
InetAddress ip = null;
//声明字符串数组对象request,用于存储从客户端发送来的信息
String request[];
public ServerSocketDemo()
{
request = new String[3]; //初始化字符串数组
try
{
//获取本地服务器地址信息
ip = InetAddress.getLocalHost();
//以PORT为服务端口号,创建serverSocket对象以监听该端口上的连接
serverSocket = new ServerSocket(PORT);
//创建Socket类的对象socket,用于保存连接到服务器的客户端socket对象
Socket socket = serverSocket.accept();
System.out.println("This is server:"+String.valueOf(ip)+PORT);
//创建服务器端数据输入输出流,用于对客户端接收或发送数据
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
//接收客户端发送来的数据信息,并显示
request[0] = in.readUTF();
request[1] = in.readUTF();
request[2] = in.readUTF();
System.out.println("Received messages form client is:");
System.out.println(request[0]);
System.out.println(request[1]);
System.out.println(request[2]);
//向客户端发送数据
out.writeUTF("Hello client!");
out.writeUTF("Your ip is:"+request[1]);
out.writeUTF("Your port is:"+request[2]);
}
catch(IOException e){e.printStackTrace();}
}
public static void main(String[] args)
{
ServerSocketDemo demo = new ServerSocketDemo();
}
}
‘肆’ java源码是指什么
Java源码是指程序员在编写程序时所使用的原始代码。软件运行前,程序员需要编写这些代码,就像音乐家用五线谱创作音乐,建筑师用图纸设计建筑一样,程序员则使用源码进行编程。Java源码可以直接用普通记事本打开查看或编辑,这对于初学者而言,是一个很好的开始,因为它易于理解和操作。
源码是软件开发的基础,它包含了程序的逻辑结构和功能实现。对于初学者来说,学习如何编写Java源码是掌握编程技能的重要一步。使用记事本编辑Java源码,不仅简单直观,而且有助于培养对代码细节的关注。
编写Java源码的过程需要程序员具备良好的逻辑思维能力和解决问题的能力。源码的质量直接影响到程序的运行效率和稳定性。因此,编写清晰、简洁、易于维护的源码是非常重要的。
对于Java程序员而言,熟悉和掌握Java源码的编写技巧,不仅能提高编程效率,还能更好地理解和维护现有的Java代码库。初学者在学习过程中,应注重源码的阅读和编写,通过实践来提高自己的编程能力。
使用记事本编辑Java源码时,应注意代码格式的规范性和一致性。良好的代码格式可以使代码更易于阅读和理解,从而降低维护成本。同时,合理的注释也是编写高质量源码的重要组成部分,它有助于其他开发者快速了解代码的功能和逻辑。
总之,Java源码是程序员进行软件开发的基础,掌握编写高质量的Java源码对于提升编程技能至关重要。初学者应从学习使用记事本编写Java源码开始,逐步提升自己的编程能力。