JAVA工具类

发送get请求并返回JSONObject对象

public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
        JSONObject jsonObject =null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet =new HttpGet(url);
        SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
        HttpResponse response = client.execute(httpGet);
        HttpEntity entity =response.getEntity();
        if(entity!=null){
            //把返回的结果转换为JSON对象
            String result =EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSON.parseObject(result);
        }

        return jsonObject;
    }

发送POST请求并返回string对象

public static String sendPost(String param, String url) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            // out = new PrintWriter(conn.getOutputStream());
            out = new PrintWriter(new OutputStreamWriter(
                    conn.getOutputStream(), "utf-8"));
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

将xml转化为Map集合

public static Map<String, String> xmlToMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        SAXReader reader = new SAXReader();
        InputStream ins = null;
        try {
            ins = request.getInputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Document doc = null;
        try {
            doc = reader.read(ins);
        } catch (DocumentException e1) {
            e1.printStackTrace();
        }
        Element root = doc.getRootElement();
        @SuppressWarnings("unchecked")
        List<Element> list = root.elements();
        for (Element e : list) {
            map.put(e.getName(), e.getText());
        }
        try {
            ins.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return map;
    }

根据url下载文件

//根据url下载文件,参数(文件网址,存文件的本地地址)
    public static Boolean downloadFile(String urlString, String filePath){
        // 构造URL
        URL url;
        try {
            url = new URL(urlString);
            // 打开连接
            URLConnection con;
            try {
                con = url.openConnection();
                // 输入流
                InputStream is = con.getInputStream();
                // 1K的数据缓冲
                byte[] bs = new byte[1024];
                // 读取到的数据长度
                int len;
                // 输出的文件流
                OutputStream os = new FileOutputStream(filePath);
                // 开始读取
                while ((len = is.read(bs)) != -1) {
                    os.write(bs, 0, len);
                }
                // 完毕,关闭所有链接
                os.close();
                is.close();
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

    }

基于JAVA的图片合成(合成二维码海报)

/**
    * @Description: 图片合成
    * @Param: [image1Path, image2Path, imgX, imgY, imgWidth, imgHeight] [背景图片绝对路径,素材图片路径,素材图片X轴坐标,素材图片Y轴坐标,素材图片宽度,素材图片高度]
    * @return: java.lang.String
    * @Author: Chenjf
    * @Date: 2018/10/8
    */
    public String ImgSynthesis(String image1Path,String image2Path,int imgX,int imgY,int imgWidth,int imgHeight) throws IOException {
        InputStream imagein = new FileInputStream(image1Path);
        InputStream imagein2 = new FileInputStream(image2Path);

        BufferedImage image = ImageIO.read(imagein);
        BufferedImage image2 = ImageIO.read(imagein2);
        Graphics g = image.getGraphics();
        g.drawImage(image2, imgX, imgY,imgWidth, imgHeight, null);
//        g.drawImage(image2, 1900, 2600,
//                image2.getWidth() + 10, image2.getHeight() + 5, null);

        //输出图片覆盖生成的二维码文件
//        OutputStream outImage = new FileOutputStream(image2Path);
        String formatName = image2Path.substring(image2Path.lastIndexOf(".") + 1);
        ImageIO.write(image, /*"GIF"*/ formatName /* format desired */ , new File(image2Path) /* target */ );

//        JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(outImage);
//        enc.encode(image);
        imagein.close();
        imagein2.close();
//        outImage.close();

        return  "";
    }

读取本地文本文件
*注:若文件编码不是UTF-8,读出来的中文会乱码。需要转码。也可以将文本文件编码改为UTF-8。我是通过notepad++直接将文本转成utf-8的,简单省事。

public String readTxt(File file) {
        StringBuffer result = new StringBuffer();
//        File file = new File("D:/course.txt");
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
				result.append(System.lineSeparator()+s);
            }
            System.out.println(result);
            br.close();

        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值