Skip to content

Instantly share code, notes, and snippets.

View kentyeh's full-sized avatar

Kent Yeh kentyeh

View GitHub Profile
@kentyeh
kentyeh / RFC3986.java
Last active November 18, 2015 03:17
RFC3986 java utility.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RFC3986 {
private static final Pattern ptn = Pattern.compile("%[A-Fa-f0-9]{2}");
private static final String character = "字元值[";
private static final String overRange = "]超出範圍";
private static final String string = "字串[";
private static final String containInvalidEncode = "]含有無效編碼";
@kentyeh
kentyeh / Utilities.java
Last active July 22, 2016 08:33
alert complex message in html
import java.util.*;
public class Utilities {
private static final Pattern pDquote = Pattern.compile("\"");
private static final Pattern pSquote = Pattern.compile("'");
private static final Pattern pBackSlash = Pattern.compile("\\\\");
private static final Pattern pNewline = Pattern.compile("[\n\r]+");
public static String alertInner(String msg) {
return msg == null || msg.trim().isEmpty() ? "" : pNewline.matcher(
pSquote.matcher(pDquote.matcher(pBackSlash.matcher(msg).replaceAll("\\\\\\\\")).replaceAll(""")).replaceAll("\\\\'")).replaceAll("\\\\r\\\\n");
@kentyeh
kentyeh / ForceDownload.java
Last active August 29, 2017 02:16
force download in servlet
public class DownloadAction extends Action {
public void downloadXls(HttpServletRequest request,HttpServletResponse response){
WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
//....Genrate Xls
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("UTF-8");
//starts with "attachment;" means force download,otherwise "inline;" instaed means view in browser
import java.math.BigInteger;
import java.util.UUID;
/** Code from
* https://github.com/cchigoriwa/jaxcsd/blob/master/jaxcsd-api/src/main/java/zw/co/hitrac/jaxcsd/api/util/UUIDBasedOID.java
*/
public class UUIDBasedOID {
protected static final String OID_PREFIX = "2.25"; // {joint-iso-itu-t uuid(25) <uuid-single-integer-value>}
@kentyeh
kentyeh / rolling3images.svg
Last active February 23, 2017 16:15
svg to rolling images
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kentyeh
kentyeh / Proj4j.java
Created October 24, 2018 08:26
TWD97座標轉經緯度
import org.osgeo.proj4j.CRSFactory;
import org.osgeo.proj4j.CoordinateReferenceSystem;
import org.osgeo.proj4j.CoordinateTransform;
import org.osgeo.proj4j.CoordinateTransformFactory;
import org.osgeo.proj4j.ProjCoordinate;
public class Proj4j {
/**
* https://search.maven.org/search?q=a:proj4j 找出proj4j轉案
* http://www.sunriver.com.tw/taiwanmap/grid_tm2_convert.php#a03 線上轉換(參考範本)
@kentyeh
kentyeh / ConvertMp3toWav
Created January 7, 2020 08:17
轉換Mp3為Wav
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import javax.sound.sampled.AudioFormat;
@kentyeh
kentyeh / ZhBigNum.java
Last active March 4, 2024 08:48
數值轉中文大寫
import java.util.regex.Pattern;
/**
* @author Kent Yeh
*/
public class ZhBigNum {
public static final Pattern ZEROP = Pattern.compile("\\x{96F6}{2,}");
public static void main(String[] args) {
@kentyeh
kentyeh / ResizeImage
Created January 14, 2020 06:47
重新調整影像大小
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class ResizeImage {
public static void resize(BufferedImage bufferedImage, int maxEdge, OutputStream os) throws IOException {
int w = bufferedImage.getWidth();
public static boolean isValidIDorRCNumber(String ID) {
if (ID == null || ID.trim().isEmpty() || !ID.matches("[A-Za-z][A-Da-d1289]\\d{8}")) {
return false;
}
String head = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
char[] id = ID.toUpperCase().toCharArray();
int chksum = (head.indexOf(id[0]) + 10) / 10
+ (head.indexOf(id[0]) + 10) % 10 * 9 % 10;
chksum += (id[1] > '@' && id[1] < 'E') ? ((head.indexOf(id[1]) + 10) % 10) * 8 % 10
: (id[1] - 48) * 8 % 10;