1.上傳圖片
圖片在客戶端JavaScipt的判斷,和服務器端action中的判斷
A. javaScipt判斷
當用戶選中要上傳的圖片後,可爲<input type="file" id="imgfile" name= "imgfile" onchange="chkimg(this.value)"> 添加onchange事件
JavaScipt代碼如下:
<script type="text/javascript">
var img=null;
function chkimg(inp) //判斷文件是否爲圖片,不只是判斷後綴名
{
if(img)img.removeNode(true); //判斷其中是否有圖片內容,有則清空
if(inp!=null && inp != "")
{
var patn = /\.jpg$|\.jpeg$|\.gif$/i; //圖片格式,如果不屬於這 幾種則不進行圖片判斷
if(patn.test(inp)) //判斷文件名是否是屬於上面的圖片格式
{
img=document.createElement("img"); //創建一個對象
img.attachEvent("onreadystatechange",isimg); //綁定方法, 圖片信息正確時調用
img.attachEvent("onerror",notimg); //綁定方法, 圖片信息錯誤時調用
img.src=inp; //設置其src爲得到的路徑
}else
{
show1.innerHTML = "不是圖片!!"; //給出提示
}
}else{ //當沒有值時,則爲不上傳,不進行判斷
show1.innerHTML = "";
}
}
function notimg()
{
show1.innerHTML = "您插入圖片錯誤,或者該圖片已經損壞!";
}
function isimg()
{
show.insertAdjacentElement("BeforeEnd",img);
show1.innerHTML = "圖片大小:"+ img.fileSize/1024+
"圖片寬度:"+ img.offsetWidth+
"圖片高度:"+ img.offsetHeight;
}
</script>
B. action中判斷
當跳轉到action中時進行圖片判斷,代碼如下:
/**
* 判斷圖片是否正確,當文件後綴名爲圖片格式時,才進行此判斷
* 注:該方法不能判斷動畫格式的圖片,動畫格式的圖片無法通過
* @param inputStream 輸入流
* @return(當圖片文件不正確時,返回False,正確時返回true)
*/
private boolean isValidImage(InputStream inputStream) {
if (inputStream!=null) {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(inputStream);
try {
decoder.decodeAsRaster(); //check the image format
}catch (Exception e) {
//e.printStackTrace();
System.out.println("image problem");
return false;
}
}
return true;
}
上面爲判斷圖片是否正確,當頁面判斷通過後,跳轉到action中
得到界面上傳的文件路徑request.getParameter(“”);
創建File對象,File file = new File(“路徑”);
得到其後綴名,判斷是否爲圖片格式,如果是則進行圖片是否正確判斷
如果想要得到圖片的寬,高,可用下面方法:
InputStream input = new FileInputStream(file); //輸入流
BufferedImage bi = ImageIO.read(input); //得到BufferedImage
注:使用BufferedImage進行判斷圖片,可以通過動畫形式的圖片
If(bi != null){ //如果不爲空,則表示圖片正確,
int width = bi.getWidth(); //上傳圖片的寬
int height = bi.getHeight(); //上傳圖片的高
}
當一切都判斷完了則進行文件上傳,
String name = file.getName(); //原文件路勁名
int random = (int)(Math.random()*10000); //隨機數
String fileName = System.currentTimeMillis() + random
name.substring(name.indexOf("."));
//通過得到系統時間加隨即數字生成新文件名,避免重複
得到新的文件名,以避免重複,創建輸入流
InputStream input = new FileInputStream(file); //輸入流
創建緩衝區,創建輸入流,進行文件上傳,代碼如下:
byte[] buff = new byte[4096]; //緩衝區
FileOutputStream output = new FileOutputStream("存放路徑+新文件名");
int bytecount = 1;
while((bytecount = input.read(buff, 0, 4096)) != -1){
//當input.read()方法,不能讀取到字節流的時候,返回-1
output.write(buff, 0, bytecount); //寫入字節到文件
}
output.flush();
output.close();
2.其他文件上傳
使用Struts中的FormFile控件上傳文件
在界面中使用<html:file property="testFile"></html:file>
在ActionForm中定義FormFile對象;
在Action中得到ActionForm中的FormFile對象。
實例:UploadForm uploadForm = (UploadForm)form;
FormFile file = uploadForm.getTestFile();
然後得到其後綴名,判斷是否爲圖片格式,如果爲圖片格式則進行圖片正確 判斷,判斷完成後進行文件上傳
String name = file.getFileName(); //原文件路勁名
int random = (int)(Math.random()*10000); //隨機數
String fileName = System.currentTimeMillis() + random
name.substring(name.indexOf("."));
//通過得到系統時間加隨即數字生成 新文件名,避免重複
FileOutputStream output = new FileOutputStream("存放路徑+新文件名");
output.write(file.getFileData()); //將文件輸出到指定路徑
output.flush();
output.close();
進行文件輸入輸出的時候也可以用上面的方式
上傳圖片時,改變圖片大小並存儲
圖片上傳後,有需要改變圖片大小可使用下面方法:
/**
* 改變上傳圖片大小
* @param img BufferedImage 對象(通過ImageIO.read()方法得到)
* @param name 原文件後綴名
* @param filePath 上傳圖片存放路徑
* @return
*/
public static boolean uploadJPGfile(BufferedImage img, String name,String filePath)
{
try
{
// 轉爲jpg標準格式//
int random = (int)(Math.random()*10000); //隨機數
String fileName = System.currentTimeMillis()+random+name; // 通過得到系統時間加隨機數生成新文件名,避免重複
if (img != null) {
int new_w = 150; // 新圖片寬
int new_h = 100; // 新圖片高
BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(img, 0, 0, new_w, new_h, null); // 繪製縮小後的圖
FileOutputStream out = new FileOutputStream(filePath+fileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
return true;
} else{
return false;
}
} catch (Exception e) {
System.out.println("異常錯誤!");
return false;
}
}