2022年12月12日 星期一

Spring Batch發生 EmptyResultDataAccessException 的排除

日前有個job Fail拋送EmptyResultDataAccessException,因為有組套設計故update會有多筆相符狀況,去爬了文找找發現是UPDATE的機制造成的。




解決方式: 使用 setAssertUpdates(boolean); 設定成FALSE後排除,不過使用上還是得依照Table Schema設計,若無多筆會符合情況下還是保留預設會比較安全。

2022年10月13日 星期四

HTML Page Convert to PDF

產製PDF在拉版時往往相對耗時,尤其是在有多頁的情況下相對搞工。測出來HTML FILE直轉PDF效果還不賴,做個備忘。

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;

public void ConvertPDF(String htmlPath ) {
String src = htmlPath;
try {
ConverterProperties properties = new ConverterProperties();
DefaultFontProvider fontProvider = new DefaultFontProvider();
FontProgram fontProgram;
try {
fontProgram = FontProgramFactory.createFont("c:\\windows\\fonts\\kaiu.TTF");
fontProvider.addFont(fontProgram);
properties.setFontProvider(fontProvider);
} catch (IOException e) {
System.out.println("creat base font erro:" + e );
}

HtmlConverter.convertToPdf(new File(src), new File(htmlPath.replace(".html", ".pdf")), properties);
} catch (Exception ex) {
System.out.println(ex);
}

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</artifactId>
    <version>4.0.3</version>
</dependency>

後記:
2022/12/12 使用者反映轉出來的PDF有文字被裁切的問題,檢視後發現Bootstrap的CSS套用上拋了許多Exception出來,看來是HTML套版改用Bootstrap造成的問題,解決方式更新為 V4.0.4 後再檢視PDF成果已無被裁切之情事,結案。

iTextPDF add image

近期處理個需求需要在產出的PDF上面加上章戳圖,專案使用的是iTextPDF所以爬了些文找到了這方式,很順利的處理需求!! 做個備忘。

import java.net.URL;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;

document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\test.pdf"));
document.open();

//取得圖檔存放路徑
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
String spath = url.toString();
spath = spath.substring(0, spath.indexOf("WEB-INF"));
String imageFile = spath + "test.jpg";

PdfContentByte cb = writer.getDirectContent();
mage imgSoc = Image.getInstance(imageFile);
//設定大小
imgSoc.scaleToFit(60, 45);                     
//設定位置
imgSoc.setAbsolutePosition(490, 760); 
cb.addImage(imgSoc);

document.close();

補充說明:若原圖縮小到合適的SIZE後加入,印出來會相對模糊,因此改用scaleToFit來做縮圖處理,這樣列印的效果USER會比較滿意。

2022年10月6日 星期四

java read from DB2 column by Bolb type

近期配合保留原始資料將class資料轉json sting 存到DB的blob欄位內,底下是JAVA取值用的邏輯備忘下,試了許多方式就這樣最精簡也符合需要。

至於為什麼選擇blob而不用clob這個主要是考量若干資安掃描會去檢查cloumn data是否有踩紅線,改bytes array可以省去不必要的困擾,不然blob通常拿來存圖或是編譯後的檔案(PDF),這類json string存clob在DB tools上會較方便識別,就看各自的需要囉!!


source json data: 
bldata value is  new Gson().toJson(Variable.class).getBytes("utf-8")

Read

select bldata from table where length(bldata) > 4 with ur;

Blob blob = resultSet.getBlob("bldata"); 

byte[] bdata= blob.getBytes(1, (int) blob.length());

Variable.class Variables jsonStr = new Gson().fromJson(new String(bdata), Variable.class);

2022年9月21日 星期三

JAVA WEB專案中呼叫外部JAR執行取得回傳

 近日碰到了個需求,要整併外部的JAR到專案中,不過對方的JAR SIZE數十MB而自已所需要的只是裡面其中一個函式,故放入lib呼叫會造成專案封裝後的SIZE大增,不太OK~所以找到了ProcessBuilder這個好用的solution來,底下做個備忘囉!!

完工後的結果如下,效果如預期的方向呈現,OK的!

Process proc = null;

BufferedReader reader = null;
StringBuilder sbTravel = new StringBuilder();
try {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("cmd", "/c", "java -jar d://hello.jar");
processBuilder.redirectErrorStream(true);
proc = processBuilder.start();

reader = new BufferedReader( new InputStreamReader(proc.getInputStream(), "Big5"));
String line;
while ((line = reader.readLine()) != null) {
if (StringUtils.isNotBlank(line)) sbTravel.append(line + "<br>");
}
int exitVal = proc.waitFor();
if (exitVal!=0) System.out.println("sbTravel:" + sbTravel + ",exitVal:" + exitVal);

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
if (proc != null) proc.getOutputStream().close();
} catch (IOException e) {
System.out.println("has IOException:" + e.toString());
}
}

補充:
間中也嘗試寫bat來執行,不過webSphere不給跑TOMCAT到是沒問題
Runtime.getRuntime().exec("cmd.exe /c start d:/hello.bat");

另外一個狀況是回傳值的取得,嘗試若干方法都沒能順利取值,間中也想過把console輸出寫成File來存取,但這方向容易有檔案被使用中卡住的exception(神奇的websphere),不然可以早些結案掉的。
Runtime.getRuntime().exec("java -jar d:/hello.jar > d:/bak/hello.TXT " );

後記:
2022-12-12 : 發行在WebSphere 9運行順利;但若改為WebSphere 8.5會拋送ssh handshake
exception,因此實作上等待伺服器更新緩不濟急因此用跳板方式轉到WebSphere 9的機器上
執行呼叫,缺點就是得多等個幾秒鐘。