admin管理员组

文章数量:1516870

图片下载

图片下载

/***  图片类型* @param path    图片地址* @param id      uuid* @param isAspose* @return* @throws IOException*/private String setSignImage(String path, String id, boolean isAspose) throws IOException {String result = "";try {// 图片路径不为空if (StringUtils.isNotEmpty(path) && !path.equals("null")) {BufferedInputStream bin = null;FileOutputStream out = null;String imagepath = null;try {//图片完整地址String urlPath = fastdfs_server + "/" +path;// 统一资源URL url = new URL(urlPath);logger.info("file path is : " + urlPath);// 连接类的父类,抽象类URLConnection urlConnection = url.openConnection();// http的连接类HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;// 设置字符编码httpURLConnection.setRequestProperty("Charset", "UTF-8");// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。httpURLConnection.connect();bin = new BufferedInputStream(httpURLConnection.getInputStream());String fileType = HttpURLConnection.guessContentTypeFromStream(bin);fileType = fileType.split("/")[1];//生成本地图片路径String binPath = System.getProperty("user.dir");logger.info("binPath=" + binPath);if (binPath.endsWith(File.separator)) {binPath = binPath.substring(0, binPath.length() - 1);}SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");//设置日期格式String appPath = binPath.substring(0, binPath.lastIndexOf(File.separator));String wordImagePath = appPath + File.separator + "webapps" + File.separator + "wordImage" + File.separator + df.format(new Date());logger.info("wordImagePath=" + wordImagePath);File toDir = new File(wordImagePath);if (!toDir.exists()) {toDir.mkdirs();}imagepath = wordImagePath + File.separator + id + "." + fileType;logger.info("图片路径{}", imagepath);out = new FileOutputStream(new File(imagepath));int size = 0;byte[] buf = new byte[1024];while ((size = bin.read(buf)) != -1) {out.write(buf, 0, size);}bin.close();out.close();} catch (Exception e) {e.printStackTrace();logger.error(e.getMessage());} finally {if (bin != null) {bin.close();}if (out != null) {out.close();}}File file = new File(imagepath);//linux 下需要加  file://imagepath = "file://" + imagepath;if (!file.exists()) {//文件下载失败logger.info("文件下载失败,或图片不存在={}", imagepath);result = "";} else {if (isAspose) {result = imagepath;} else {result = "[image]" + imagepath + "[/image]";}}} else {//查询姓名logger.info("图片不存在", "");result = "";}logger.info("图片返回值={}", result);} catch (Exception e) {logger.error("get the image is error", e);}return result;}

二维码生成

依赖

 <!-- .google.zxing/core --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version></dependency><!-- .google.zxing/javase --><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version></dependency></dependencies>
/*** 获取图片 , 二维码 条形码** @param* @throws IOException e*/private String getImage(String path, String id, boolean isAspose) throws IOException {String result = "";try {// 图片路径不为空if (StringUtils.isNotEmpty(path) && !path.equals("null")) {BufferedInputStream bin = null;FileOutputStream out = null;String imagepath = null;/* try {String urlPath = fdfsDownServer + path;// 统一资源URL url = new URL(urlPath);logger.info("file path is="+urlPath);// 连接类的父类,抽象类URLConnection urlConnection = url.openConnection();// http的连接类HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;// 设置字符编码httpURLConnection.setRequestProperty("Charset", "UTF-8");// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。httpURLConnection.connect();bin = new BufferedInputStream(httpURLConnection.getInputStream());String fileType = HttpURLConnection.guessContentTypeFromStream(bin);fileType = fileType.split("/")[1];//生成本地图片路径String binPath = System.getProperty("user.dir");logger.info("binPath=" + binPath);if (binPath.endsWith(File.separator)) {binPath = binPath.substring(0, binPath.length() - 1);}SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");//设置日期格式String appPath = binPath.substring(0, binPath.lastIndexOf(File.separator));String wordImagePath = appPath + File.separator + "webapps"+File.separator+"wordImage"+File.separator+df.format(new Date());logger.info("wordImagePath=" + wordImagePath);File toDir = new File(wordImagePath);if (!toDir.exists()) {toDir.mkdirs();}imagepath = wordImagePath + File.separator + id + "." + fileType;logger.info("图片路径{}", imagepath);out = new FileOutputStream(new File(imagepath));int size = 0;byte[] buf = new byte[1024];while ((size = bin.read(buf)) != -1) {out.write(buf, 0, size);}bin.close();out.close();} catch (Exception e) {e.printStackTrace();logger.error(e.getMessage());} finally {if (bin != null) {bin.close();}if (out != null) {out.close();}}*///生成本地图片路径String binPath = System.getProperty("user.dir");logger.info("binPath=" + binPath);if (binPath.endsWith(File.separator)) {binPath = binPath.substring(0, binPath.length() - 1);}SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHH");//设置日期格式String appPath = binPath.substring(0, binPath.lastIndexOf(File.separator));String wordImagePath = appPath + File.separator + "webapps" + File.separator + "wordImage" + File.separator + df.format(new Date());//路径白名单校验,路径特殊符号校验,此处不需要//wordImagePath = FileUrlWhiteListUtil.getInstance().filenameFilter(wordImagePath);logger.info("wordImagePath=" + wordImagePath);File toDir = new File(wordImagePath);if (!toDir.exists()) {toDir.mkdirs();}imagepath = wordImagePath + File.separator + id;logger.info("图片路径{}", imagepath);imagepath = HisignQRCode.createQrcode(path, 80, 80, "jpg", imagepath, 4);logger.info("图片路径{}", imagepath);File file = new File(imagepath);//linux 下需要加  file://imagepath = "file://" + imagepath;if (!file.exists()) {//文件下载失败logger.info("文件下载失败,或图片不存在={}", imagepath);result = "";} else {if (isAspose) {result = imagepath;} else {result = "[image]" + imagepath + "[/image]";}}} else {//查询姓名logger.info("图片不存在", "");result = "";}logger.info("图片返回值={}", result);} catch (Exception e) {logger.error("get the image is error", e);}return result;}

本文标签: 图片下载