package org.deyi.sc.yunwei;
import org.deyi.sc.util.Log;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
String url = "http://localhost:8080/syscore/task/rptpub.do?seqno=DDN21010210_NTb01F_NRFGdEXY1&sites=undefined";
writeHtml("D:/sina.html", getHtmlCode(url), "NO");
}
private static long star = 0;
private static long end = 0;
private static long ttime = 0;
// 返回html代码
private static String getHtmlCode(String httpUrl)//,int i, int j)
{
Date before = new Date();
star = before.getTime();
StringBuffer htmlCode = new StringBuffer();
try {
InputStream in;
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in,
"UTF-8"));
String currentLine;
while ((currentLine = breader.readLine()) != null) {
htmlCode.append(currentLine);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Date after = new Date();
end = after.getTime();
ttime = end - star;
System.out.println("执行时间:" + ttime + "毫秒");
}
return htmlCode.toString();
}
// 存储文件
private static synchronized void writeHtml(String filePath, String info, String flag) {
PrintWriter pw = null;
try {
File writeFile = new File(filePath);
boolean isExit = writeFile.exists();
if (isExit != true) {
writeFile.createNewFile();
} else {
if (!flag.equals("NO")) {
writeFile.delete();
writeFile.createNewFile();
}
}
pw = new PrintWriter(new FileOutputStream(filePath, true));
pw.println(info);
pw.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
pw.close();
}
}
}
上面代码有生成后图表不显示问题
之后把输出流改为字节流问题解决
package org.deyi.sc.yunwei;
import org.deyi.sc.util.Log;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
String url = "http://localhost:8080/syscore/task/rptpub.do?seqno=DDN21010210_NTb01F_NRFGdEXY1&sites=undefined";
writeHtml("D:/sina.html", getHtmlCode(url), "NO");
}
private static long star = 0;
private static long end = 0;
private static long ttime = 0;
// 返回html代码
private static String getHtmlCode(String httpUrl) {
Date before = new Date();
star = before.getTime();
URL url;
HttpURLConnection conn;
try {
url = new URL(httpUrl);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200) {
InputStream is = (InputStream) conn.getContent();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int buffer = 1024;
byte[] b = new byte[buffer];
int n = 0;
while ((n = is.read(b, 0, buffer)) > 0) {
baos.write(b, 0, n);
}
String s = baos.toString("UTF-8");
is.close();
baos.flush();
baos.close();
return s;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Date after = new Date();
end = after.getTime();
ttime = end - star;
System.out.println("执行时间:" + ttime + "毫秒");
}
return "";
}
// 存储文件
private static synchronized void writeHtml(String filePath, String info, String flag) {
PrintWriter pw = null;
try {
File writeFile = new File(filePath);
boolean isExit = writeFile.exists();
if (isExit != true) {
writeFile.createNewFile();
} else {
if (!flag.equals("NO")) {
writeFile.delete();
writeFile.createNewFile();
}
}
pw = new PrintWriter(new FileOutputStream(filePath, true));
pw.println(info);
pw.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
pw.close();
}
}
}
问题解析:
通过BufferedReader接收响应丢失响应结果中的换行符\n,举例如下
http预期响应:
0
SECTION
2
HEADER
9
通过BufferedReader解析代码如下
BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf-8" ));
while ((line = in. readLine()) != null) {
result += line + "\n";
}
in.close();
结果:
0SECTION2HEADER9
结果与预期值不符,改用ByteArrayOutputStream解析代码如下
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[ BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0, BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
in.close();
data = null;
return new String(outStream.toByteArray(),encoding); =
响应结果为预期结果
原因分析:
BufferedReader使用readLine()来读取的,意思是一行一行的来读取,读到回车符的时候,就先返回给字符串,然后再进行下一行的读取,导致响应结果中确实换行符。