一、前言
在工作中,难免遇到各种各样的问题,每个人似乎都有一套自己的解决方案。而我,又不想每次解决完问题就把东西扔了,捡了芝麻,丢了西瓜,什么时候才能进步勒?学习要靠积累,毕竟量变才能引起质变嘛。所以写了这篇博文,不定时更新自己项目中遇到的问题、踩过的那些坑......
二、项目
1、Java 将两张图片合成一张图片

/** * 图片合并 */ public String joinImage(String url1, String url2) { try { InputStream is1 = getImgConn(url1); InputStream is2 = getImgConn(url2); BufferedImage image1 = ImageIO.read(is1); BufferedImage image2 = ImageIO.read(is2); BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, image1.getWidth(), 0, null); String imgURL=System.currentTimeMillis()+".jpg"; ImageIO.write(combined, "JPG", new File("/home/wwwroot/picFiles",imgURL)); return "/home/wwwroot/picFiles/"+imgURL; } catch (IOException e) { e.printStackTrace(); } return null; } //读文件 private InputStream getImgConn(String url){ try { URL url1 = new URL(url); URLConnection urlConnection = url1.openConnection(); InputStream is1 = urlConnection.getInputStream(); //先读入内存 ByteArrayOutputStream buf = new ByteArrayOutputStream(8192); byte[] b = new byte[1024]; int len; while ((len = is1.read(b)) != -1) { buf.write(b, 0, len); } is1=new ByteArrayInputStream(buf.toByteArray()); return is1; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }

/** * 图片合并 */ public static String joinImage(File file1, File file2) { BufferedImage image1 = null; BufferedImage image2 = null; try { image1 = ImageIO.read(file1); image2 = ImageIO.read(file2); BufferedImage combined = new BufferedImage(image1.getWidth() * 2, image1.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, image1.getWidth(), 0, null); String imgURL=System.currentTimeMillis()+".jpg"; ImageIO.write(combined, "JPG", new File("/home/wwwroot/picFiles",imgURL)); return "/home/wwwroot/picFiles/"+imgURL; } catch (IOException e) { e.printStackTrace(); } return null; }
2、通过经纬度获得高德地图返回的具体地址

/** * 获取经纬度的确切位置 * @param lat 经度 * @param lon 维度 * @param type type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项) * @return */ private String getLocByLonAndLat(String lat,String lon,String type){ String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+lon+"&type="+type; String res = ""; BufferedReader in=null; try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { res += line+"\n"; } JSONObject jsonObject = JSONObject.fromObject(res); JSONArray addrList =(JSONArray)jsonObject.get("addrList"); JSONObject o =(JSONObject)addrList.get(0); String s = o.get("admName").toString() + o.get("addr").toString(); return StringUtils.remove(s,","); } catch (Exception e) { System.out.println("error in wapaction,and e is " + e.getMessage()); }finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }