1, (8 分 ) 使用程序从网上下载 pdf, 网址为
http://files.saas.hand-china.com/java/target.pdf,保存在本地,
编程时使用带缓冲的读写,将需要保证保存后的 pdf 文件能正常
打开.
解析:只能用字节流来读写,用字符流写入pdf后 文件打不开,程序还与待优化
1 package com.fansi.com; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 import java.net.MalformedURLException; 11 import java.net.URL; 12 import java.net.URLConnection; 13 14 public class Url_File { 15 16 public static void main(String[] args) { 17 18 try { 19 20 21 22 URL url = new URL("http://files.saas.hand-china.com/java/target.pdf"); 23 URLConnection conn = url.openConnection(); 24 InputStream is = conn.getInputStream(); 25 BufferedInputStream bis = new BufferedInputStream(is); 26 27 File file = new File("target.pdf"); 28 FileOutputStream fos = new FileOutputStream(file); 29 BufferedOutputStream bos = new BufferedOutputStream(fos); 30 31 byte[] b = new byte[1024]; 32 while(bis.read(b)!=-1){ 33 bos.write(b); 34 } 35 36 bos.close(); 37 fos.close(); 38 bis.close(); 39 is.close(); 40 41 42 } catch (MalformedURLException e) { 43 44 e.printStackTrace(); 45 } catch (IOException e) { 46 e.printStackTrace(); 47 } 48 49 } 50 51 }
2, (8 分)实现一个 Socket 服务器端和一个 Socket 客户端,先启动服
务器端,当客户端连接上来后,将第一题中得到的 target.pdf 通过
Stream 发送给客户端,客户端接收并保存为 pdf 文件,并保证 pdf
文件能被正常打开.
服务器端代码,Server.java (字节流)
1 package com.fansi; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.IOException; 8 import java.io.OutputStream; 9 import java.net.ServerSocket; 10 import java.net.Socket; 11 12 public class Server { 13 14 public static void main(String[] args) { 15 try { 16 ServerSocket serversocket = new ServerSocket(23456); 17 while(true){ 18 Socket socket = serversocket.accept(); 19 System.out.println("有客户端连上了"); 20 21 File file = new File("down.pdf"); 22 FileInputStream fis = new FileInputStream(file); 23 BufferedInputStream bis = new BufferedInputStream(fis); 24 25 BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); 26 27 byte b[] = new byte[1024]; 28 while(bis.read(b)!=-1){ 29 bos.write(b); 30 31 } 32 bos.close(); 33 bis.close(); 34 fis.close(); 35 } 36 37 38 } catch (IOException e) { 39 40 e.printStackTrace(); 41 } 42 } 43 }
客户端代码,Client.java(字节流)
1 package com.fansi; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.net.Socket; 9 import java.net.UnknownHostException; 10 11 public class Client { 12 13 public static void main(String[] args) { 14 try { 15 16 17 Socket socket = new Socket("127.0.0.1", 23456); 18 BufferedInputStream bis =new BufferedInputStream(socket.getInputStream()); 19 20 File file = new File("new.pdf"); 21 FileOutputStream fos = new FileOutputStream(file); 22 BufferedOutputStream bos = new BufferedOutputStream(fos); 23 24 byte[] b = new byte[1024]; 25 while(bis.read(b)!=-1){ 26 bos.write(b); 27 } 28 bos.flush(); 29 bos.close(); 30 fos.close(); 31 bis.close(); 32 33 34 35 } catch (UnknownHostException e) { 36 e.printStackTrace(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 41 } 42 }
3, (8 分)
Sina 股票数据接口,以大秦铁路(股票代码:601006)为
例,如果要获取它的最新行情,只需访问新浪的股票数据
接口:http://hq.sinajs.cn/list=sh601006 这个 url 会返回
一串文本,例如:
var hq_str_sh601006="大秦铁路, 27.55, 27.25, 26.91,
27.55, 26.20, 26.91, 26.92,
22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700,
26.89, 14300,
26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230,
26.94, 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";
这个字符串由许多数据拼接在一起,不同含义的数据用逗
号隔开了,顺序号从 0 开始。
0:”大秦铁路”,股票名字;name
1:”27.55″,今日开盘价;open
2
2:”27.25″,昨日收盘价;close
3:”26.91″,当前价格;current
4:”27.55″,今日最高价;high
5:”26.20″,今日最低价;low
编程访问 http://hq.sinajs.cn/list=sz300170 ,解释所得到的数
据,并将生成 XML 数据文件和 JSON 文件,并保存到本地,如:
xml 数据:
xxxxx
< open >xxxxx
……
json 数据:
{name:”xxx”,open:xxxx……}
Json_Xml.java (字符流)
1 package com.fansi; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStreamReader; 9 import java.io.OutputStreamWriter; 10 import java.net.MalformedURLException; 11 import java.net.URL; 12 import java.net.URLConnection; 13 14 15 import javax.xml.parsers.DocumentBuilder; 16 import javax.xml.parsers.DocumentBuilderFactory; 17 import javax.xml.parsers.ParserConfigurationException; 18 import javax.xml.transform.Result; 19 import javax.xml.transform.Source; 20 import javax.xml.transform.Transformer; 21 import javax.xml.transform.TransformerConfigurationException; 22 import javax.xml.transform.TransformerException; 23 import javax.xml.transform.TransformerFactory; 24 import javax.xml.transform.dom.DOMSource; 25 import javax.xml.transform.stream.StreamResult; 26 27 import org.w3c.dom.Document; 28 import org.w3c.dom.Element; 29 30 import com.google.gson.JsonObject; 31 32 public class Json_Xml { 33 34 public static void main(String[] args) { 35 36 try { 37 38 URL url = new URL("http://hq.sinajs.cn/list=sz300170"); 39 URLConnection conn = url.openConnection(); 40 InputStreamReader bis = new InputStreamReader(conn.getInputStream()); 41 BufferedReader br = new BufferedReader(bis); 42 43 44 String line; 45 String[] res = new String[100]; 46 while((line=br.readLine())!=null){ 47 System.out.println(line); 48 res = line.split(","); 49 } 50 // for(int i=0;i<res.length;i++){ 51 // System.out.println(res[i]); 52 // } 53 res[0]=res[0].substring(21); 54 //System.out.println(res[0]); 55 56 57 br.close(); 58 bis.close(); 59 60 61 62 //-----------------json创建 63 JsonObject object = new JsonObject(); 64 65 object.addProperty("0", res[0]); 66 object.addProperty("1", res[1]); 67 object.addProperty("2", res[2]); 68 object.addProperty("3", res[3]); 69 object.addProperty("4", res[4]); 70 object.addProperty("5", res[5]); 71 72 //System.out.println(object.toString()); 73 File f1 = new File("json.txt"); 74 FileOutputStream fis1 = new FileOutputStream(f1); 75 OutputStreamWriter osw1 = new OutputStreamWriter(fis1, "utf-8"); 76 BufferedWriter bw1 = new BufferedWriter(osw1); 77 78 bw1.write(object.toString()); 79 80 bw1.close(); 81 osw1.close(); 82 fis1.close(); 83 84 85 86 //---------------------------xml的写 87 88 89 try { 90 91 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 92 DocumentBuilder builder = factory.newDocumentBuilder(); 93 Document document = builder.newDocument(); 94 Element root = document.createElement("hand"); 95 96 97 Element r1 = document.createElement("name"); 98 r1.setTextContent(res[0]); 99 root.appendChild(r1); 100 101 Element r2 = document.createElement("open"); 102 r2.setTextContent(res[1]); 103 root.appendChild(r2); 104 105 Element r3 = document.createElement("current"); 106 r3.setTextContent(res[2]); 107 root.appendChild(r3); 108 109 Element r4 = document.createElement("high"); 110 r4.setTextContent(res[3]); 111 root.appendChild(r4); 112 113 Element r5 = document.createElement("low"); 114 r5.setTextContent(res[4]); 115 root.appendChild(r5); 116 117 document.appendChild(root); 118 119 TransformerFactory transformerFactory = TransformerFactory.newInstance(); 120 Transformer transformer = transformerFactory.newTransformer(); 121 122 transformer.transform(new DOMSource(document), new StreamResult(new File("xml.txt") )); 123 124 } catch (ParserConfigurationException e) { 125 126 e.printStackTrace(); 127 } catch (TransformerConfigurationException e) { 128 e.printStackTrace(); 129 } catch (TransformerException e) { 130 // TODO Auto-generated catch block 131 e.printStackTrace(); 132 } 133 134 135 136 } catch (MalformedURLException e) { 137 e.printStackTrace(); 138 } catch (IOException e) { 139 e.printStackTrace(); 140 } 141 142 } 143 144 }