打包JAR 调用JAR 抓取网页乱码, 帮朋友写的一段小程序

打包JAR 调用JAR 抓取网页乱码, 帮朋友写的一段小程序
JAVA代码:
import  java.io.BufferedReader;
import  java.io.File;
import  java.io.FileWriter;
import  java.io.InputStreamReader;
import  java.net.URL;
import  java.text.SimpleDateFormat;
import  java.util.Calendar;

public   class  W
{
    
public static void main(String[] args)
    
{
        
/**//*
         * String[] paras = new String[]
         * {"
http://m.weather.com.cn/data/101020100.html", "g:/abc.txt" };
         
*/

        
new W().working(args);
    }

    
    
/** *//**
     * <ul>
     * <li>Description:[总调用程序]</li>
     * <li>Created by [Huyvanpull] [2013-3-14]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * </ul>
     * 
     * 
@param paras
     
*/

    
public void working(String[] paras)
    
{
        StringBuffer result 
= new StringBuffer();
        
if (paras != null && paras.length == 2)
        
{
            result.append(paras[
0]).append(",");
            result.append(getDateStr()).append(
",");
            
            String tempStr 
= "";
            
try
            
{
                tempStr 
= this.sendUrl(paras[0]);
            }

            
catch (Exception ex)
            
{
                ex.printStackTrace();
            }

            result.append(tempStr);
        }

        
else
        
{
            result.append(
"参数不正确");
        }

        result.append(System.getProperty(
"line.separator"));
        System.out.println(result);
        writeFile(paras[
1], result.toString());
    }

    
    
/** *//**
     * <ul>
     * <li>Description:[根据网址得到内容]</li>
     * <li>Created by [Huyvanpull] [2013-3-14]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * </ul>
     * 
     * 
@param urlStr
     * 
@return
     
*/

    
public String sendUrl(String urlStr)
    
{
        BufferedReader in 
= null;
        StringBuffer temp 
= new StringBuffer();
        
try
        
{
            URL yahoo 
= new URL(urlStr);
            in 
= new BufferedReader(new InputStreamReader(yahoo.openStream(),
                    
"UTF-8"));
            String inputLine;
            
while ((inputLine = in.readLine()) != null)
                temp.append(inputLine);
        }

        
catch (Exception ex)
        
{
            ex.printStackTrace();
        }

        
finally
        
{
            
if (in != null)
            
{
                
try
                
{
                    in.close();
                }

                
catch (Exception ex)
                
{
                    ex.printStackTrace();
                }

            }

        }

        
return temp.toString();
    }

    
    
/** *//**
     * <ul>
     * <li>Description:[写字符串到文件]</li>
     * <li>Created by [Huyvanpull] [2013-3-14]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * </ul>
     * 
     * 
@param filePath
     * 
@param content
     
*/

    
public static void writeFile(String filePath, String content)
    
{
        FileWriter fw 
= null;
        
try
        
{
            File file 
= new File(filePath);
            fw 
= new FileWriter(file, true);
            fw.write(content);
        }

        
catch (Exception ex)
        
{
            ex.printStackTrace();
        }

        
finally
        
{
            
try
            
{
                
if (fw != null)
                
{
                    fw.close();
                }

            }

            
catch (Exception ex)
            
{
                ex.printStackTrace();
            }

        }

    }

    
    
public String getDateStr()
    
{
        SimpleDateFormat simpleDateFormate 
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
return simpleDateFormate.format(Calendar.getInstance().getTime());
    }

    
}



调用代码:
java  - jar f: / w.jar http: // m.weather.com.cn/data/101020100.html f:/kkk.txt

你可能感兴趣的:(打包JAR 调用JAR 抓取网页乱码, 帮朋友写的一段小程序)