jsp图片上传? 用jsp+bean的方式做的程序需要有一个功能,就是把一个需要上传的图片放到数据库中(不是存放路径,而是二进制文件)最好有个例子,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用 apache的commons-fileupload,可以看看他带的例子. 直接数数据流读到数据库中,数据库用blob接受。注意设置大小。 /---------------------将图片存入数据库----------------------------JavaBean: Session session = HibernateSessionFactory.currentSession(); Transaction tx = session.beginTransaction(); String path = "d:/China.jpg"; FileInputStream str = new FileInputStream(path); PreparedStatement pstmt = session.connection().prepareStatement("insert into saveimage(image) values(?)"); pstmt.setBinaryStream(1,str,str.available()); pstmt.execute(); pstmt.close(); str.close(); tx.commit(); HibernateSessionFactory.closeSession();//---------------------将图片显示到网页上----------------------------Servlet: Session sess = HibernateSessionFactory.currentSession(); Transaction tx = sess.beginTransaction(); String sql = "select * from saveimage where id = "+getImageForm.get("id"); PreparedStatement pstmt = sess.connection().prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); rs.next(); ServletOutputStream sout = response.getOutputStream(); InputStream in = rs.getBinaryStream("image"); byte b[] = new byte[0x7a120]; for(int i = in.read(b); i != -1;){ sout.write(b); in.read(b); } sout.flush(); sout.close(); rs.close(); tx.commit(); HibernateSessionFactory.closeSession(); <%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.sql.*"%><%@page import="java.io.*" %><%@page import="java.util.*" %><%@ page import = "database_connection.sun_wap_refresh_conn" %><jsp:useBean id="myconn1" class="database_connection.sun_wap_refresh_conn" scope="session"/><jsp:setProperty name="myconn1" property="*"/><%//上一行的 jsp:useBean id="myconn11" id 不能有重复myconn1.getConn();CallableStatement cstmt=null;Statement stmt=myconn1.conn.createStatement();String url="";String flag="";String procedure="";String id="";url=request.getParameter("url");flag=request.getParameter("flag");id=request.getParameter("id");ResultSet rs=null;if(flag!=null){if(flag.compareTo("add")==0){BufferedInputStream data;BufferedInputStream is;int FormSize; byte[] bb;byte t; String image="";data=new BufferedInputStream(request.getInputStream());FormSize=request.getContentLength(); int i=0,start1=0,start2=0,_start1=0,_start2=0; String temp=""; String _temp="";String boundary=""; is=new BufferedInputStream(data);bb=new byte[FormSize]; while (i<FormSize) { t=(byte)is.read(); bb[i]=t; i++; } is.close(); temp=new String(bb,"ISO8859_1"); _temp=temp;_start1=_temp.indexOf("boundary=");_temp=_temp.substring(_start1+9); _start2=_temp.indexOf("\r\n");boundary=_temp.substring(0,_start2); _start1=_temp.indexOf(boundary);_temp=_temp.substring(_start1+boundary.length());_start1=_temp.indexOf(boundary);image=_temp.substring(0,_start1);_temp=_temp.substring(_start1+boundary.length());_start1=image.indexOf("\r\n\r\n");_start2=image.lastIndexOf("\r\n");image=image.substring(_start1+4,_start2);//out.println("username:"+username+"<br>alt_name:"+alt_name+"<br>level:"+level+"<br>image:"+image);procedure="{call sun_insert_ad_pic_image(?,?)}";cstmt = myconn1.conn.prepareCall(procedure);cstmt.setString(1,url);cstmt.setBytes(2,image.getBytes("ISO8859_1"));cstmt.executeUpdate();}if(flag.compareTo("del")==0){procedure="{call sun_delete_ad_pic_image(?)}";cstmt = myconn1.conn.prepareCall(procedure);cstmt.setInt(1,Integer.parseInt(id));cstmt.executeUpdate();}}procedure="{call sun_select_ad_pic_image(?)}";cstmt = myconn1.conn.prepareCall(procedure);cstmt.setString(1,url);rs=cstmt.executeQuery();%><script language="javascript">function del(id){location.replace("edit_keyword_pic.jsp?flag=del&url=<%=url%>&id="+id);}</script> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#B6EF07"> <tr> <td width="20%" bgcolor="#FFFFFF">ID</td> <td width="60%" bgcolor="#FFFFFF">图片</td> <td width="20%" bgcolor="#FFFFFF">操作</td> </tr><%while(rs.next()){ String _id=rs.getString("id"); %><tr><td width="20%" bgcolor="#FFFFFF"><%=_id%></td><td width="60%" bgcolor="#FFFFFF"><img src="wc1.jsp?id=<%=_id%>"></td><td width="20%" bgcolor="#FFFFFF"><input type="button" onclick="del('<%=_id%>')" value="删除"></td></tr><%}%> </table> <form name="form1" action="edit_keyword_pic.jsp?flag=add&url=<%=url%>" method="post" enctype="multipart/form-data"><input type=file name="image"><input type="submit" value="上传"> </form> http://wenwen.soso.com/z/q151212357.htm 每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分 这里发言,表示您接受了CSDN社区的用户行为准则。 请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。 转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。 求一个JMeter写的压力测试例子 网站的多线程问题 如何將做好的電腦網頁可以在手機上觀看 急救一个集合如何在iReport上显示啊? 关于博客系统 JSP 用户登陆判断问题,望高手指点... 目前流行的Tags功能是如何实现的?谁能说说原理 怎样把native2ascii 转化后的文件还原过来? 我找不到TOMCAT,我看不懂英语,谁能给我传一个呀?或者给我一个准确下载地址也行,谢谢 请问关于JavaBean,Servlet的问题 struts2 验证问题 A站点与B站点如何以http的方式传输字节流信息
Session session = HibernateSessionFactory.currentSession();
Transaction tx = session.beginTransaction();
String path = "d:/China.jpg";
FileInputStream str = new FileInputStream(path);
PreparedStatement pstmt = session.connection().prepareStatement("insert into saveimage(image) values(?)");
pstmt.setBinaryStream(1,str,str.available());
pstmt.execute();
pstmt.close();
str.close();
tx.commit();
HibernateSessionFactory.closeSession();//---------------------将图片显示到网页上----------------------------
Servlet:
Session sess = HibernateSessionFactory.currentSession();
Transaction tx = sess.beginTransaction();
String sql = "select * from saveimage where id = "+getImageForm.get("id");
PreparedStatement pstmt = sess.connection().prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
rs.next();
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream("image");
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;){
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
rs.close();
tx.commit();
HibernateSessionFactory.closeSession();
<%@ page import="java.sql.*"%>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@ page import = "database_connection.sun_wap_refresh_conn" %>
<jsp:useBean id="myconn1" class="database_connection.sun_wap_refresh_conn" scope="session"/><jsp:setProperty name="myconn1" property="*"/>
<%//上一行的 jsp:useBean id="myconn11" id 不能有重复myconn1.getConn();
CallableStatement cstmt=null;
Statement stmt=myconn1.conn.createStatement();
String url="";
String flag="";
String procedure="";
String id="";
url=request.getParameter("url");
flag=request.getParameter("flag");
id=request.getParameter("id");ResultSet rs=null;if(flag!=null){
if(flag.compareTo("add")==0){BufferedInputStream data;
BufferedInputStream is;
int FormSize;
byte[] bb;
byte t; String image="";
data=new BufferedInputStream(request.getInputStream());
FormSize=request.getContentLength(); int i=0,start1=0,start2=0,_start1=0,_start2=0;
String temp="";
String _temp="";
String boundary="";
is=new BufferedInputStream(data);
bb=new byte[FormSize];
while (i<FormSize)
{
t=(byte)is.read();
bb[i]=t;
i++;
}
is.close();
temp=new String(bb,"ISO8859_1");
_temp=temp;_start1=_temp.indexOf("boundary=");
_temp=_temp.substring(_start1+9);
_start2=_temp.indexOf("\r\n");
boundary=_temp.substring(0,_start2); _start1=_temp.indexOf(boundary);
_temp=_temp.substring(_start1+boundary.length());
_start1=_temp.indexOf(boundary);
image=_temp.substring(0,_start1);
_temp=_temp.substring(_start1+boundary.length());
_start1=image.indexOf("\r\n\r\n");
_start2=image.lastIndexOf("\r\n");
image=image.substring(_start1+4,_start2);//out.println("username:"+username+"<br>alt_name:"+alt_name+"<br>level:"+level+"<br>image:"+image);procedure="{call sun_insert_ad_pic_image(?,?)}";
cstmt = myconn1.conn.prepareCall(procedure);
cstmt.setString(1,url);
cstmt.setBytes(2,image.getBytes("ISO8859_1"));
cstmt.executeUpdate();
}
if(flag.compareTo("del")==0){
procedure="{call sun_delete_ad_pic_image(?)}";
cstmt = myconn1.conn.prepareCall(procedure);
cstmt.setInt(1,Integer.parseInt(id));
cstmt.executeUpdate();
}}procedure="{call sun_select_ad_pic_image(?)}";
cstmt = myconn1.conn.prepareCall(procedure);
cstmt.setString(1,url);
rs=cstmt.executeQuery();%>
<script language="javascript">
function del(id){
location.replace("edit_keyword_pic.jsp?flag=del&url=<%=url%>&id="+id);
}
</script> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#B6EF07">
<tr>
<td width="20%" bgcolor="#FFFFFF">ID</td>
<td width="60%" bgcolor="#FFFFFF">图片</td>
<td width="20%" bgcolor="#FFFFFF">操作</td>
</tr>
<%while(rs.next()){
String _id=rs.getString("id");
%>
<tr>
<td width="20%" bgcolor="#FFFFFF"><%=_id%></td>
<td width="60%" bgcolor="#FFFFFF"><img src="wc1.jsp?id=<%=_id%>"></td>
<td width="20%" bgcolor="#FFFFFF"><input type="button" onclick="del('<%=_id%>')" value="删除"></td>
</tr>
<%}%>
</table> <form name="form1" action="edit_keyword_pic.jsp?flag=add&url=<%=url%>" method="post" enctype="multipart/form-data">
<input type=file name="image"><input type="submit" value="上传">
</form>
每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分
这里发言,表示您接受了CSDN社区的用户行为准则。
请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。