這是一個十分簡單的程式,主要的功用在於把二張size一樣的圖,合成在一張圖的左右二邊,程式寫的蠻死的,一定要size一樣,有興趣的人可以自己改成動態的二張圖。
首先我們先找了二張圖,就下面二張,如果突然找不到圖的朋友們,可以直接下載下面二張來使用。
圖一,1.jpg

程式教學,java,eclipse,.net,gui,netbean,phtyon
這是一個十分簡單的程式,主要的功用在於把二張size一樣的圖,合成在一張圖的左右二邊,程式寫的蠻死的,一定要size一樣,有興趣的人可以自己改成動態的二張圖。
首先我們先找了二張圖,就下面二張,如果突然找不到圖的朋友們,可以直接下載下面二張來使用。
圖一,1.jpg

這是一個簡單的fileupload程式,把整個upload動作都包裝在UploadTool這個class裡,而在jsp裡再call此class來做檢查及上傳等動作。jar檔需放置WEB-INF/lib/裡,而程式complier後放置WEB-INF/classes/toolkie/裡。
首先需要二個第三方的jar檔 Apache Filupload及Apache common io 都下載Binary的jar檔就可以了
檔案的配制結構如下圖:

再來利用下面的程式來進行上傳作業
Read More大部份在寫網頁要上傳檔案時都需要使用到iframe做一個中繼,可是如果上傳POST時有錯訊息時,前一個iframe資料就會變成記錄history,而此時如果使用者重新整理時,就會遇到暫存資料的問題,iframe裡的內容會再被執行一次。
以下有一個JavaScript方法,可以在檢查完所有form條件時才自動建立iframe內容createiFrame做post動作,而重新整理時(或第一次進入此畫面時)又不會真實存在,來解決暫存的問題。
當呼叫createIFrame時,會在body這一個tab建立一個iframe元素,並把需求屬性設定好,像是frameborder=0、width=0、height=0等。
function createIFrame() {
if (!document.getElementById("_hiddenframe"))
{
var frame = document.createElement("iframe");
frame.setAttribute("name", "_hiddenframe1");
frame.setAttribute("src", "about:blank");
frame.setAttribute("frameborder", "0");
frame.setAttribute("height", "0");
frame.setAttribute("width", "0");
frame.setAttribute("id", "_hiddenframe");
frame.name = "_hiddenframe";
document.body.appendChild(frame);
window.frames._hiddenframe.name = "_hiddenframe";
document.getElementsByTagName("body")[0].appendChild(frame);
}
}
一般在寫Ajax時,最直覺的使用方法就是利用Ajax Object來要求Server給與回應, 再依回應的內容解西倒底是完成動作?還是有錯誤產生,如必填欄位未填。
這裡介紹一個小技巧,在編譯式的網頁伺服器語言可能比較合適使用。
大概的原理是利用程式在執行有錯誤時Throw Exception來當做錯誤訊息, 回傳給client,而client只要接收到503的錯誤,則就可以知道動作並未完成, 反之則是完成。
範例使用prototype來使用Ajax,傳送及回應,有需要可以參考:
以下是載行結果,當有填值時,則回應填寫的值,
沒填值的時候就回傳錯誤訊息。(Exception)


程式很簡單 ,只需要在利用command在程式目錄下執行
java -jar ReSizeImage.jar
就可以啦!
預設是縮成以寬為500px基準,如果要改變寬可以利用
java -jar ReSizeImage.jar 數字
縮完的圖會放到程式目錄的resize目錄裡
我Blog都是以500寬為基準,我想應該夠用了吧^^
程式目錄結構
記得要先下載Imagick來安裝才能使用喔
按裝方法如下:
範例圖檔可以按圖下載,放置於程式目錄

class Tool整個壓縮以外的實作均在此
package image;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class Tool {
public Tool(int width) {
// 取得class路徑所有檔案
File f = new File(".");
File[] list = f.listFiles();
for (int i = 0; i < list.length; i++) {
// 是否為圖檔,是的話進行縮圖
if (isImg(getFileType(list[i].getName()))) {
System.out.println("處理檔案名稱:" + list[i].getName());
toCompressImg(list[i].getAbsolutePath(), getRealDir(list[i]
.getAbsolutePath())
+ "resize" + File.separator + list[i].getName(), width);
}
}
}
// 取得副檔名
private String getFileType(String str) {
int startIndex = str.lastIndexOf(46) + 1;
int endIndex = str.length();
return str.substring(startIndex, endIndex); // 副檔名
}
// 傳入副檔名 檢查是否為指定圖檔jpg gif png
private boolean isImg(String str) {
return (("jpg,gif,png").indexOf(str.toLowerCase())) > -1;
}
// 找目錄
private String getRealDir(String str) {
int endIndex = str.lastIndexOf(File.separator) + 1;
int startIndex = 0;
return str.substring(startIndex, endIndex); // 副檔名
}
public void toCompressImg(String iFile, String oFile, int newWidth) {
try {
File fi = new File(iFile); // 大圖文件
if (!fi.exists())
return;
String oPath = oFile
.substring(0, oFile.lastIndexOf(File.separator));
File foPath = new File(oPath);
if (!foPath.exists())
if (foPath.mkdir())
System.out.println("ooo toCompressImg() File out path:"
+ oPath + " mkdir成功!");
File fo = new File(oFile); // 將要轉換出的小圖文件
int nw = newWidth;
// AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = 0, h = 0;
try {
w = bis.getWidth();
h = bis.getHeight();
} catch (Exception e) {
fileCopy(fi, fo);
return;
}
if (nw > (w < h ? h : w)) {
fileCopy(fi, fo);
return;
} // 壓縮寬度比原始檔大,那就不壓縮了;
int nh = (nw * h) / w;
// double sx = (double) nw / w;
// double sy = (double) nh / h;
JMagickScale.Compress(fi.getAbsolutePath(), fo.getAbsolutePath(),
nw, nh);
/*
* transform.setToScale(sx, sy); //System.out.println(w + ", " + h);
* AffineTransformOp ato = new AffineTransformOp(transform, null);
* BufferedImage bid = new BufferedImage(nw, nh,
* BufferedImage.TYPE_3BYTE_BGR); ato.filter(bis, bid);
* ImageIO.write(bid, "jpeg", fo);
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void fileCopy(File inputFile, File outputFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
int c;
while ((c = fis.read()) != -1)
fos.write(c);
fis.close();
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]) {
int width = 500;
try {
width = Integer.parseInt(args[0]);
} catch (Exception e) {
}
if (width <= 0) {
System.out.println("Width Error");
} else {
new Tool(width);
}
}
}
class JmagickScale壓縮的實作
package image;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
public class JMagickScale {
public static void Compress(String source, String To, int width, int height)
throws MagickException {
if (System.getProperty("jmagick.systemclassloader") == null) {
System.setProperty("jmagick.systemclassloader", "no");
}
ImageInfo info = new ImageInfo(source);
MagickImage image = new MagickImage(info);
// resize image
MagickImage scaleImg = image.scaleImage(width, height);
// write image to file
scaleImg.setFileName(To);
scaleImg.writeImage(info);
}
}
成功的圖,可以按進去flickr看壓好的圖效果
