zoukankan      html  css  js  c++  java
  • 关于Java中Http下载的一些整理

    /**
    	 * 下载小图
    	 * @param img
    	 */
    	public String download(String file){
    		DataInputStream is = null;
    		DataOutputStream os = null;
    		String p = "";
    		HttpURLConnection con = null;
    		try {
    			URL url = new URL(file);
    			con = (HttpURLConnection) url.openConnection();
    			URL path = Thread.currentThread().getContextClassLoader().getResource("");
    			p = path.getPath();
    			String[] strs = url.getFile().split("/");
    			p = p.replace("WEB-INF/classes/", "") + "images/" + strs[strs.length-1];
    			p = p.substring(1);
    			File img11 = new File(p);
    			if(!img11.exists()){
    				img11.createNewFile();
    			}
    			is = new DataInputStream(con.getInputStream());
    			os = new DataOutputStream(new FileOutputStream(p));
    			byte[] buffer = new byte[51200];
    			int count = 0;
    			while(((count = is.read(buffer)) > 0)){
    				os.write(buffer, 0, count);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally{
    			try {
    				is.close();
    				os.close();
    				con.disconnect();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			return p;
    		}
    	}
    
  • 相关阅读:
    丑数(摘)
    queue 之团队队列(摘)
    stack 集合栈计算机 (摘)
    反片语(map)
    stl的集合set——安迪的第一个字典(摘)
    stringstream函数(i o)
    T
    S
    R
    java面试题
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/2112652.html
Copyright © 2011-2022 走看看