Java Code Examples for sun.misc.BASE64Decoder#decodeBuffer()

The following examples show how to use sun.misc.BASE64Decoder#decodeBuffer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageUtils.java    From wechat-pay-sdk with MIT License 6 votes vote down vote up
/**
 * <pre>
 * Base64编码的字符解码为图片
 *
 * </pre>
 *
 * @param imageString
 * @return
 */
public static BufferedImage decodeToImage(String imageString) {

    BufferedImage image = null;
    byte[] imageByte;
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        imageByte = decoder.decodeBuffer(imageString);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return image;
}
 
Example 2
Source File: Uploader.java    From jeewx-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 接受并保存以base64格式上传的文件
 * @param fieldName
 */
public void uploadBase64(String fieldName){
	String savePath = this.getFolder(this.savePath);
	String base64Data = this.request.getParameter(fieldName);
	this.fileName = this.getName("test.png");
	this.url = savePath + "/" + this.fileName;
	BASE64Decoder decoder = new BASE64Decoder();
	try {
		File outFile = new File(this.getPhysicalPath(this.url));
		OutputStream ro = new FileOutputStream(outFile);
		byte[] b = decoder.decodeBuffer(base64Data);
		for (int i = 0; i < b.length; ++i) {
			if (b[i] < 0) {
				b[i] += 256;
			}
		}
		ro.write(b);
		ro.flush();
		ro.close();
		this.state=this.errorInfo.get("SUCCESS");
	} catch (Exception e) {
		this.state = this.errorInfo.get("IO");
	}
}
 
Example 3
Source File: ImageUtils.java    From classchecks with Apache License 2.0 6 votes vote down vote up
/**
 * 对字节数组字符串进行Base64解码并生成图片
 * @param base64	 图像的Base64编码字符串
 * @param fileSavePath	图像要保存的地址
 * @return
 */
public static boolean convertBase64ToImage(String base64, String fileSavePath) {
	if(base64 == null) return false;
	
	BASE64Decoder decoder = new BASE64Decoder();
	
	// Base64解码
	try {
		byte[] buffer = decoder.decodeBuffer(base64);
		for(int i = 0; i < buffer.length; i ++) {
			if(buffer[i] < 0) {
				buffer[i] += 256;
			}
		}
		// 生成JPEG图片
		OutputStream out = new FileOutputStream(fileSavePath);
		out.write(buffer);
		out.flush();
		out.close();
		return true;
	} catch (IOException e) {
		e.printStackTrace();
	}
	return false;
}
 
Example 4
Source File: DESUtil.java    From Project with Apache License 2.0 6 votes vote down vote up
/**
 * 获取解密之后的信息
 * 
 * @param str 待解密字符串
 * @return
 */
public static String getDecryptString(String str) {
	// 基于BASE64编码,接收byte[]并转换为String
	BASE64Decoder base64decoder = new BASE64Decoder();
	try {
		// 将字符串decode成byte[]
		byte[] bytes = base64decoder.decodeBuffer(str);
		// 获取解密对象
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		// 初始化解密信息
		cipher.init(Cipher.DECRYPT_MODE, key);
		// 解密
		byte[] doFinal = cipher.doFinal(bytes);
		// 返回解密之后的信息
		return new String(doFinal, CHARSETNAME);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 5
Source File: LocalUploadServiceImpl.java    From mysiteforme with Apache License 2.0 5 votes vote down vote up
@Override
public String uploadBase64(String base64) {
    StringBuffer webUrl=new StringBuffer("/static/upload/");
    BASE64Decoder decoder = new BASE64Decoder();
    try
    {
        //Base64解码
        byte[] b = decoder.decodeBuffer(base64);
        for(int i=0;i<b.length;++i)
        {
            if(b[i]<0)
            {//调整异常数据
                b[i]+=256;
            }
        }
        //生成jpeg图片
        StringBuffer ss = new StringBuffer(ResourceUtils.getURL("classpath:").getPath());
        String filePath = ss.append("static/upload/").toString();
        File targetFileDir = new File(filePath);
        if(!targetFileDir.exists()){
            targetFileDir.mkdirs();
        }
        StringBuffer sb = new StringBuffer(filePath);
        StringBuffer fileName = new StringBuffer(RandomUtil.randomUUID());
        sb.append(fileName);
        sb.append(".jpg");
        String imgFilePath = sb.toString();//新生成的图片
        OutputStream out = new FileOutputStream(imgFilePath);
        out.write(b);
        out.flush();
        out.close();
        return webUrl.append(sb).toString();
    }
    catch (Exception e)
    {
        return null;
    }
}
 
Example 6
Source File: SecureUtil.java    From DataM with Apache License 2.0 5 votes vote down vote up
/**
 * 使用 默认key 解密
 */
public static String decrypt(String data) throws IOException, Exception {
    if (data == null)
        return null;
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] buf = decoder.decodeBuffer(data);
    byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));
    return new String(bt, ENCODE);
}
 
