jsp完整图片上传

本文介绍了一个使用JSP实现文件上传功能,并将文件信息存储到数据库中的实例。通过整合DBConn2类进行数据库连接,以及DiskFileUpload类进行文件上传处理,实现了前端文件上传与后端数据库存储的无缝对接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据库设计如图: 
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(); 
    fu.setHeaderEncoding("utf-8"); 
    fu.setSizeMax(1024*1024*20); 
    fu.setSizeThreshold(4096);//设置缓冲区4k 
    fu.setRepositoryPath("c:\\uploade"); 
     
    List file=fu.parseRequest(request); 
    Iterator it=file.iterator(); 
    while(it.hasNext()){ 
    FileItem fi=(FileItem)it.next(); 
    if(!fi.isFormField()){ 
    String file1=fi.getName(); 
    String filename=file1.substring(file1.lastIndexOf("\\")+1); 
    String filecontent=fi.getContentType(); 
    int filesize=(int)fi.getSize(); 
    
    out.write("文件名"+filename+"<br>"); 
    out.write("文件类型:"+filecontent+"<br>"); 
    out.write("文件大小:"+filesize+"<br>"); 
    
    ps=conn.prepareStatement(sql); 
    InputStream is=fi.getInputStream(); 
    System.out.println(filesize); 
    ps.setString(1,filename); 
    
    ps.setBinaryStream(2,is,filesize); 
    ps.executeUpdate(); 
    is.close(); 
    out.println("文件上传成功!!!"); 
  } 
    } 
    }catch(Exception e){ 
    e.printStackTrace(); 
    }finally{ 
    ps.close(); 
    conn.close(); 
    } 
    
    
     %> 
  </body> 
</html> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值