![](images/smilies/emot/em03.gif)
![](images/smilies/emot/em03.gif)
![](images/smilies/emot/em03.gif)
因为是向数据库中写图片了,所以上传的时候我先建了一个临时的文件夹叫TEMP <%@ page import="com.jspsmart.upload.SmartUpload" %> <%@ page import="com.jspsmart.upload.SmartUploadException" %> <%@ page import="com.gallop.util.*" %>
<META http-equiv="Content-Style-Type" content="text/css"> <LINK href="../theme/css.css" rel="stylesheet" type="text/css"> <script language="javascript" src="../include/hetong.js"></script> <style type="text/css"> <!-- .style1 {color: #FF0000} --> </style> <body> <jsp:useBean id="DBO" class="com.gallop.database.ConnectionManager" scope="page"/> <% try{ String URLpath=request.getRequestURL().toString(); String realPath = request.getRealPath(URLpath); int flag = realPath.indexOf("http:"); realPath = realPath.substring(0,flag); realPath = realPath + "Temp"; SmartUpload upLoad = new SmartUpload(); upLoad.initialize(config,request,response); upLoad.setMaxFileSize(20000000); upLoad.setAllowedFilesList("jpg"); upLoad.upload(); com.jspsmart.upload.File file = upLoad.getFiles().getFile(0); //get the upload file's name String fileName = file.getFileName(); //file path String filePath = realPath + "\\" + fileName; //save the upload file file.saveAs("/Temp/" + fileName); String name = upLoad.getRequest().getParameter("name"); java.io.File file1 = new java.io.File(filePath); InputStream in = new FileInputStream(file1); String C02ID=""; C02ID=upLoad.getRequest().getParameter("C02ID"); System.out.println(C02ID); try{ Connection conn=DBO.getConnection(); String sqlstr = "insert into C02FJ(C02ID,FJ) values (?,?)"; PreparedStatement stat = conn.prepareStatement(sqlstr); stat.setString(1,C02ID); stat.setBinaryStream(2,in,(int)file1.length()); int i = stat.executeUpdate(); in.close(); } catch(java.sql.SQLException ee){ out.print(ee.toString()); } boolean b = file1.delete(); if(b){ %> <center>文件上传成功</center> <p> </p> <p> </p> <p align="center"> </p> <p align="center"> </p> <form name="form1" method="post" action="bianhao.jsp"> <div align="center"> <input type="hidden" name="C02ID" value="<%=C02ID%>"> <input type="button" name="Submit" value="还要上传" onClick="fuwei()"> <input type="button" name="Submit" value="结束操作" onClick="closeWindow()"> </div> </form> <p align="center"> <% } else{ %> <center><h1 class="style1">对不起,文件上传失败了,请检查你以前的操作!!!</h1> </center> <div align="center"> <form name="form2" method="post" action=""> <input type="submit" name="Submit" value="返回上一步" onClick="fuwei()"> </form> </div> <% } } catch(Exception e){ e.printStackTrace(System.out); } %> </p>
</body> 你也看到了我使用的是jspsmart的一个上传组件
至于你的看图片的时候,还要分页,我页没有理解好清楚了 我是这样做的,先获取图片的ID,然后把图片读到页面上来了哈 因为关系到流的关系,所以页面上的东西都会被冲掉的了,只会是一张图片了 <%@ page contentType="text/html;charset=GB2312" %> <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <%@ page import="com.gallop.util.*" %> <jsp:useBean id="DBO" class="com.gallop.database.ConnectionManager" scope="page"/>
<HTML> <script language="javascript" src="../include/hetong.js"></script> <BODY BGCOLOR="white"> <p> <% String id=""; String abs=""; String sql=""; Connection con=null; Statement stmt; ResultSet rs=null; int temp=0; id=request.getParameter("id"); abs=request.getParameter("abs"); if(abs!=null || !(abs.equals(""))) temp=Integer.parseInt(abs); con=DBO.getConnection(); stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); sql="select * from C02FJ where C02ID="+id; rs=stmt.executeQuery(sql); rs.absolute(temp); ServletOutputStream out1 = response.getOutputStream(); InputStream in = rs.getBinaryStream("FJ"); int c; while((c = in.read()) != -1){ out1.write(c); } out1.close(); in.close(); out.clearBuffer(); %> </p> <p> </p> <p align="center"><a href="" onClick="fuwei()">返回</a></p>