Example 7
Source File: FtpClient.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getSecurityData() {
    String s = getLastResponseString();
    if (s.substring(4, 9).equalsIgnoreCase("ADAT=")) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Need to get rid of the leading '315 ADAT='
            // and the trailing newline
            return decoder.decodeBuffer(s.substring(9, s.length() - 1));
        } catch (IOException e) {
            //
        }
    }
    return null;
}
 
Example 8
Source File: Encrypt.java    From watchdog-framework with MIT License 5 votes vote down vote up
/**
 * Base64解密
 * */
public static String base64Decode(String s) {
    byte[] b = null;
    String result = null;
    if (s != null) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            b = decoder.decodeBuffer(s);
            result = new String(b, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}
 
Example 9
Source File: EngineStartServletIOManager.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] getTemplate(boolean forEdit) {
	byte[] templateContent = null;

	if (template == null) {
		contentProxy = getContentServiceProxy();
		HashMap requestParameters = ParametersDecoder.getDecodedRequestParameters(getRequestContainer());
		if (forEdit) {
			requestParameters.put(ON_EDIT_MODE, ON_EDIT_MODE);
		}
		template = contentProxy.readTemplate(getDocumentId(), requestParameters);
	}

	if (template != null) {
		templateName = template.getFileName();
		logger.debug("Read the template [" + template.getFileName() + "]");

		try {
			// BASE64Decoder cannot be used in a static way, since it is not thread-safe;
			// see https://spagobi.eng.it/jira/browse/SPAGOBI-881
			BASE64Decoder decoder = new BASE64Decoder();
			templateContent = decoder.decodeBuffer(template.getContent());
		} catch (IOException e) {
			throw new SpagoBIRuntimeException("Impossible to get content from template [" + template.getFileName() + "]", e);
		}
	} else {
		logger.warn("Document template is not defined or it is impossible to get it from the server");
	}

	return templateContent;
}
 
Example 10
Source File: FtpClient.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getSecurityData() {
    String s = getLastResponseString();
    if (s.substring(4, 9).equalsIgnoreCase("ADAT=")) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Need to get rid of the leading '315 ADAT='
            // and the trailing newline
            return decoder.decodeBuffer(s.substring(9, s.length() - 1));
        } catch (IOException e) {
            //
        }
    }
    return null;
}
 
Example 11
Source File: FtpClient.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getSecurityData() {
    String s = getLastResponseString();
    if (s.substring(4, 9).equalsIgnoreCase("ADAT=")) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Need to get rid of the leading '315 ADAT='
            // and the trailing newline
            return decoder.decodeBuffer(s.substring(9, s.length() - 1));
        } catch (IOException e) {
            //
        }
    }
    return null;
}
 
Example 12
Source File: FtpClient.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private byte[] getSecurityData() {
    String s = getLastResponseString();
    if (s.substring(4, 9).equalsIgnoreCase("ADAT=")) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Need to get rid of the leading '315 ADAT='
            // and the trailing newline
            return decoder.decodeBuffer(s.substring(9, s.length() - 1));
        } catch (IOException e) {
            //
        }
    }
    return null;
}
 
