<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tinguo002

     

    java下載網頁內容和網絡圖片

    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class UrlResource {

        
    public static void main(String[] args){
            
    try {
                System.out.println(UrlResource.getUrlDetail(
    "http://www.baidu.com",true));
                saveUrlFile(
    "http://www.baidu.com/img/baidu_jgylogo3.gif""D:\\1.gif");
            }
     catch (Exception e) {
                e.printStackTrace();
            }

        }

        
        
    //獲取網絡文件,轉存到fileDes中,fileDes需要帶文件后綴名
        public static void saveUrlFile(String fileUrl,String fileDes) throws Exception
        
    {
            File toFile 
    = new File(fileDes);
            
    if (toFile.exists())
            
    {
    //            throw new Exception("file exist");
                return;
            }

            toFile.createNewFile();
            FileOutputStream outImgStream 
    = new FileOutputStream(toFile);
            outImgStream.write(getUrlFileData(fileUrl));
            outImgStream.close();
        }

        
        
    //獲取鏈接地址文件的byte數據
        public static byte[] getUrlFileData(String fileUrl) throws Exception
        
    {
            URL url 
    = new URL(fileUrl);
            HttpURLConnection httpConn 
    = (HttpURLConnection) url.openConnection();
            httpConn.connect();
            InputStream cin 
    = httpConn.getInputStream();
            ByteArrayOutputStream outStream 
    = new ByteArrayOutputStream();
            
    byte[] buffer = new byte[1024];
            
    int len = 0;
            
    while ((len = cin.read(buffer)) != -1{
                outStream.write(buffer, 
    0, len);
            }

            cin.close();
            
    byte[] fileData = outStream.toByteArray();
            outStream.close();
            
    return fileData;
        }

        
        
    //獲取鏈接地址的字符數據,wichSep是否換行標記
        public static String getUrlDetail(String urlStr,boolean withSep) throws Exception
        
    {
            URL url 
    = new URL(urlStr);
            HttpURLConnection httpConn 
    = (HttpURLConnection)url.openConnection();
            httpConn.connect();
            InputStream cin 
    = httpConn.getInputStream();
            BufferedReader reader 
    = new BufferedReader(new InputStreamReader(cin,"UTF-8"));
            StringBuffer sb 
    = new StringBuffer();
            String rl 
    = null;
            
    while((rl = reader.readLine()) != null)
            
    {
                
    if (withSep)
                
    {
                    sb.append(rl).append(System.getProperty(
    "line.separator"));
                }

                
    else
                
    {
                    sb.append(rl);
                }

            }

            
    return sb.toString();
        }

        
    }

    // 禁止圖像緩存
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);


    文章詳細參考:http://blog.csdn.net/pandakong/article/details/7430844


    歡迎大家訪問我的個人網站 萌萌的IT人

    posted on 2014-08-04 18:38 一堣而安 閱讀(1506) 評論(0)  編輯  收藏 所屬分類: java

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久久青草青青国产亚洲免观| 成人免费乱码大片A毛片| 久久亚洲精品中文字幕| 亚洲人AV永久一区二区三区久久| 国产精品久久久久免费a∨| 国产色无码精品视频免费| 国产AV无码专区亚洲AV琪琪| 亚洲制服丝袜第一页| 久久亚洲AV成人无码软件| 亚洲成色WWW久久网站| 亚洲区日韩区无码区| 可以免费观看一级毛片黄a| 免费无码A片一区二三区| 鲁大师在线影院免费观看 | 无码少妇一区二区浪潮免费| 久久精品成人免费观看| 丝瓜app免费下载网址进入ios| 一区二区三区AV高清免费波多| 和老外3p爽粗大免费视频 | 日韩视频在线观看免费| 国产99视频精品免费视频76| 豆国产96在线|亚洲| 婷婷亚洲综合五月天小说在线| 亚洲人成无码网站在线观看| 精品亚洲成在人线AV无码| 亚洲免费在线视频播放| 亚洲一区二区三区四区视频| 亚洲人成片在线观看| 亚洲乱码无限2021芒果| 亚洲三级在线播放| 国产成人精品日本亚洲专一区| 亚洲一区二区三区久久| 亚洲性无码一区二区三区| 亚洲国产精品无码观看久久| 亚洲av日韩精品久久久久久a| 亚洲AV无码男人的天堂| 成人久久久观看免费毛片| 2022国内精品免费福利视频| 两个人看的www高清免费观看| 久久免费视频观看| 最近免费2019中文字幕大全|