- 11lingxian
- 等级: 初级会员
- 性别:
- 文章: 8
- 积分: 40
- 来自: 青岛
|
java 代码
-
-
-
-
-
-
- public String createHtmlFile(String filePath, String urlPath)
-
- {
- try
- {
- Util.log("urlPath="+urlPath);
-
- URL url = new URL(urlPath);
- URLConnection urlConnection = url.openConnection();
- urlConnection.setAllowUserInteraction(false);
-
- InputStream urlStream = urlConnection.getInputStream();
- byte b[] = new byte[1024];
- int numRead = urlStream.read(b);
- String content = new String(b, 0, numRead);
- StringBuffer tempHtml = new StringBuffer();
- while ( (numRead != -1) && (content.length() < MAXSIZE))
- {
- numRead = urlStream.read(b);
- if (numRead != -1)
- {
- String newContent = new String(b, 0, numRead);
- content += newContent;
- }
- }
- tempHtml = tempHtml.append(content);
- FileOperation.writeFromBuffer(filePath, tempHtml);
- return content;
- }
-
- catch (IOException e)
- {
- e.printStackTrace();
- Util.log("ERROR: couldn't open URL ");
- return "";
- }
- }
为什么不用InputStreamReader去读文件呢,可以用指定编码方式去读取文件,设置读取方式为GBK就可以了
同意,采用如下方法就可以了:
java 代码
-
-
-
-
-
-
- public String createHtmlFile(String filePath, String urlPath)
-
- {
- try
- {
- Util.log("urlPath="+urlPath);
-
- URL url = new URL(urlPath);
-
- URLConnection urlConnection = url.openConnection();
- urlConnection.setAllowUserInteraction(false);
- InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
- BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
-
- String inputLine;
- String content="";
- StringBuffer tempHtml = new StringBuffer();
-
- while ((inputLine = in.readLine()) != null)
-
- {
-
-
- tempHtml.append(inputLine+"/n");
- }
-
-
-
-
-
-
-
- FileOperation.writeFromBuffer(filePath, tempHtml);
- return content;
- }
-
- catch (IOException e)
- {
- e.printStackTrace();
- Util.log("ERROR: couldn't open URL ");
- return "";
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- liquidthinker
- 等级: 初级会员
- 性别:
- 文章: 156
- 积分: 40
- 来自: 成都
|
这段程序太多问题了
|
返回顶楼 |
|
|
- pikachu
- 等级:
- 文章: 473
- 积分: 1488
|
发贴子不动脑子,隐藏!!
|
返回顶楼 |
|
|
- 11lingxian
- 等级: 初级会员
- 性别:
- 文章: 8
- 积分: 40
- 来自: 青岛
|
两位大侠
是我在贴得时候有问题
可
程序没那么多问题吧
我这是转别人的
我用的挺好
当然要改进一下
知道原理就好啦
多谢两位
|
返回顶楼 |
|
|