我是把数据库读取出来的图像先生成临时文件放到服务器的临时目录
进行查看 当查看完关闭页面时再把相应的临时图像删除
附件为相应样式
下列为参考代码。。。
---jsp
<center>
<div style="font-size: 12px;width: 580px;height: 330px;text-align: left;margin-top: 15px;">
<b>查看扫面图像:</b>
<table border="0" cellpadding="0" cellspacing="0" style="font-size: 12px;width: 100%;border-collapse: collapse;">
<tr>
<td bgcolor="#F4F7F7" class="RecordRow" style="height: 25px;">
<a id="scansp" href="javascript:void(0);" style="color:#999999;">首票</a>
<a id="scanqp" href="javascript:void(0);" style="color:#999999;">上票</a>
<a id="scanxp" href="javascript:void(0);" style="color:#999999;">下票</a>
<a id="scanmp" href="javascript:void(0);" style="color:#999999;">末票</a>
第<label id="curscan">0</label>票/共<label id="sumscan">0</label>票
</td>
</tr>
<tr>
<td align="center" class="RecordRow" style="height: 300px;">
<div id="scan">(图像)</div>
</td>
</tr>
</table>
</div>
</center>
<script type="text/javascript">
<!--
/** liweichao */
var current=0;//当前票
var sum=0;//总票
var scans="${inSupportCommendForm.scans}";
if(scans==null||scans=="")
scans=",";
var scanArray=scans.substring(0,scans.length-1).split(",");
sum=scanArray.length;
if(sum>0&&scanArray[0].length>0){
showScan(0,1);
document.getElementById("sumscan").innerHTML=sum;
}
/**
*控制显示票
*前一票:cut=1, 首票:cut=-1
*后一票:app=1, 末票:app=-1
*/
function showScan(cut,app){
//cut=1前一票
if(cut==1&¤t>1)
current--;
//app=1后一票
if(app==1&¤t<sum)
current++;
//cut=-1首票
if(cut==-1)
current=1;
//app=-1末票
if(app==-1)
current=sum;
//alert(cut+"--"+app+"==="+scanArray[current-1]);
//用img显示服务器临时目录的图像
var strImg="<img src=\"/servlet/DisplayOleContent?filename="+scanArray[current-1]+"\" height=300 width=578 border=0 >";
document.getElementById("curscan").innerHTML=current;
document.getElementById("scan").innerHTML=strImg;
if(current==1){
document.getElementById("scansp").href="javascript:void(0);";
document.getElementById("scansp").style.color="#999999";
document.getElementById("scanqp").href="javascript:void(0);";
document.getElementById("scanqp").style.color="#999999";
}else{
document.getElementById("scansp").href="javascript:showScan(-1,0);";
document.getElementById("scansp").style.color="";
document.getElementById("scanqp").href="javascript:showScan(1,0);";
document.getElementById("scanqp").style.color="";
}
if(current==sum){
document.getElementById("scanxp").href="javascript:void(0);";
document.getElementById("scanxp").style.color="#999999";
document.getElementById("scanmp").href="javascript:void(0);";
document.getElementById("scanmp").style.color="#999999";
}else{
document.getElementById("scanxp").href="javascript:showScan(0,1);";
document.getElementById("scanxp").style.color="";
document.getElementById("scanmp").href="javascript:showScan(0,-1);";
document.getElementById("scanmp").style.color="";
}
}
//关闭窗体时返回要删除的临时图像名称
window.οnunlοad=function(){
if(!window.closed)
{
if(sum>0&&scanArray[0].length>0){
var returnScan=new Object();
returnScan.scanArray=scans;
//returnValue=returnScan;
//调用删除方法
}
}
}
//-->
</script>
--业务类
1.删除
//是否删除生成的临时图像scanFlag=1是
String scanFlag=(String) this.getFormHM().get("scanFlag");
if(scanFlag!=null&&scanFlag.equals("1")){
String scanArray=(String) this.getFormHM().get("scanArray");
String separator = System.getProperty("file.separator");//分隔符
String tmpdir = System.getProperty("java.io.tmpdir"); //临时目录
if(scanArray!=null&&scanArray.length()>0){
String[] sc=scanArray.split(",");
for (int j = 0; j < sc.length; j++) {
if(sc[j]!=null&&sc[j].length()>0){
File file = new File(tmpdir + separator + sc[j]);
//删除文件
if (file.isFile())
file.delete();
}
}
}
}
2.读取
import java.io.File;
import java.io.InputStream;
import java.sql.SQLException;
import com.hjsj.hrms.servlet.PhotoFileDeleter;
import com.hrms.frame.dao.ContentDAO;
import com.hrms.struts.exception.GeneralException;
import com.hrms.struts.facade.transaction.IBusiness;
/**
* @author liweichao
*
*/
public class ShowScanImagesTrans extends IBusiness{
/**查看扫面图像 和 删除服务器临时图像*/
public void execute() throws GeneralException {
StringBuffer scans=new StringBuffer();
File tempFile = null;
String filename="";
createTempDir();//创建临时目录
ContentDAO dao =new ContentDAO(this.getFrameconn());
StringBuffer strsql = new StringBuffer();
//ext图像后缀名 如:.jpg/.png/.bmp/gif 等
//Ole Image
strsql.append("select ext,Ole from UsrA00 where Flag='P'");
this.frecset=dao.search(strsql.toString());
while (this.frecset.next()) {
java.io.FileOutputStream fout = null;
//createTempFile(name,postfix,path);
tempFile = File.createTempFile("scan-", this.frecset.getString("ext"),
new File(System.getProperty("java.io.tmpdir")));
InputStream in = this.frecset.getBinaryStream("Ole");
fout = new java.io.FileOutputStream(tempFile);
int len;
if(in!=null){
byte buf[] = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1) {
fout.write(buf, 0, len);
}
fout.close();
filename= tempFile.getName();
//System.out.println("filename="+filename);
scans.append(filename+",");
}
this.getFormHM().put("scans", scans.toString());
}
/** 创建临时文件夹 */
public static void createTempDir() {
String tempDirName = System.getProperty("java.io.tmpdir");
if (tempDirName == null) {
throw new RuntimeException("Temporary directory system property (java.io.tmpdir) is null.");
}
// create the temporary directory if it doesn't exist
File tempDir = new File(tempDirName);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
}
---Servlet
public class DisplayOleContent extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String filename = request.getParameter("filename");
if (filename == null) {
throw new ServletException("Parameter 'filename' must be supplied");
}
//考虑到中文的文件名称
filename=SafeCode.decode(filename);
// Check the file exists
filename=new String(filename.getBytes("GB2312"),"GBK");
File file = new File(System.getProperty("java.io.tmpdir"), filename);
if (!file.exists()) {
//return;
throw new ServletException("File '" + file.getAbsolutePath() + "' does not exist");
}
/**显示对象文件*/
ServletUtilities.sendTempFile(file,response);
}
/**
*
*/
public DisplayOleContent() {
super();
}
}