<%@ page language="java" import="java.sql.*,java.lang.*,com.jspsmart.upload.*"%> <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /> <% // Variables int count=0; // Connect to the database Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test"); String inValID; inValID = request.getParameter("idval"); %> <%=inValID %> <% // SQL Request Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE); // ResultSet rs = stmt.executeQuery("SELECT * FROM journals WHERE ID = 1"); ResultSet rs = stmt.executeQuery("SELECT * FROM journals WHERE ID = " + inValID); // if the resultset is not null if (rs.next()){ // Initialization mySmartUpload.initialize(pageContext); // Upload mySmartUpload.upload(); // upload file in the DB if this file is not missing if (!mySmartUpload.getFiles().getFile(0).isMissing()){
try {
java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames(); // Retreive parameters while (e.hasMoreElements()) { String key = (String)e.nextElement(); String[] values = mySmartUpload.getRequest().getParameterValues(key); // Add the current file in the DB field rs.updateString("FILE_NAME",mySmartUpload.getFiles().getFile(0).getFileName()); mySmartUpload.getFiles().getFile(0).fileToField(rs,"BODY"); // Update rs.updateRow(); count++; } } catch(Exception e) { out.println("An error occurs : " + e.toString()); } } } rs.close(); stmt.close(); con.close(); %>
就是用 smartupload 到网上找找,有大把的!!<%@ page contentType="text/html;charset=gb2312" %> <%@ page language="java" import="com.jspsmart.upload.*"%> <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /> <jsp:useBean id="inewBean" scope="page" class="hitachi.news.inewBean" /> <HTML> <BODY BGCOLOR="white"> <H1>jspSmartUpload : Sample 3</H1> <HR> <%// Variables int count=0; // Initialization mySmartUpload.initialize(pageContext); // Only allow txt or htm files //mySmartUpload.setAllowedFilesList("htm,html,txt,,"); // DeniedFilesList can also be used : // mySmartUpload.setDeniedFilesList("exe,bat,jsp"); // Deny physical path // mySmartUpload.setDenyPhysicalPath(true); // Only allow files smaller than 50000 bytes // mySmartUpload.setMaxFileSize(50000); // Deny upload if the total fila size is greater than 200000 bytes // mySmartUpload.setTotalMaxFileSize(200000); // Upload mySmartUpload.upload(); // Save the files with their original names in a virtual path of the web server try { count = mySmartUpload.save("/pic/upload/news", mySmartUpload.SAVE_VIRTUAL); String title=mySmartUpload.getRequest().getParameter("title"); String content=mySmartUpload.getRequest().getParameter("content"); String picture=mySmartUpload.getFiles().getFile(0).getFileName();inewBean.insert_news(title,content,picture); } catch (Exception e){ out.println("<b>Wrong selection : </b>" + e.toString()); } // Display the number of files uploaded out.println(count + " file(s) uploaded."); %> </BODY> </HTML>
<%@ page contentType="text/html; charset=gb2312" %>
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="cn" scope="page" class="bbs.cn" />
<jsp:useBean id="mycheck" scope="page" class="bbs.check" />
<jsp:useBean id="xiruo" scope="page" class="bbs.xiruo" />
<html>
<head>
<title>jsp论坛--文件上传</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK href="inc/css.css" rel=stylesheet>
</head>
<body bgcolor="#f2f8ff" alink="#333333" vlink="#333333" link="#333333" topmargin=10 leftmargin=10 onkeydown="if(event.keyCode==13 && event.ctrlKey)messager.submit()">
<%
session.setMaxInactiveInterval(20*60);
String user=(String)session.getAttribute("username");
String action=(String)request.getParameter("action");
java.text.SimpleDateFormat dformat=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String errmessage="";
if(user==null||user.trim()=="")
errmessage=errmessage+"<li>您没有<a href=\"login.jsp\" target=\"_blank\">登录</a></li>";
int count=0;
String mypath=request.getRealPath("BBS")+"\\information\\";
myUpload.initialize(pageContext);
//文件上传
myUpload.upload();
//上传的情况统计
//***************************************************//判断提交的表单数据
String content="";
String url[]=new String[myUpload.getFiles().getCount()];
boolean y=true;
boolean x=false;
String myfilename="";
for (int i=0;i<myUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = myUpload.getFiles().getFile(i);
//获得文本的内容
if(!myFile.isMissing()) {
y=false;
content=myUpload.getRequest().getParameter("text"+(i+1));
java.io.File file = new java.io.File(mypath+content);
if(file.exists()) {
x=true;
myfilename=myfilename+" "+content;
}
}
}
if(y)
errmessage=errmessage+"<li>你没有选择任何要上传的文件</li>";
if(content.trim()=="")
errmessage=errmessage+"<li>你必须给需要上传的文件命名</li>";
if(x)
errmessage=errmessage+"<li>文件 "+myfilename+" 已经存在,请重新命名</li>";
//*****************************************************
if(errmessage.trim()=="") {
for (int i=0;i<myUpload.getFiles().getCount();i++){
com.jspsmart.upload.File myFile = myUpload.getFiles().getFile(i);
//获得文本的内容
content=myUpload.getRequest().getParameter("text"+(i+1));
//显示文本的内容
//out.println(content);
if (!myFile.isMissing()) {
myFile.saveAs(mypath + content);//myFile.getFileName());
cn.executeUpdate("insert into [file](filename,username) values('"+content+"','"+user+"')");
url[i]=content;
//out.println("FieldName = " + myFile.getFieldName() + "<BR>");
//out.println("Size = " + myFile.getSize() + "<BR>");
//out.println("FileName = " + myFile.getFileName() + "<BR>");
//out.println("FileExt = " + myFile.getFileExt() + "<BR>");
//out.println("FilePathName = " + myFile.getFilePathName() + "<BR>");
//out.println("ContentType = " + myFile.getContentType() + "<BR>");
//out.println("ContentDisp = " + myFile.getContentDisp() + "<BR>");
//out.println("TypeMIME = " + myFile.getTypeMIME() + "<BR>");
//out.println("SubTypeMIME = " + myFile.getSubTypeMIME() + "<BR>");
count ++;
}
}
//out.println("<BR>" + myUpload.getFiles().getCount() + " files could be uploaded.<BR>");
//out.println(count + " file(s) uploaded.");
%>
<table cellpadding=0 cellspacing=0 border=0 width=95% bgcolor=#777777 align=center>
<tr>
<td>
<table cellpadding=3 cellspacing=1 border=0 width=100%>
<tr>
<td bgcolor=#e8f4ff align=center><font face="宋体" color=#333333><b>文件上传成功!</b>(可以在论坛中把地址用HTML代码贴出来)</font></td>
</tr>
<tr bgcolor="#f2f8ff">
<td valign=middle align=center> <a href="upload.jsp"><img src="PIC/upload.gif" border=0 alt="上传文件" width="50" height="28"></a><br/><br/>
上传文件资料:<br/><br/>
<%
for(int i=0;i<url.length;i++) {
if(url[i]!=null&&url[i].trim()!="")
out.println("<a href='http://172.18.24.134:9999/BBS/information/"+url[i]+"' target='_blank'>"+url[i]+"</a><br/><br/>");
}
%>
</td>
</tr>
<tr bgcolor="#e8f4ff">
<td valign=middle align=center bgcolor="#e8f4ff"> <b><a href="javascript:history.back();"><<返回上一页</a></b></td>
</tr>
</table>
</td>
</tr>
</table>
<%
} else {
%>
<br/>
<table cellpadding=0 cellspacing=0 border=0 width=95% bgcolor=#0099cc align=center>
<tr>
<td>
<table cellpadding=3 cellspacing=1 border=0 width=100%>
<tr align="center">
<td width="100%" bgcolor=#99ccff><font color="#000000"><b>论坛错误信息</b></font></td>
</tr>
<tr>
<td width="100%" bgcolor=#f2f8ff><font color="#000000"><b>产生错误的可能原因:</b><br/><br/>
<li>您是否仔细阅读了<a href="help.jsp" target="_blank"><font color="#000000">帮助文件</font></a></li><%=errmessage%></font>
</td>
</tr>
<tr align="center">
<td width="100%" bgcolor=#99ccff>
<a href="javascript:history.go(-1)"><font color="#000000"> << 返回上一页</font></a>
</td>
</tr>
</table> </td></tr></table>
<%
}
%>
<br/>
<br/>
<%@include file="inc/bottom.jsp"%>
</BODY></HTML>
<%@page contentType="text/html;charset=gb2312"%>
<%@ page language="java"%><%@ page import="java.io.*"%> <%@ page import="java.util.*"%> <%@ page import="javax.servlet.*"%> <%@ page import="javax.servlet.http.*"%> <%@ page import="java.lang.*"%><HTML>
<BODY BGCOLOR="white"><H1>我的upload</H1>
<HR><%
String fname=request.getParameter("image");
String id=request.getParameter("id");
if(id.trim()!=null&&!id.trim().equals(""))
{
int picid = Integer.parseInt(id.trim());}
%>
<p>数据库picid是:<%=picid%></p>
<p>图片文件是:<%=fname%></p>
<%
fname=new String(fname.trim().getBytes("ISO8859_1"), "GBK");
String url=new String();
url="jdbc:microsoft:sqlserver://192.168.0.202:1433";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Nonnection con=DriverManager.getConnection(url,"sa","sa");
con.setCatalog( "mybase");
String sqlin="insert into PICS(PICID,IMAGE) values (?,?)";
File file = new File(fname);
long l1=file.length();
int l2=(int)l1;
try
{
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = con.prepareStatement(sqlin);
ps.setInt(1,picid);
ps.setBinaryStream(2,fis,l2);
ps.executeUpdate();
ps.close();
fis.close();
}
catch(SQLException e)
{
System.out.println(e);
} %></BODY>
</HTML>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
// Variables
int count=0;
// Connect to the database
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test");
String inValID;
inValID = request.getParameter("idval");
%>
<%=inValID %>
<%
// SQL Request
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
// ResultSet rs = stmt.executeQuery("SELECT * FROM journals WHERE ID = 1");
ResultSet rs = stmt.executeQuery("SELECT * FROM journals WHERE ID = " + inValID);
// if the resultset is not null
if (rs.next()){
// Initialization
mySmartUpload.initialize(pageContext);
// Upload
mySmartUpload.upload();
// upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){
try {
java.util.Enumeration e = mySmartUpload.getRequest().getParameterNames();
// Retreive parameters
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String[] values = mySmartUpload.getRequest().getParameterValues(key);
// Add the current file in the DB field
rs.updateString("FILE_NAME",mySmartUpload.getFiles().getFile(0).getFileName());
mySmartUpload.getFiles().getFile(0).fileToField(rs,"BODY");
// Update
rs.updateRow();
count++;
}
} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}
}
}
rs.close();
stmt.close();
con.close();
%>
<%@ page language="java" import="com.jspsmart.upload.*"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<jsp:useBean id="inewBean" scope="page" class="hitachi.news.inewBean" />
<HTML>
<BODY BGCOLOR="white">
<H1>jspSmartUpload : Sample 3</H1>
<HR>
<%// Variables
int count=0;
// Initialization
mySmartUpload.initialize(pageContext);
// Only allow txt or htm files
//mySmartUpload.setAllowedFilesList("htm,html,txt,,");
// DeniedFilesList can also be used :
// mySmartUpload.setDeniedFilesList("exe,bat,jsp");
// Deny physical path
// mySmartUpload.setDenyPhysicalPath(true);
// Only allow files smaller than 50000 bytes
// mySmartUpload.setMaxFileSize(50000);
// Deny upload if the total fila size is greater than 200000 bytes
// mySmartUpload.setTotalMaxFileSize(200000);
// Upload
mySmartUpload.upload();
// Save the files with their original names in a virtual path of the web server
try {
count = mySmartUpload.save("/pic/upload/news", mySmartUpload.SAVE_VIRTUAL);
String title=mySmartUpload.getRequest().getParameter("title");
String content=mySmartUpload.getRequest().getParameter("content");
String picture=mySmartUpload.getFiles().getFile(0).getFileName();inewBean.insert_news(title,content,picture);
} catch (Exception e){
out.println("<b>Wrong selection : </b>" + e.toString());
}
// Display the number of files uploaded
out.println(count + " file(s) uploaded."); %>
</BODY>
</HTML>
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*,java.io.*"%>
<html>
<head>
<title>
saveMovie
</title>
</head>
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
}
return null;
}
%>
<%!
public static String returnToBr(String sStr)
{
if(sStr==null||sStr.equals(""))
{
return sStr;
}
StringBuffer sTmp=new StringBuffer();
int i=0;
while(i<=sStr.length()-1)
{
if(sStr.charAt(i)=='\n'||sStr.charAt(i)=='\r')
sTmp=sTmp.append("<br>");
else if(sStr.charAt(i)==' ')
sTmp=sTmp.append(" ");
else sTmp=sTmp.append(sStr.substring(i,i+1));
i++;
}
String s1=sTmp.toString();
return s1;
}
%>
<%!String title,filmLevel,actor,movieType,image1,movieAdd,content;
String pubTime,pubArea,filesNum;
String movKind;
%>
<%
//主演为非必填项目(actor)
title=request.getParameter("title");
String movKind=request.getParameter("moviekind");
out.println(movKind);
pubArea=request.getParameter("where"); pubTime=request.getParameter("pubtime");
filmLevel=request.getParameter("filmlevel");
actor=request.getParameter("actor");
out.println(actor);
movieType=request.getParameter("movieType");//rm or mov or dat
image1=request.getParameter("image1");
filesNum=request.getParameter("filesnum");
movieAdd=request.getParameter("movie1");
content=request.getParameter("content");
//
title=returnToBr(getStr(title));
actor=returnToBr(getStr(actor));
movKind=getStr(movKind);
pubArea=getStr(pubArea);
pubTime=getStr(pubTime);
filmLevel=getStr(filmLevel);
movieType=getStr(movieType);
filesNum=getStr(filesNum);
movieAdd=returnToBr(getStr(movieAdd));
content=returnToBr(getStr(content)); //
java.util.Calendar upTime=Calendar.getInstance();
String uploadTime=String.valueOf(upTime.getTime());
//
if(actor.trim().equals(""))
{
actor=null;
}
File file1=new File(image1);
FileInputStream fis=new FileInputStream(file1);
String fileName=file1.getName();
String table=movKind;//电影类型
String sql="set textsize 2147483647;insert into "+table+"(title,actor,pubtime,pubarea,introimage,";
sql=sql+"content,filesnum,reqtimes,uploadtime,faddress,reclevel)values('";
sql=sql+title+"','"+actor+"','"+pubTime+"','"+pubArea+"',"+"?";
sql=sql+",'"+content+"','"+filesNum+"','"+0+"','"+uploadTime;
sql=sql+"','"+movieAdd+"','"+filmLevel+"')";
out.println(sql);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:myDB");
PreparedStatement ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_UPDATABLE);
ps.setBinaryStream(1,fis,fis.available());
ps.executeUpdate();
ps.close();
fis.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
System.out.println("ok!");
%>
<body>
</body>
</html>
[Microsoft][ODBC SQL Server Driver][SQL Server]将截断字符串或二进制数据。
但是数据库里面的image字段的值为binary,这是不是意味着图片没有存到数据库,
这是怎么回事呢?要是需要,我贴出所有源代码!