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源碼開始,逐步提升自己的編程能力。