1.创建 url.properties
#Recroad_logo
IMG_Recroad_LOG_PATH=d:/cmsimg/image/recroad/
IMG_Recroad_LOG_READPATH=http://192.168.1.73:8080/cmsimg/image/recroad/
2.RecroadAction类中
属性:
private static final ResourceBundle RESOURCE_BUNDLE_URL = ResourceBundle.getBundle("url");
private File logob;
public String saveDiv() {
KangarooRecroad kangarooRecroad = new KangarooRecroad();
kangarooRecroad.setModifytype(1);
kangarooRecroad.setTitle(title);
try {
String IMG_Recroad_LOG_PATH = RESOURCE_BUNDLE_URL
.getString("IMG_Recroad_LOG_PATH");
String IMG_Recroad_LOG_READPATH = RESOURCE_BUNDLE_URL
.getString("IMG_Recroad_LOG_READPATH");
String filename = null;
if (logob.exists() && logob != null) {
filename = recroadService.uploadLogo("yes",logob, logobFileName,
new File(IMG_Recroad_LOG_PATH));
kangarooRecroad.setLogob(IMG_Recroad_LOG_READPATH + filename);
} else {
kangarooRecroad.setLogob(null);
}
kangarooRecroad = recroadService.saveDiv(kangarooRecroad);
ServletActionContext.getRequest().setAttribute("flag", "flag");
setPicid(kangarooRecroad.getPicid());
} catch (Exception e) {
request.setAttribute("uploadFail", "文件上传失败");
e.printStackTrace();
}finally{
return "addpath";
}
}
//读取一个带图片对象时:
Action中加入:
if (logob.exists() && logob != null) {
filename = recroadService.uploadLogo("yes",logob, logobFileName,
new File(IMG_Recroad_LOG_PATH));
kangarooRecroad.setLogob(IMG_Recroad_LOG_READPATH + filename);
if (templogobpic != null) {
recroadService.delLogo(templogobpic, IMG_Recroad_LOG_PATH);
}
}
Service
public String uploadLogo(String node,File srcFile, String filename, File dir)
throws Exception{
StringBuffer path = new StringBuffer();
String fileType = (filename.substring(filename.lastIndexOf('.') + 1)).toLowerCase();
FileInputStream input=new FileInputStream(srcFile);
FileOutputStream out =null;
try{
byte[] b = new byte[4096*2];
int len;
if (!dir.exists()) {
dir.mkdirs();
}
int num = (int) (Math.random() * 999 + 1);
Calendar calendar = Calendar.getInstance();
path.append(calendar.get(Calendar.YEAR));
path.append(calendar.get(Calendar.MONTH));
path.append(calendar.get(Calendar.DAY_OF_MONTH));
path.append(calendar.get(Calendar.HOUR_OF_DAY));
path.append(calendar.get(Calendar.MINUTE));
path.append(calendar.get(Calendar.SECOND));
path.append(num);
String orgialPath=dir + "//" + path.toString()+"-." + fileType;
File dirFile = new File(orgialPath);
if(dirFile.exists()){
dirFile.delete();
}
out = new FileOutputStream(dirFile);
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
out.flush();
if(node.equals("yes")){
Boolean flag = createAbbreviateImg(orgialPath,path.toString()+"."+fileType);
if(!flag){
logger.debug("in RecroadMangerServiceImpl create AbbreviateImg failer..............");
}
return path.toString() + "." + fileType;
}
}
catch(Exception e){
e.printStackTrace();
return "faile";
}finally{
if(input !=null){
input.close();
}
if(out !=null){
out.close();
}
}
return path.toString() + "-." + fileType;
}
/**
* 创建缩略图片
* @param orgpath
* @param filename
* @return
* @description: 描述
*/
public static Boolean createAbbreviateImg (String orgpath,String filename){
Boolean flag = true;
String filetype = orgpath.substring(orgpath.lastIndexOf(".")+1,orgpath.length());
if("jpg".equalsIgnoreCase(filetype)||"gif".equalsIgnoreCase(filetype)){
try {
Image srcImage = ImageIO.read(new File(orgpath));
BufferedImage tag = new BufferedImage(380, 230,
BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcImage, 0, 0, 380, 230, null);
String newImage = IMG_Recroad_LOG_PATH + filename;
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
jep.setQuality(1, true);
encoder.encode(tag, jep);
srcImage.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
BufferedImage bis = ImageIO.read(new File(orgpath));
int width = bis.getWidth();
int height = bis.getHeight();
int wantwidth = 80;//想要的高度
int wantheight = (wantwidth * height) / width;
if(wantheight>wantwidth) {
wantheight = wantwidth;
wantwidth = (wantheight * width) / height;
}
double scalew = (double)wantwidth / width;
double scaleh = (double)wantheight / height;
AffineTransform transform = new AffineTransform();
transform.setToScale(scalew,scaleh);
AffineTransformOp at = new AffineTransformOp(transform, null);
BufferedImage bi = new BufferedImage(wantwidth, wantheight, BufferedImage.TYPE_3BYTE_BGR);
at.filter(bis,bi);
ImageIO.write(bi, "jpg", new File(IMG_Recroad_LOG_PATH+filename));//存储小图片
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
}
return flag;
}
//删除logo
public boolean delLogo(String logopic, String filepath) throws Exception {
Boolean flag = true;
String[] temp = logopic.split("/");
String filename = temp[temp.length - 1];
String bigImg ="";
String imgType=filename.substring(filename.length()-4, filename.length());
File f = new File(filepath + filename);
if (f.exists()) {
f.delete();
}
if(imgType.equals(".jpg")||imgType.equals(".gif")){
bigImg = filename.substring(0,filename.length()-4)+"-"+filename.substring(filename.length()-4, filename.length());
File f2 = new File(filepath + bigImg);
if (f2.exists()) {
f2.delete();
}
}
return flag;
}