package test;
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供豐滿網(wǎng)站建設、豐滿做網(wǎng)站、豐滿網(wǎng)站設計、豐滿網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、豐滿企業(yè)網(wǎng)站模板建站服務,十余年豐滿做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
private String u;
private String encoding;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest("", "UTF-8");
client.run();
}
public HttpTest(String u, String encoding) {
this.u = u;
this.encoding = encoding;
}
public void run() throws Exception {
URL url = new URL(u);// 根據(jù)鏈接(字符串格式),生成一個URL對象
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打開URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網(wǎng)頁的內(nèi)容
String line; // 讀取輸入流的數(shù)據(jù),并顯示
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
根據(jù)具體問題類型,進行步驟拆解/原因原理分析/內(nèi)容拓展等。
具體步驟如下:/導致這種情況的原因主要是……
1.編寫useSourceViewer 類的基本框架,該類僅包括無返回值的main ()方法,該方法從參數(shù)中獲取URL,通過輸入緩沖和輸出緩沖將該URL 原碼輸出。
2.編寫useSourceViewer 類,代碼如下:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length 0)
{
try
{
//讀入URL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 為增加性能存儲輸入流
in = new BufferedInputStream(in);
// 將輸入流連接到閱讀器
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1)
{
System.out.print((char) c);
}
Object o = u.getContent( );
System.out.println("I got a " + o.getClass().getName( ));
}
catch (MalformedURLException e)
{
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException e)
{
System.err.println(e);
}
} // end if
} // end main
} // end SourceViewer}
import?java.awt.BorderLayout;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.io.BufferedReader;
import?java.io.IOException;
import?java.io.InputStream;
import?java.io.InputStreamReader;
import?java.net.HttpURLConnection;
import?java.net.MalformedURLException;
import?java.net.URL;
import?java.util.regex.Matcher;
import?java.util.regex.Pattern;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPanel;
import?javax.swing.JScrollPane;
import?javax.swing.JTextArea;
import?javax.swing.JTextField;
public?class?HttpViewer?extends?JFrame?{
private?JTextField?urlInput;
private?JTextArea?viewArea;
public?static?void?main(String[]?args)?{
new?HttpViewer();
}
public?HttpViewer()?{
this.setTitle("Http?Viewer");
this.setSize(800,?600);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
initPanel();
initAction();
this.setVisible(true);
}
//?這個方法用來設置窗口布局
private?void?initPanel()?{
JPanel?northPanel?=?new?JPanel();
JLabel?urlInputLabel?=?new?JLabel("URL:");
urlInput?=?new?JTextField(60);
northPanel.add(urlInputLabel);
northPanel.add(urlInput);
this.add(northPanel,?BorderLayout.NORTH);
JPanel?centerPanel?=?new?JPanel();
viewArea?=?new?JTextArea(27,?60);
centerPanel.add(new?JScrollPane(viewArea));
this.add(centerPanel);
}
//?這個方法用來設置事件
private?void?initAction()?{
urlInput.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
String?text?=?urlInput.getText();
if?(text?==?null?||?text.length()?==?0)?{
viewArea.setText("您沒有輸入URL");
return;
}
try?{
URL?url?=?new?URL(text);
String?context?=?getContent(url);
if?(context?!=?null)?{
searchFromText(context);
}
}?catch?(MalformedURLException?e1)?{
viewArea.setText("您輸入的URL不合法:"?+?text);
}
}
});
}
private?String?getContent(URL?url)?{
StringBuffer?builder?=?new?StringBuffer();
int?responseCode?=?-1;
HttpURLConnection?con?=?null;
try?{
con?=?(HttpURLConnection)?url.openConnection();
con.setRequestProperty("User-Agent",
"Mozilla/4.0?(compatible;?MSIE?5.0;?Windows?NT;?DigExt)");//?IE代理進行下載
con.setConnectTimeout(60000);
con.setReadTimeout(60000);
//?獲得網(wǎng)頁返回信息碼
responseCode?=?con.getResponseCode();
if?(responseCode?==?-1)?{
viewArea.setText("連接失敗:"?+?url.toString());
return?null;
}
if?(responseCode?=?400)?{
viewArea.setText("請求失敗,錯誤碼:"?+?responseCode);
return?null;
}
InputStream?is?=?con.getInputStream();
InputStreamReader?isr?=?new?InputStreamReader(is);
BufferedReader?br?=?new?BufferedReader(isr);
String?str?=?null;
while?((str?=?br.readLine())?!=?null)
builder.append(str);
is.close();
}?catch?(IOException?e)?{
e.printStackTrace();
viewArea.setText("IOException:?"?+?url.toString());
}?finally?{
con.disconnect();
}
return?builder.toString();
}
private?void?searchFromText(String?context)?{
viewArea.setText("查找URL中:\n");
Pattern?pattern?=?Pattern.compile("a(?[^]+)*(.*?)/a");
Matcher?matcher?=?pattern.matcher(context);
while?(matcher.find())?{
for?(String?prop?:?matcher.group(1).split("?"))?{
int?indexOf?=?prop.indexOf('=');
if?(indexOf??0)?{
if?(prop.substring(0,?indexOf).equals("href"))?{
String?url2?=?prop.substring(indexOf?+?2,?prop.length()?-?1);
viewArea.append(url2?+?"\n");
}
}
}
}
}
}
您好,這樣的:
/**
*
*/
package com.b510.base.bean.install;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author hongten(hongtenzone@foxmail.com)
* @date 2013-2-24
*/
@SuppressWarnings("unchecked")
public class BeanUtils {
//公共部分
private static final String RT_1 = "\r\n";
private static final String RT_2 = RT_1+RT_1;
private static final String BLANK_1 =" ";
private static final String BLANK_4 =" ";
private static final String BLANK_8 =BLANK_4 + BLANK_4;
//注釋部分
private static final String ANNOTATION_AUTHOR_PARAMTER = "@author ";
private static final String ANNOTATION_AUTHOR_NAME = "hongten(hongtenzone@foxmail.com)";
private static final String ANNOTATION_AUTHOR = ANNOTATION_AUTHOR_PARAMTER + ANNOTATION_AUTHOR_NAME;
private static final String ANNOTATION_DATE = "@date ";
private static final String ANNOTATION = "/**"+RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_AUTHOR +RT_1+BLANK_1+"*"+BLANK_1+ANNOTATION_DATE +getDate()+RT_1+BLANK_1+"*/"+RT_1;
//文件 地址
//private static final String BEAN_PATH = "com/b510/base/bean";
private static final String DAO_PATH = "com/b510/base/dao";
private static final String DAO_IMPL_PATH = "com/b510/base/dao/impl";
private static final String SERVICE_PATH = "com/b510/base/service";
private static final String SERVICE_IMPL_PATH = "com/b510/base/service/impl";
//包名
private static final String BEAN_URL = "com.b510.base.bean";
private static final String DAO_URL = "com.b510.base.dao";
private static final String DAO_IMPL_URL = "com.b510.base.dao.impl";
private static final String SERVICE_URL = "com.b510.base.service";
private static final String SERVICE_IMPL_URL = "com.b510.base.service.impl";
//基本類名稱
private static final String BASE_DAO_NAME = DAO_URL + ".BaseDao";
private static final String ABSTRACT_BASE_DAO_IMPL_NAME = DAO_IMPL_URL + ".AbstractBaseDaoImpl";
private static final String BASE_SERVICE_NAME = SERVICE_URL + ".BaseService";
private static final String ABSTRACT_BASE_SERVICE_IMPL_NAME = SERVICE_IMPL_URL + ".AbstractBaseServiceImpl";
/**
* 創(chuàng)建bean的Daobr
*
* @param c
* @throws Exception
*/
public void createBeanDao(Class c) throws Exception {
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + DAO_PATH
+ "/" + getLastChar(cName) + "Dao.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+DAO_URL+";"+RT_2+ANNOTATION+"public interface " +
getLastChar(cName) + "Dao extends "+BASE_DAO_NAME+" " + cName + " {"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 創(chuàng)建bean的Dao的實現(xiàn)類
* @param c
* @throws Exception
*/
public void createBeanDaoImpl(Class c) throws Exception{
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + DAO_IMPL_PATH
+ "/" + getLastChar(cName) + "DaoImpl.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+DAO_IMPL_URL+";"+RT_2+ANNOTATION+"public class " +
getLastChar(cName) + "DaoImpl extends "+ABSTRACT_BASE_DAO_IMPL_NAME+"" +
cName + " implements "+DAO_URL+"."+getLastChar(cName)+"Dao{"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 創(chuàng)建bean的service
* @param c
* @throws Exception
*/
public void createBeanService(Class c) throws Exception{
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_PATH
+ "/" + getLastChar(cName) + "Service.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+SERVICE_URL+";"+RT_2+ANNOTATION+"public interface " +
getLastChar(cName) + "Service extends "+BASE_SERVICE_NAME+""+ cName +"{"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 創(chuàng)建bean的service的實現(xiàn)類
* @param c
* @throws Exception
*/
public void createBeanServiceImpl(Class c) throws Exception{
String cName = c.getName();
String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH
+ "/" +getLastChar(cName)+"ServiceImpl.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
fw.write("package "+SERVICE_IMPL_URL+";"+RT_2+ANNOTATION+"public class "
+ getLastChar(cName) + "ServiceImpl extends "+ABSTRACT_BASE_SERVICE_IMPL_NAME+""+ cName
+ " implements "+SERVICE_URL+"."+getLastChar(cName)+"Service{"+RT_2+BLANK_4
+"private "+DAO_URL+"."+getLastChar(cName)+"Dao "+getLowercaseChar(getLastChar(cName))
+"Dao;"+RT_2+BLANK_4+"public void set"+getLastChar(cName)+"Dao("+DAO_URL+"."+getLastChar(cName)+"Dao "
+getLowercaseChar(getLastChar(cName))+"Dao){"+RT_1+BLANK_8+"this."+getLowercaseChar(getLastChar(cName))+"Dao = "
+getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+BLANK_4+"@Override"+RT_1+BLANK_4
+"public "+DAO_URL+"."+"BaseDao"+BEAN_URL+"."+getLastChar(cName)+" getBaseDao(){"+RT_1+BLANK_8
+"return "+getLowercaseChar(getLastChar(cName))+"Dao;"+RT_1+BLANK_4+"}"+RT_2+"}");
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 獲取路徑的最后面字符串br
* 如:br
* codestr = "com.b510.base.bean.User"/codebr
* code return "User";code
* @param str
* @return
*/
public String getLastChar(String str) {
if ((str != null) (str.length() 0)) {
int dot = str.lastIndexOf('.');
if ((dot -1) (dot (str.length() - 1))) {
return str.substring(dot + 1);
}
}
return str;
}
/**
* 把第一個字母變?yōu)樾慴r
* 如:br
* codestr = "UserDao";/codebr
* codereturn "userDao";/code
* @param str
* @return
*/
public String getLowercaseChar(String str){
return str.substring(0,1).toLowerCase()+str.substring(1);
}
/**
* 顯示信息
* @param info
*/
public void showInfo(String info){
System.out.println("創(chuàng)建文件:"+ info+ "成功!");
}
/**
* 獲取系統(tǒng)時間
* @return
*/
public static String getDate(){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
return simpleDateFormat.format(new Date());
}
}