java照片处理:
/**
* @author yzh 照片处理
* @return
* @throws Exception
*/
public String getPersonnelPhoto() throws Exception {
HttpServletRequest req = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
pno = (String) session.get("no");
String zhiGongBHstr = req.getParameter("bh");
if (zhiGongBHstr == null)
zhiGongBHstr = pno;
String xingbiestr = req.getParameter("xingbie");
if (zhiGongBHstr != null && xingbiestr != null) {
QuaPersonPhoto ph = personQualityService
.getQuaPersonPhoto(zhiGongBHstr);
File url = null;
if (ph == null) {
getDefaultpic(xingbiestr, url, response);
} else {
if (ph.getPhoto() == null) {
getDefaultpic(xingbiestr, url, response);
} else {
byte[] photobytes = ph.getPhoto();
response.setContentType("image/jpeg");
OutputStream toClient = response.getOutputStream();
InputStream in = new ByteArrayInputStream(photobytes);
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
toClient.write(buf, 0, len);
}
toClient.close();
/* //第二方法
* Image image = null;
* InputStream isPhoto = new ByteArrayInputStream(photobytes);
* image = ImageIO.read(isPhoto);
* response.setContentType("image/jpeg");
* JPEGImageEncoder encoder =
* JPEGCodec.createJPEGEncoder(response.getOutputStream());
* JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam((BufferedImage) image);
* param.setQuality(1.0F, true);
* encoder.encode((BufferedImage) image, param);
* ImageIO.setUseCache(false); ImageIO.write((RenderedImage)
* image, "jpg", response.getOutputStream());
*/
}
}
}
return null;
}
/**
* @author yzh 取得默认图片
* @param xingbiestr
* @param url
* @param response
*/
private void getDefaultpic(String xingbiestr, File url,
HttpServletResponse response) {
if (xingbiestr != null) {
if ("1".equals(xingbiestr)) {
url = new File(ServletActionContext.getRequest()// 女
.getSession().getServletContext().getRealPath(
"/images/quality/QLoveLady.gif"));
}
if ("0".equals(xingbiestr)) {// 男
url = new File(ServletActionContext.getRequest().getSession()
.getServletContext().getRealPath(
"/images/quality/QLoveMan.gif"));
}
BufferedInputStream bis = null;
ServletOutputStream sout = null;
if (url != null) {
try {
bis = new BufferedInputStream(new FileInputStream(url));
byte[] buffer = new byte[1024 * 8];
int result = -1;
sout = response.getOutputStream();
while ((result = bis.read(buffer)) != -1) {
sout.write(buffer, 0, result);
}
sout.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
sout.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
//项目中,默认图片的路径: