文本文件上传数据库
来源:互联网 发布:sql server raiserror 编辑:程序博客网 时间:2024/05/21 08:53
代码:
index2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>文件上传</title></head><body> <form action="FileUploadServlet" method="post" enctype="multipart/form-data"> 选择上传文件: <input type="file" name="file" id="file"/><br/><br/> <input type="submit" value="上传" name="upload" id="upload"/> </form></body></html>
FileUploadServlet.java
package com.servlet;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.sql.Connection;import java.sql.SQLException;import java.sql.PreparedStatement;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.annotation.MultipartConfig;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;import com.connect.DButil;@MultipartConfig//标识Servlet支持文件上传public class FileUploadServlet extends HttpServlet {private static final long serialVersionUID = 1L; public FileUploadServlet() { super(); // TODO Auto-generated constructor stub } protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); final Part filePart=request.getPart("file"); final String fileName=getFileName(filePart); Connection conn=null; OutputStream out=null; InputStream filecontent=null; final PrintWriter writer=response.getWriter(); try { conn=DButil.open();conn.setAutoCommit(false);String sql="INSERT INTO images(name,image) VALUES(?,?)";PreparedStatement stmt=(PreparedStatement) conn.prepareStatement(sql);stmt.setString(1, fileName);filecontent=filePart.getInputStream();stmt.setBinaryStream(2, filecontent, (int)filePart.getSize());stmt.execute();conn.commit();//response.sendRedirect("disping2.jsp");RequestDispatcher view =request.getRequestDispatcher("disping2.jsp"); view.forward(request, response);} catch (SQLException e) {// TODO Auto-generated catch blockwriter.println("<br/>错误:"+e.getMessage());e.printStackTrace();} finally {if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(out!=null){out.close();}if(filecontent!=null){filecontent.close();}if(writer!=null){writer.close();}} }public String getFileName(final Part part) {// TODO Auto-generated method stubfor(String content : part.getHeader("content-disposition").split(";")){String filename=content.substring(content.lastIndexOf('\\')+1).trim().replace("\"", "");return filename;}return null;}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubprocessRequest(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubprocessRequest(request, response);String Filename=request.getParameter("file");}}
disping2.jsp
<%@page import="com.servlet.FileUploadServlet"%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% final Part filePart=request.getPart("file"); %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>显示附件</title></head><body> <h4>显示上传附件</h4> <a href="ImageServlet">附件1</a></body></html>
ImageServlet.java
package com.servlet;import java.io.IOException;import java.io.InputStream;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.connect.DButil;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;public class ImageServlet extends HttpServlet {private static final long serialVersionUID = 1L; public ImageServlet() { super(); // TODO Auto-generated constructor stub }protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//response.setContentType("image/jpeg");response.setContentType("text/html HTML ");request.setCharacterEncoding("UTF-8");ServletOutputStream os=response.getOutputStream();Connection conn=null;try {conn=DButil.open();String sql="SELECT name,image FROM images ORDER BY id DESC";PreparedStatement stmt=(PreparedStatement) conn.prepareStatement(sql);ResultSet rs=stmt.executeQuery();if(rs.next()){//没有用到@SuppressWarnings("unused") String name=rs.getString(1); byte[] buffer=new byte[1]; InputStream is=rs.getBinaryStream(2); while(is.read(buffer)>0){ os.write(buffer); } os.flush();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(os!=null){os.close();}}}}
截图:
点开文本文件内容
0 0
- 文本文件上传数据库
- 上传 文本文件 ,jpg
- php处理上传文本文件
- TCP实现 文本文件上传
- java(7)--上传文本文件
- jsp 实现文本文件上传
- Django上传文本文件
- TCP-上传文本文件
- TCP实现文本文件上传
- 文件上传(一)原生文本文件上传
- csv文本文件导入oracle数据库
- mongodb导出数据库到文本文件
- mysql数据库(七)导入文本文件
- asp操作数据库以及文本文件
- java自动识别用户上传的文本文件编码
- 网络编程_TCP协议上传文本文件
- 把文本文件读取到SQLServer数据库中
- 将文本文件导入Sql server数据库
- xamarin.iOS 设置Label指定文字的颜色
- gstreamer学习笔记:通过udp传输音视频
- 使用C#强制转化日期
- RNN以及LSTM的介绍和公式梳理
- Java泛型之Type体系
- 文本文件上传数据库
- Java——数据结构——堆排序
- web接口测试中需要测试的几个点
- Mining Twitter Data with Python Part 6: Sentiment Analysis Basics
- 关于easyui中datagrid数据网格与form表单的使用总结
- Android Json解析详解(详细代码)
- mysql用一个表更新另一个表
- String字符串特殊字符强制不转义
- 【Unity】基于ProtoBuffer与Socket实现网络传输