影像檔上傳資料庫__資料庫

來源:互聯網
上載者:User

 代碼:

 index.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" accept="image/*"/><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.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("disping.jsp");} 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();}}        }private 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);}}

disping.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>     <h4>顯示最後一張圖片</h4>     <img alt="圖片" src="ImageServlet"/></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 stubresponse.setContentType("image/jpeg");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();}}}}

截圖:




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.