String a = "1234567890.123123"; DecimalFormat df = new DecimalFormat("#,###.##"); String result = df.format(Double.parseDouble(a)); System.out.println(result); //결과 //1,234,567,890.12
[카테고리:] JAVA/JSP
jsp page, taglib, import
<%@ page import="com.myweb.utils.Utils" %> /*이 부분은 콤마(,)로 여러개를 추가할 수 있다.*/ <%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="ax" uri="http://axisj.com/axu4j" %>
크로스사이트스크립트 필터(XSS)
일번적으로 <를 < 로, 또는 >를 > 로만 잡아주면 되지만,부득이한 상황으로 그렇게 못할 경우 아래 코드를 사용한다. iframe을 i_rame으로 2번째 문자를 _로 바꿔주는 형식이다. package com.mySite.utils; public class XssUtils { public static final String[] ignoreKeyword = new String[]{ // "<" // ,">" "\"" // ,"%22" ,"'" // ,"%27" // ,"%" // ,"&" ,"%00" // ,"\\(" … 계속 읽기 크로스사이트스크립트 필터(XSS)
JAVA 한글깨짐
아래처럼 한번에 테스트하니깐 속이 편하다. String title = "한글이 깨져서 뚁땅해~"; System.out.println("euc-kr → utf-8: " + new String(title.getBytes("euc-kr"), "utf-8")); System.out.println("euc-kr → ksc5601: " + new String(title.getBytes("euc-kr"), "ksc5601")); System.out.println("euc-kr → x-windows-949: " + new String(title.getBytes("euc-kr"), "x-windows-949")); System.out.println("euc-kr → iso-8859-1: " + new String(title.getBytes("euc-kr"), "iso-8859-1")); System.out.println("utf-8 → euc-kr: " + new String(title.getBytes("utf-8"), "euc-kr")); System.out.println("utf-8 → ksc5601: " … 계속 읽기 JAVA 한글깨짐
JAVA 나눗셈 소수이하 표기(BigDecimal)
두 개의 인수 중 한개 이상이 double 이면 double로 리턴된다.double c = (double) 100 / 3; 아래는 소수자릿수를 원하는 만큼 보여주는 방식이다. 1. 첫번째 방법(리턴타입 Double) System.out.println(Math.round((c)*100)/(double) 100); //33.33System.out.println(Math.round((c)*1000)/(double) 1000); //33.333System.out.println(Math.round((c)*10000)/(double) 10000); //33.3333 2. 두번째 방법(리턴타입 String) System.out.println(String.format("%.2f", c)); //33.33System.out.println(String.format("%.3f", c)); //33.333System.out.println(String.format("%.4f", c)); //33.3333 3. Double, Float의 계산 오류 >> BigDecimal 사용 double d = … 계속 읽기 JAVA 나눗셈 소수이하 표기(BigDecimal)
JAVA에서 javascript 엔진 사용하기
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public static void main(String[] agrgs){ ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); //javascript 엔진 불러오기 ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("js"); try { String value = scriptEngine.eval("(Math.round(1/3*10000)/10000).toFixed(4)").toString(); System.out.println(value); } catch (ScriptException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
현재 시간 밀리세컨드 millisecond 구하기
public String getCurrentTimeStamp() { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()); }
외부 URL 이미지, html을 response
public void responseURLImage(HttpServletResponse res){ res.setStatus(200); res.setContentType("application/octet-stream"); String imageLocation = "https://www.aaboo.co.kr/img/aaboo.gif"; URL imgUrl = new URL(imageLocation); InputStream in = new BufferedInputStream(imgUrl.openStream()); OutputStream out = res.getOutputStream(); byte[] buf = new byte[1024]; int count = 0; while((count = in.read(buf)) >=0){ out.write(buf, 0, count); } out.close(); in.close(); } 위의 경우는 이미지파일이 다운받아진다. 브라우저에서 다운받는 대신, 링크를 이동할 경우 아래처럼 … 계속 읽기 외부 URL 이미지, html을 response
Mybatis 쿼리를 바인딩된 String으로 가져오는 방법
마이바티스에서 수행되어 만들어진 쿼리를 추출하고 싶을 때 사용합니다. @Autowired SqlSession sqlSession 을 사용합니다. 1. Mapper.xml 에 쿼리가 아래와 같다고 가정하고 <select id="selectTest"> select * from test where idx1 = #{param.idx1} and idx2 = #{param.idx2} and userid = #{param.session.userid} and testList in ( <foreach collection="param.testList" item="testList" separator=","> #{testList} </foreach> )</select> 2. 파라미터를 HashMap 형식일 경우(기본적으로 모두 … 계속 읽기 Mybatis 쿼리를 바인딩된 String으로 가져오는 방법
PDF 파일 처리 – JPG>PDF, PDF+PDF, 비밀번호 주입
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; public class PdfUtils { private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class); /** * PDF파일에 비밀번호를 주입한다. * @Param input … 계속 읽기 PDF 파일 처리 – JPG>PDF, PDF+PDF, 비밀번호 주입