锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
濡傛灉鏃墮棿鍏佽,51鐪熻鍥炲幓涓瓚熶簡.鏈変簺鎯沖康瀹朵漢浜嗐?/font>
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.upload.*;
import java.io.IOException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class UploadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse)throws Exception {
System.out.println("asdasdasdasdasdasd");
UploadForm uploadForm = (UploadForm) actionForm;
FormFile pic = uploadForm.getPic();
String picname = pic.getFileName();
String uploadFileName = servletRequest.getSession()
.getServletContext()
.getRealPath("upload")+"\\"+picname;
File upliadFile = new File(uploadFileName);
BufferedInputStream bis = null;
Image image = null;
BufferedOutputStream bos = null;
try{
if(pic.getFileSize()<2*1024*1024){
bis = new BufferedInputStream(pic.getInputStream());
image = javax.imageio.ImageIO.read(bis);
int width = image.getWidth(null);
int height = image.getHeight(null);
int w = 160;
int h = 120;
if(width>w||height>h){
BufferedImage bi = new BufferedImage(w,h,
BufferedImage.TYPE_INT_RGB);
bi.getGraphics().drawImage(image,0,0,w,h,null);
bos = new BufferedOutputStream(new FileOutputStream(
upliadFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(bi);
System.out.println(width * height);
}else{
bos = new BufferedOutputStream(new FileOutputStream(upliadFile));
byte[] date = new byte[5*1024];
int len = bis.read(date);
while (len!=-1){
bos.write(date);
len = bis.read(date);
}
}
}
return actionMapping.findForward("ok");
}catch(Exception e){
e.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if (bos != null)
bos.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return actionMapping.findForward("ok");
}
}