1、数据结构:
CREATE TABLE [photo] ( [id] [int] IDENTITY (1, 1) NOT NULL , [ordernumber] [nvarchar] (20) [photo] [image] NULL , PRIMARY KEY CLUSTERED ( [id] ) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
2、整体架构:struts2+spring2+hibernate3
3、vo结构:
public class Photo implements Serializable { private int id; private String ordernum; private byte[] photo;
......省get(),set()方法
}
4、数据存取DAO:
public interface PhotoDao { public void savePhoto(Photo photo); public Photo getPhoto(String ordername); }
5、上传Action
public class UpLoadPhoto extends ActionSupport { private PhotoDao pdi;//pdi为通过spring注入的Dao实例,用于把Photo对象存入数据库 private String filename; private File upload; private InputStream inputStream; public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String upload() { if(filename!=null && !"".equals(filename)) { Photo photo=new Photo(); photo.setOrdernum(filename); photo.setPhoto(getBytesFromFile(upload)); System.out.println(photo.getOrdernum()+photo.getPhoto()); pdi.savePhoto(photo); } return SUCCESS; } public static byte[] getBytesFromFile( File file ) { long length = file.length(); byte[] bytes = new byte[(int)length]; try { InputStream is = new FileInputStream(file); // Read in the bytes int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file "+file.getName()); } // Close the input stream and return bytes is.close(); } catch(Exception e ) { throw new RuntimeException(e); } return bytes; } } */ public PhotoDao getPdi() { return pdi; } public void setPdi(PhotoDao pdi) { this.pdi = pdi; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; }
}
6、jsp上传
<s:form action="uploadfile" method="post" enctype="multipart/form-data"> <s:file name="upload"/> 文件名:<s:textfield name="filename"/> <s:submit value="图片上传"></s:submit> </s:form>
7、用于从数居库显示图片的Action
public class ShowPhoto extends ActionSupport { private PhotoDao pdi; private String filename; public String getFilename() { return filename; }
public void setFilename(String filename) { this.filename = filename; }
public PhotoDao getPdi() { return pdi; }
public void setPdi(PhotoDao pdi) { this.pdi = pdi; }
@Override public String execute() throws Exception { // TODO Auto-generated method stub return SUCCESS; } public InputStream getInputStream() { Photo photo=new Photo(); try { photo=pdi.getPhoto(filename); } catch (RuntimeException e) { e.printStackTrace(); } if(photo==null||photo.getPhoto()==null) { return null; } else { System.out.println(photo.getOrdernum()); System.out.println(photo.getPhoto()); System.out.println("Image_Lenghth:"+photo.getPhoto().length); ByteArrayInputStream bais; try { bais = new ByteArrayInputStream(photo.getPhoto()); return bais; } catch (RuntimeException e) { e.printStackTrace(); return null; } } }
}
8、jsp中显示图片
<s:url id="image" action="getPhoto?filename=ck" /><!--ck为数据库中查找图片的关键字(上传时设的文件名)--> <img src="<s:property value="#image"/>"/>
9、struts.xml配置
<action name="uploadfile" class="uploadfile" method="upload"><!-- 指定逻辑方法method --> <result name="success">/success.jsp</result> </action> <action name="getPhoto" class="getPhoto" > <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="s.jpg"</param><!--指定默认文件名--> <param name="bufferSize">1024</param>><!--指定缓存大小--> </result> </action>
发表于 @ 2009年09月18日 16:15:00 转