您好,欢迎光临本网站![请登录][注册会员]  
文件名称: java用servlet实现文件上传到数据库
  所属分类: Java
  开发工具:
  文件大小: 13kb
  下载次数: 0
  上传时间: 2011-03-23
  提 供 者: Java_T******
 详细说明: java用servlet实现文件上传到数据库 2009-03-19 14:26 index.jsp <%@ page language="java" contentType="text/html;charset=gb2312"%> 文件上传
web.xml upload servletUpload upload /servletUpload index.jsp lib:jspupload.jar java: import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import com.jspsmart.upload.*; public class servletUpload extends HttpServlet { private ServletConfig config; /** * 初始化Servlet */ final public void init(ServletConfig config) throws ServletException { this.config = config; } /** * 处理GET请求 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("

jspSmartUpload : Servlet Sample

"); out.println("

"); out.println("The method of the HTML form must be POST."); out.println(""); out.println(""); } /** * 响应POST请求 */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("

Upload File

"); out.println("
"); // 变量定义 SmartUpload mySmartUpload = new SmartUpload(); String myFileName = ""; //myFileName为带文件后缀 String filename = ""; //filename为不带文件后缀 String fileext = ""; int filesize = 0; //连接数据库 Connection conn = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=TestUpload;User=sa;Password=sa"); System.out.println("Success to connect database"); } catch (Exception e) { System.out.println("Fail to connect database"); e.printStackTrace(); } try { // 初始化 mySmartUpload.initialize(config,request,response); // 上载 mySmartUpload.upload(); com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(1); if (!myFile.isMissing()) { int t1; myFileName = myFile.getFileName(); filename = myFileName.substring(0,myFileName.lastIndexOf('.')); t1 = myFileName.lastIndexOf('.')+1; fileext = myFileName.substring(t1,myFileName.length()); filesize = myFile.getSize(); } String trace="c:/"+myFileName; myFile.saveAs(trace,mySmartUpload.SAVE_PHYSICAL); java.io.File file = new java.io.File(trace); java.io.FileInputStream fis = new java.io.FileInputStream(file); String sql="insert into fileinfo values (?,?,?,?)"; PreparedStatement pstmt=conn.prepareStatement(sql); pstmt.setString(1,filename); pstmt.setString(2,fileext); pstmt.setInt(3,filesize); pstmt.setBinaryStream(4,fis,(int)file.length()); pstmt.executeUpdate(); //删除上传的原始文件 java.io.File origFile = new java.io.File("c:\\"+myFileName); if (origFile.isFile() && origFile.exists()) { origFile.delete(); } // 显示处理结果 out.println(filename + "." + fileext + " has been uploaded."); } catch (Exception e){ out.println("Unable to upload the file.
"); out.println("Error : " + e.toString()); } out.println(""); out.println(""); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 输入关键字,在本站1000多万海量源码库中尽情搜索: