记录下来,方便以后再用~·
servlet中图片存入:
public void SaveDeliveryReceipt(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {
String strAdmin = (String) req.getSession().getAttribute("isAdmin");
boolean isAdmin = "1".equals(strAdmin);
Long op_id = Long.parseLong(req.getParameter("op_id"));
String opt_type = objectToString(req.getParameter("op_type"));
Date opt_time = DateBuilder.strToUtilDateWithFormat(objectToString(req.getParameter("op_time")));
String op_cert_dn = URLDecoder.decode(objectToString(req.getParameter("op_certdn")), "utf-8");
String signLog = objectToString(req.getParameter("sign_data"));
JSONObject json = new JSONObject();
json.put("success", false);
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置允许用户上传文件大小,单位:字节
fu.setHeaderEncoding("UTF-8");
fu.setSizeMax(1000000000);
List fileItems = fu.parseRequest(req);
FileItem item = (FileItem) fileItems.get(0); // 只有一个文件
InputStream inStream = item.getInputStream();
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = inStream.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
//统一图片大小
this.ChangeImgSize(imgdata,300,300);
String send_number = objectToString(req.getParameter("send_number"));
CertSend certSend= new CertSend();
certSend.setSend_number(send_number);
CertSend objcertSend = certSendService.selectCert(certSend);
this.writeByteArryToImage(imgdata, send_number);
objcertSend.setReceive_evidence(imgdata);
inStream.close();
certSendService.updateCertSend(objcertSend);
String strErrMsg = "用户签收凭据导入成功!";
errMsg.setErrMsg(strErrMsg);
json.put("err", errMsg);
json.put("success", true);
if(!isAdmin){
opLog.writeOptLog(strErrMsg,op_id,null,op_cert_dn, opt_type,opt_time,"SUCCESS",signLog,null,null,null,null,null,null);
}
}
catch (Exception e) {
String strErrMsg = " 检测需要导入凭据是否已存在异常!";
errMsg.setErrMsg(strErrMsg);
json.put("err", errMsg.toJson());
if(!isAdmin){
sysLog.writeSysLog(strErrMsg,"FAILURE","SysCertImportServlet",e.getMessage());
opLog.writeOptLog(strErrMsg,op_id, null,op_cert_dn, opt_type,opt_time, "FAILURE", signLog,null,null,null,null,null,null);
}
showErrMsg(json,strErrMsg,"SysCertImportServlet",e);
}
finally{
try {
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write(json.toString());
}
catch (Exception e2) {
}
}
}
将blog类型取出:
public void GetImageByteId(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
String strId=req.getParameter("id");
CertSend temp=new CertSend();
temp.setSend_number(strId);
CertSend query=certSendService.selectCert(temp);
byte[] data=null;
data= query.getReceive_evidence();
try{
//清除页面缓存
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cahe-Control", "no-cache");
resp.setDateHeader("Expires", 0);
InputStream inStream=new ByteArrayInputStream(data);
int size=-1;
size=inStream.read(data, 0, data.length);
ServletOutputStream sout= resp.getOutputStream();
if(size>0){
sout.write(data,0,size);
}
inStream.close();
//data=ChangeImgSize(data,w,h);
}catch(Exception ex){
ex.printStackTrace();
}
}
前台ext页面显示:
{
xtype : "component",
width:200,
height:300,
autoHeight:true,
id:"browseImage",
name:"browseImage",
autoEl:{
tag:'img',
src:'certSend.fm?method=GetImageByteId&id='+check+''
}
}