java图片读写_java图片读写

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();

}

}

}

}

}

//项目中,默认图片的路径:

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值