功能说明:图片的上传和查看图片
具体实现:
思路:首先读取图片的属性,然后以流的形式建立在项目的指定路径(类型:char[]),然后给图片一个id和改项目指定的路径,然后图片的名称我就是用的当前的时间,精确到秒(这确实也可以加上id或者一些附加值方面后期的一些查询,我比较懒刚做就搞的最简单的),数据库中存的是图片的id和url,页面就是用的url显示的。另外要说的是这里没有写连接数据库这块,因为是公司内部框架所以不能公布。(大家可以用hibernate,或者jdbc都行)
首先是页面:
当然我们这里还是先读取文件的属性的,我是写在后台,然后在页面导入的,后台代码如下:
/**
* 读取properties文件
* @author Administrator
*
*/
public class ConfigVersoin {
private static Properties properties = null;
/**
* 读取文件属性
* @param read
*/
public static synchronized void readConfigProperties(boolean read) {
if(properties != null && !read)
return;
InputStream input = null;
try {
input = ConfigVersoin.class.getClassLoader().getResourceAsStream("config.properties");
if(read)
properties = null;
properties = new Properties();
properties.load(input);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(input != null)
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 根据key读取值
* @param key
* @return
*/
public static String getProperties(String key) {
if(properties == null){
readConfigProperties(false);
}
return (String)properties.get(key);
}
/**
* 根据类型读取照片配置路径
* @param type 0头像,1背景,2患者照片,4医院照片,5应用照片
* @return
*/
public static String photoPath(String type){
String path = ConfigVersoin.getProperties("photo_path");
StringBuffer bf = new StringBuffer();
bf.append(path).append("/");
if("0".equals(type)){//头像
path = ConfigVersoin.getProperties("user_portrait");
bf.append(path);
}else if("1".equals(type)){//背景
path = ConfigVersoin.getProperties("user_background");
bf.append(path);
}else if("2".equals(type)){//患者照片
path = ConfigVersoin.getProperties("patient_portrait");
bf.append(path);
}else if("3".equals(type)){//医院照片
path = ConfigVersoin.getProperties("hospital_logo");
bf.append(path);
}else if("4".equals(type)){//应用照片
path = ConfigVersoin.getProperties("app_img");
bf.append(path);
}
bf.append("/");
return bf.toString();
}
/**
* 获取版本号
* @return
*/
public static String jsversion(){
return getProperties("js_versoin");
}
}
HTML:
<%@page import="com.yuanqitech.web.util.ConfigVersoin"%>
<div align="right" class="fudong"> 照片:<img src="${patientInfo.photo_url }"
class="img_upload" type="2" comp="PatientInfoData" imgId="<s:property value="dialyse_id"/>" /></div>
后台代码:
/**
* 图片上传
* @author Administrator
*
*/
public class ImgUnloadAction extends BaseFormAction {
private ImgInfo img;
public String init(){
this.resultContent="/bqnjsp/img_upload.jsp";
return resultName;
}
public String unload(){
String poweremr = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/");
String path = ConfigVersoin.photoPath(img.getType());
FileUpload upload = new FileUpload();
img = upload.upload(img, poweremr, path);
int t = saveImgMsg(img.getImgId(), img.getImgPath(),img.getImgByte(), img.getComp());
String msg ="{\"rc\":\""+t+"\",\"path\":\""+img.getImgPath().replace("\\", "/")+"\"}";
HttpServletResponse response = ServletActionContext.getResponse();
HttpParserUtil.flush(msg, response);
return null;
}
private int saveImgMsg(String imgId,String path,byte[] bts,String comp){
if(comp != null && !"".equals(comp)){
CompService com = BaseDAOFactory.getCompService();
com.addParameter(imgId);
com.addParameterObject(bts);
com.addParameter(path);
Object o = com.invoke(comp, "uploadImg");
if("1".equals(o.toString())){
HttpSession session = ServletActionContext.getRequest().getSession();
UserInfo user = (UserInfo) session.getAttribute("userInfo");
user.setImage_url(path);//此处有修改
session.setAttribute("userInfo", user);
}
return Integer.valueOf(o.toString());
}else{
return -1;
}
}
public ImgInfo getImg() {
return img;
}
public void setImg(ImgInfo img) {
this.img = img;
}
}
//设计到切图片
public class FileUpload {
public String uploadByte(byte[] b,String pathname,String imgtype){
File dir = new File(pathname);
if(!dir.exists())
dir.mkdir();
String rtp = DateUtil.dateToString(new Date(),"yyyyMMddHHmmss");
String fileName = rtp + "." +imgtype;
File saveFile = new File(dir,fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(saveFile);//建立一個上传文件的输出流
fos.write(b);//将strutsFile文件流写入目标路径中
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close(); //任何时候都关闭
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return rtp;
}
public ImgInfo upload(ImgInfo imgInfo,String webpath,String filePath){
System.out.println("filePath=="+filePath);
if(imgInfo == null)
return null;
String imgfile = imgInfo.getFile();
String path = webpath + filePath;
try {
Map<String,String> fileMap = getfile(imgfile);
String file = fileMap.get("file");
String imgtype = fileMap.get("type");
byte[] b = new BASE64Decoder().decodeBuffer(file);
//物理地址存入图片,返回图片ID
String filename = new FileUpload().uploadByte(b, path,imgtype);
//数据库存放路径
String imgpath = filePath + filename +"."+ imgtype;
imgInfo.setImgPath(imgpath);//存放图片路径
imgInfo.setImgByte(b);//存放二进制图片
if(imgInfo.getX() != null && !"".equals(imgInfo.getX())){//剪切
int ix = math(imgInfo.getX(),imgInfo.getWb());
int iy = math(imgInfo.getY(),imgInfo.getHb());
int iw = math(imgInfo.getW(),imgInfo.getWb());
int ih = math(imgInfo.getH(),imgInfo.getHb());
//物理路径
String t = path + filename +"."+ imgtype;
ImageCut cut = ImageCut.init();
cut.setImageCut(t,ix, iy, iw, ih);
cut.cut();
}
} catch (Exception e) {
e.printStackTrace();
}
return imgInfo;
}
/**
* 根据64位图片格式,解析图片类型和数据
* @param file
* @return
*/
private Map<String, String> getfile(String file){
Map retMap = new HashMap();
String[] files = file.split(",");
String imgtype = files[0];
int s = imgtype.indexOf(":")+1;
int e = imgtype.indexOf(";");
imgtype = imgtype.substring(s, e);
String type = imgtype.split("/")[0];
if("image".equals(type)){
imgtype = imgtype.split("/")[1];
}else{
imgtype = "jpg";
}
retMap.put("type",imgtype);
retMap.put("file", files[1]);
return retMap;
}
/**
* 乘法
* @param w
* @param b
* @return
*/
private int math(String w,String b){
BigDecimal bx = new BigDecimal(w);
BigDecimal by = new BigDecimal(b);
BigDecimal bs = new BigDecimal("1");
int j = bx.multiply(by).divide(bs,0,BigDecimal.ROUND_HALF_UP).intValue();
return j;
}
}