//图片的存入
@SuppressWarnings("serial")
public class AddPhotoToAlbumAction extends ActionSupport {
private Photo photo;
private File address;//图片文件,通过<input class="text" type="file" name="address" />传入
private String addressFileName;//图片文件的后缀名,必须以命名"文件FileName"的形式命名
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
//获取该文件的后缀名
String path = addressFileName.substring(addressFileName.indexOf(".")+1);
photo.setContentType(path);
//获取字节形式的文件
byte[] rtnByContent = getPhotoContentInByte();
//使用hibernate存入数据库
Blob bC = Hibernate.createBlob(rtnByContent);
photo.setContent(bC);
}
/**
* 以缩略图的形式存储
* @return
*/
private byte[] getPhotoThumbnailInByte(){
byte[] rtn = null;
try{
Image src = ImageIO.read(address);
int oldWidth = src.getWidth(null);
int oldHeight = src.getHeight(null);
float divWidth = 200f;
int newWidth = 200;
int newHeight = 0;
float tmp;
if(oldWidth > newWidth){
tmp = oldWidth / divWidth;
newWidth = Math.round(oldWidth / tmp);
newHeight = Math.round(oldHeight / tmp);
}else{
newWidth = oldWidth;
newHeight = oldHeight;
}
int imageHeight = 100;
int imageWidth = 200;
BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = (Graphics2D)bufferedImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.clearRect(0, 0, imageWidth, imageHeight);
bufferedImage.getGraphics().drawImage(src, 0, 0, newWidth, newHeight, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(bufferedImage);
rtn = baos.toByteArray();
bos.close();
baos.close();
}catch(Exception ex){
}
return rtn;
}
/**
* 以正常模式存储
* @return
* @throws IOException
*/
private byte[] getPhotoContentInByte() throws IOException{
FileInputStream fis = new FileInputStream(address);
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = bis.read();
while(c != -1){
baos.write(c);
c = bis.read();
}
bis.close();
byte[] rtn = baos.toByteArray();
baos.close();
return rtn;
}
}
/**
*使用servlet读取图片,网页上使用<img src="PhotoViewServlet.do?photoId=<s:property value="id"/>" />
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String strId = request.getParameter("photoId");
if(null == strId)
return;
int id = Integer.parseInt(strId);
ServletContext sc = request.getSession().getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
PhotoDao dao = (PhotoDaoImpl)wac.getBean("PhotoDao");
Photo photo = dao.findById(id);
response.setContentType("image/"+photo.getContentType());
Blob b = photo.getContent();
ServletOutputStream sos = response.getOutputStream();
try {
InputStream is = b.getBinaryStream();
int bsize = (int)b.length();
byte[] bts = new byte[bsize];
int bread = 0;
while((bread = is.read(bts))!=-1){
sos.write(bts,0,bread);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}