只剩下60分,大家帮下忙
- Java code
package com.webapp.model.service.impl;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.text.SimpleDateFormat;import java.util.Date;import javax.imageio.ImageIO;import javax.jws.WebService;import org.directwebremoting.annotations.Param;import org.directwebremoting.annotations.RemoteMethod;import org.directwebremoting.annotations.RemoteProxy;import org.directwebremoting.spring.SpringCreator;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGEncodeParam;import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.webapp.model.businessobject.Errorfeedback;import com.webapp.model.dao.ErrorfeedbackDao;import com.webapp.model.service.ErrorfeedbackService;import com.webapp.model.service.GridJsonDataService;import com.webapp.model.service.UsersService;import com.webapp.model.service.UtilService;import com.webapp.util.GuidCreator;@Service@WebService(endpointInterface = "com.webapp.model.service.ErrorfeedbackService")@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "errorfeedbackServiceImpl"), name = "ErrorfeedbackService")public class ErrorfeedbackServiceImpl implements ErrorfeedbackService { @Autowired private ErrorfeedbackDao errorfeedbackDao; @Autowired private UsersService usersService; @Autowired private UtilService utilService; @RemoteMethod public String saveErrorfeedback(Errorfeedback errorfeedback){ GuidCreator guid = new GuidCreator(); String typeStr = errorfeedback.getPicture().substring(errorfeedback.getPicture().lastIndexOf(".")+1); if(!"jpg".equals(typeStr) && !"png".equals(typeStr) && !"gif".equals(typeStr) && !"bmp".equals(typeStr)){ return "typeError"; }else{// errorfeedback.setPicture(errorfeedback.getPicture().substring(errorfeedback.getPicture().lastIndexOf("\\"))); System.out.println("图片路径:"+errorfeedback.getPicture()); System.out.println("E:\\testCreateImg"); String picPath = ""; try { picPath = LoadImageToServer(errorfeedback.getPicture(),"E:\\testCreateImg"); if(picPath != null && !"".equals(picPath)){ errorfeedback.setPicture(picPath); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } errorfeedback.setGid(guid.createNewGuid(3)); errorfeedback.setSubmitby(utilService.getCurrentUser().getGid()); errorfeedback.setSubmitTime(new Date());// try {// Image image = ImageIO.read(new File(errorfeedback.getPicture()));// errorfeedback.setPicture(new String(imageToBytes(image, "jpg")));// } catch (Exception e) {// // TODO: handle exception// } this.errorfeedbackDao.save(errorfeedback); return "true"; } public String LoadImageToServer(String filePath,String serverPath) throws Exception { String resultPath = ""; //上传后图片所在的路径 FileOutputStream out = null; //文件输出流 try { //验证图片上传的格式是否正确 File f = new File(filePath); if (!f.isFile()) { throw new Exception(" 不是图片文件!"); } if (f != null && f.exists()) { //这里的ImageIO属于java工厂类,在工厂类class里面,调用的System.gc(),频繁调用会造成dump,需要考虑优化 BufferedImage image = ImageIO.read(f); // 读入文件 if (image != null) { BufferedImage tag = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); //构造一个类型为预定义图像类型之一的 BufferedImage tag.getGraphics().drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); //绘制所需要尺寸大小的图片 /* * 以下生成图片上传后在服务器上的新路径 */ int lastLength = filePath.lastIndexOf("."); Date date = new Date(System.currentTimeMillis()); String strDate = new SimpleDateFormat("yyyyMMddhhmmss").format(date); int random = (int)(Math.random()*99); String imageName = strDate + random; //以系统时间来随机的创建图片文件名 String fileType = filePath.substring(lastLength); //获取上传图片的类型 resultPath = serverPath + "\\" + imageName + fileType; /* * 进行图片的绘制 */ out = new FileOutputStream(resultPath); //下面的代码下边都有黄线不知道和这个有没有关系…………………… JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag); param.setQuality(0.95f, true); //95%图像 param.setDensityUnit(1); //像素尺寸单位.像素/英寸 param.setXDensity(300); //水平分辨率 param.setYDensity(300); //垂直分辨率 encoder.setJPEGEncodeParam(param); encoder.encode(tag); tag = null; } } f = null; } catch (Exception ex) { ex.printStackTrace(); } finally { out.close(); out = null; } return resultPath; } }