一、安裝ANT工具
所謂安裝其實(shí)就是下載下來解壓,最后解壓到C盤。當(dāng)然要配置環(huán)境變量。如解壓到C:/ant,那么ANT_HOME="c:/ant",path="c:/ant/bin"。
二、配置biuld.xml
講究很多,屬性很多,介紹它的文章也非常多,這里我舉出一個最簡單的例子,也是我第一次使用ANT時的配置。
<project name="mySite" basedir="." default="compile">
<path id="lib">
<fileset dir="G:/docfiles/mySite/WEB-INF/lib/">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="compile" depends="">
<javac srcdir="G:/docfiles/mySite/src" destdir="G:/docfiles/mySite/WEB-INF/classes" classpathref="lib"/>
</target>
</project>
參數(shù)說明:
<project/>為根目錄,里面的name="mySite"為要編譯項目的名字;default屬性定義ANT默認(rèn)要執(zhí)行的
任務(wù),在這里就是javac,編譯。
<fileset/>里的 dir值為項目中用到的jar根目錄;<include name="**/*.jar"/>包含里面所有.jar包。
<javac/>里的srcdir為要進(jìn)行編譯的java文件的根目錄,destdir為編譯好后的class文件放的位置。
三、運(yùn)行
在dos窗口找到ant/bin,直接輸入ant運(yùn)行。
----------------------------------------------------------------------------------------------
fileUpload組件實(shí)現(xiàn)圖象上傳
FormFile file = imageForm.getFilePath();//取得上傳的文件
try {
InputStream stream = file.getInputStream();//把文件讀入輸入流
java.awt.Image image = ImageIO.read(stream);//創(chuàng)建image對象,這樣就可以對圖象進(jìn)行各種處理
//計算長寬
int toWidth =500;//默認(rèn)值
int toHeigh = 500;
String tempWidth = request.getParameter("width");//接受前臺指定圖象的大小值
String tempHeight = request.getParameter("height");
if(!"".equals(tempWidth) && tempWidth != null){
toWidth = Integer.valueOf(tempWidth);
}
if(!"".equals(tempHeight) && tempHeight != null){
toHeigh = Integer.valueOf(tempHeight);
}
int old_w = image.getWidth(null); //得到源圖像的寬
int old_h = image.getHeight(null);
int new_w = 0;//縮略后的圖象寬
int new_h = 0;
float ratioWidth = old_w/toWidth;//寬的縮放比例
float ratioHeight = old_h/toHeigh;//高的縮放比例
if(ratioWidth > ratioHeight){//要保證縮放后的圖象長寬都不能大于目標(biāo)長寬,所以除以比例大的數(shù)值
new_w = Math.round(old_w / ratioWidth);
new_h = Math.round(old_h / ratioWidth);
}else{
new_w = Math.round(old_w / ratioHeight);
new_h = Math.round(old_h / ratioHeight);
}
//長寬處理結(jié)束
BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null); //繪制縮放后的圖
String currentDirPath = request.getSession().getServletContext().getRealPath("/Upload/Image");//要上傳到服務(wù)器上的
位置,即當(dāng)前路徑+Upload/Image
String oldeName = imageForm.getFilePath().getFileName();//帶擴(kuò)展名的鴨圖象的名字
String newName = getFileName(oldeName);//新圖象名,由系統(tǒng)當(dāng)前年+月+日+小時+分+秒+毫秒+四為隨機(jī)數(shù)
+原擴(kuò)展名組成,保證大部分情況下不會出現(xiàn)重名問題
String savePath = currentDirPath + "/" + newName;
OutputStream bos = new FileOutputStream(savePath);//創(chuàng)建輸出流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(tag); //近JPEG編碼
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//將文件寫入服務(wù)器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}