Example 13
Source File: Base64GroupTest.java    From MMall_JAVA with GNU General Public License v3.0 5 votes vote down vote up
public static String getFromBase64(String s) {
    byte[] b = null;
    String result = null;
    if (s != null) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            b = decoder.decodeBuffer(s);
            result = new String(b, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}
 
Example 14
Source File: GZipUtil.java    From pmq with Apache License 2.0 5 votes vote down vote up
public static String uncompress(String value) {
	if ((value == null) || (value.isEmpty())) {
		return "";
	}
	BASE64Decoder tBase64Decoder = new BASE64Decoder();
	try {
		byte[] t = tBase64Decoder.decodeBuffer(value);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(t);
		GZIPInputStream gunzip = new GZIPInputStream(in);
		try {
			byte[] buffer = new byte[256];
			int n;
			while ((n = gunzip.read(buffer)) >= 0) {					
				out.write(buffer, 0, n);
			}
		} finally {
			gunzip.close();
		}
		in.close();
		out.close();
		return out.toString("UTF-8");
	} catch (Exception ex) {
		logger.error(ex.getMessage());
	}
	return value;
}
 
Example 15
Source File: DigestUtils.java    From opencron with Apache License 2.0 5 votes vote down vote up
public static String passBase64(String s) {
    byte[] b = null;
    String result = null;
    if (s != null) {
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            b = decoder.decodeBuffer(s);
            result = new String(b, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}
 
Example 16
Source File: Base64Utils.java    From ns4_gear_watchdog with Apache License 2.0 5 votes vote down vote up
/**
 * base64字符串转换成图片
 * @param imgStr		base64字符串
 * @param imgFilePath	图片存放路径
 * @return
 *
 * @author ZHANGJL
 * @dateTime 2018-02-23 14:42:17
 */
public static boolean Base64ToImage(String imgStr,String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片

	if (imgStr == null || imgStr.equals("")) // 图像数据为空
		return false;

	BASE64Decoder decoder = new BASE64Decoder();
	try {
		// Base64解码
		byte[] b = decoder.decodeBuffer(imgStr);
		for (int i = 0; i < b.length; ++i) {
			if (b[i] < 0) {// 调整异常数据
				b[i] += 256;
			}
		}

		OutputStream out = new FileOutputStream(imgFilePath);
		out.write(b);
		out.flush();
		out.close();

		return true;
	} catch (Exception e) {
		return false;
	}

}
 
Example 17
Source File: CaptchaImageForPy.java    From ticket with GNU General Public License v3.0 5 votes vote down vote up
public String check(String base64String) {
    String filename = UUID.randomUUID() + ".jpg";
    File folder = new File("temp");
    if (!folder.exists()) {
        folder.mkdirs();
    }
    File file = new File(folder, filename);
    try {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] data = decoder.decodeBuffer(base64String);
        IOUtils.copy(new ByteArrayInputStream(data), new FileOutputStream(file));
        Runtime runtime = Runtime.getRuntime();
        String os = System.getProperty("os.name");
        Process process;
        if (os.toLowerCase().startsWith("win")) {
            String[] cmd = new String[]{"cmd", "/c", "cd python  &  set PYTHONIOENCODING=UTF-8 & python main.py " + "..\\temp\\" + filename};
            process = runtime.exec(cmd);
        } else {
            String bash = System.getProperty("user.dir") + "/" + config.getPythonPath() + "/run.sh " + System.getProperty("user.dir") + "/temp/" + filename;
            process = runtime.exec(bash);
        }
        process.waitFor();
        InputStream inputStream = process.getInputStream();
        String r = this.get(inputStream);
        if(StringUtils.isEmpty(r)){
            inputStream = process.getErrorStream();
            r = this.get(inputStream);
        }
        return r;
    } catch (Exception e) {
        log.error("", e);
        return "系统出错,联系QQ:960339491";
    } finally {
        if (file.exists() && file.isFile()) {
            file.delete();
        }
    }
}
 
Example 18
Source File: Base64Util.java    From jeewx-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 字符串转图片
 *
 * @param base64Str
 * @return
 */
public static byte[] decode(String base64Str) {
    byte[] b = null;
    BASE64Decoder decoder = new BASE64Decoder();
    try {
        b = decoder.decodeBuffer(replaceEnter(base64Str));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return b;
}
 
Example 19
Source File: DataMiningServlet.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private SourceBean getTemplateContent(HttpServletRequest servletRequest, ServletContext servletContext) {
	logger.debug("IN");
	SourceBean templateSB = null;

	ContentServiceProxy contentProxy = (ContentServiceProxy) env.get(EngineConstants.ENV_CONTENT_SERVICE_PROXY);

	requestParameters = ParametersDecoder.getDecodedRequestParameters(servletRequest);
	Content template = null;
	try {
		template = contentProxy.readTemplate(documentId, requestParameters);
		logger.debug("Read the template=" + template.getFileName());
	} catch (Exception e) {
		logger.error("Impossible to read Template");
		throw new SpagoBIRuntimeException("Impossible to read Template" + e);
	}
	InputStream is = null;
	byte[] templateContent = null;
	String s;
	try {
		BASE64Decoder bASE64Decoder = new BASE64Decoder();
		templateContent = bASE64Decoder.decodeBuffer(template.getContent());
		is = new java.io.ByteArrayInputStream(templateContent);
		int n = is.available();
		byte[] bytes = new byte[n];
		is.read(bytes, 0, n);
		s = new String(bytes, StandardCharsets.UTF_8);
		templateSB = SourceBean.fromXMLString(s);
	} catch (Throwable t) {
		logger.warn("Error on decompile", t);
	}
	return templateSB;
}
 
Example 20
Source File: GetOfficeContentAction.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public void service(SourceBean request, SourceBean responseSb) throws Exception {
    logger.debug("IN");
    freezeHttpResponse();
    
	HttpServletResponse response = getHttpResponse();
	HttpServletRequest req = getHttpRequest();
	AuditManager auditManager = AuditManager.getInstance();
	// AUDIT UPDATE
	Integer auditId = null;
	String auditIdStr = req.getParameter(AuditManager.AUDIT_ID);
	if (auditIdStr == null) {
	    logger.warn("Audit record id not specified! No operations will be performed");
	} else {
	    logger.debug("Audit id = [" + auditIdStr + "]");
	    auditId = new Integer(auditIdStr);
	}
	
	try {
	
		if (auditId != null) {
		    auditManager.updateAudit(auditId, new Long(System.currentTimeMillis()), null, "EXECUTION_STARTED", null,
			    null);
		}
		
		SessionContainer sessionContainer = this.getRequestContainer().getSessionContainer();
		SessionContainer permanentContainer = sessionContainer.getPermanentContainer();
		IEngUserProfile profile = (IEngUserProfile) permanentContainer.getAttribute(IEngUserProfile.ENG_USER_PROFILE);
		if (profile == null) {
			throw new SecurityException("User profile not found in session");
		}
		
		String documentId = (String)request.getAttribute("documentId");
		if (documentId == null) 
			throw new Exception("Document id missing!!");
		logger.debug("Got parameter documentId = " + documentId);
		
		ContentServiceImplSupplier c = new ContentServiceImplSupplier();
		Content template = c.readTemplate(profile.getUserUniqueIdentifier().toString(), documentId, null);
		String templateFileName = template.getFileName();

		logger.debug("Template Read");

		if(templateFileName==null){
			logger.warn("Template has no name");
			templateFileName="";
		}
		
		response.setHeader("Cache-Control", ""); // leave blank to avoid IE errors
		response.setHeader("Pragma", ""); // leave blank to avoid IE errors 
		response.setHeader("content-disposition","inline; filename="+templateFileName);	
		
		String mimeType = MimeUtils.getMimeType(templateFileName);
		logger.debug("Mime type is = " + mimeType);
		response.setContentType(mimeType);

		BASE64Decoder bASE64Decoder = new BASE64Decoder();
		byte[] templateContent = bASE64Decoder.decodeBuffer(template.getContent());
		
		response.getOutputStream().write(templateContent);
		response.setContentLength(templateContent.length);
		response.getOutputStream().flush();	
		response.getOutputStream().close();
		
	    // AUDIT UPDATE
	    auditManager.updateAudit(auditId, null, new Long(System.currentTimeMillis()), "EXECUTION_PERFORMED", null,
		    null);
	
	} catch (Exception e) {
	    logger.error("Exception", e);
	    // AUDIT UPDATE
	    auditManager.updateAudit(auditId, null, new Long(System.currentTimeMillis()), "EXECUTION_FAILED", e
		    .getMessage(), null);
	} finally {
	    logger.debug("OUT");
	}
}