日期:2014-05-16  浏览次数:20394 次

jsp完整图片上传
数据库设计如图:
DBConn2.java


package com.db;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConn2 {

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";
String user="sa";
String pwd="123456";
Connection conn=null;
public Connection get()
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
    conn=DriverManager.getConnection(url,user,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}

load.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'load.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
    <form action="fileupload.jsp" enctype="multipart/form-data" method="post">
    <input type="file" name="filename"/>
    <input type="submit" value="上传"/>
    </form>
  </body>
</html>

fileupload.jsp

<%@ page language="java" import="java.util.*,com.db.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*" %>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItem"%>
<%@page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.io.InputStream"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'fileupload.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
 
  <body>
    <%
    PreparedStatement ps=null;
    DBConn2 db=new DBConn2();
    Connection conn=db.get();
  try{
    String sql="insert into test.dbo.file_tbl(filename,filecontent,createdate) values(?,?,GETDATE())";
   
    DiskFileUpload  fu=new DiskFileUpload()