package imgdemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;

import utils.Base64;
import utils.StringUtil;


@SuppressWarnings("all")
public class ImgServlet extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String img = getImgById();
			byte[] decode = Base64.decode(img);
			OutputStream outputStream = response.getOutputStream();
			outputStream.write(decode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void init() throws ServletException {
	}
	
	
	public static ResultSet rs = null;
	public static void main(String[] args) throws Exception {
		insertImgDb();
//		getImgById();
	}
	
	public static void insertImgDb() throws Exception{
		File f = new File("D:\\123.jpg");
		System.out.println("s");
		Connection conn = JdbcUtils.getConnection();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String id = sdf.format(new Date());
		System.out.println(id);
		String sql = "insert into imgdemo (id,image) values(?,?)";
		PreparedStatement pst = conn.prepareStatement(sql);
		InputStream is = new FileInputStream(f);
		String img = Base64.encode(is);
		System.out.println(img);
		pst.setString(1, id);
		pst.setString(2, img);
		pst.execute();
		conn.commit();
		JdbcUtils.free(rs, pst, conn);
	}
	
	public static String getImgById() throws Exception{
		System.out.println(new String("20170812171808052").length());
		Connection conn = JdbcUtils.getConnection();
		
		String sql = "select t.image from imgdemo t where id = ? ";
		PreparedStatement pst = conn.prepareStatement(sql);
		pst.setString(1, "20170812174713600");
		rs = pst.executeQuery();
		Object img = "";
		while (rs.next()) {
			img = rs.getObject(1);	 
	    }
		String clob2String = StringUtil.getClob2String(img);
		System.out.println(clob2String);
		JdbcUtils.free(rs, pst, conn);
		return clob2String;
	}

}
最近下载更多
小白jmhuang  LV12 2022年4月23日
连夜不知终雨期  LV2 2021年12月24日
lwp011  LV27 2021年7月21日
lan12138  LV8 2021年3月18日
大神程序员  LV22 2020年11月28日
King_wsk  LV18 2020年10月14日
李小江  LV8 2020年6月23日
cxdxfx12  LV14 2019年7月7日
月球上的人7  LV2 2019年4月24日
eee222  LV2 2019年4月12日
最近浏览更多
ly4910  LV3 2023年2月21日
109284656  LV1 2022年10月26日
小白jmhuang  LV12 2022年4月23日
17798830  LV14 2022年1月25日
空心菜4  LV9 2022年1月12日
连夜不知终雨期  LV2 2021年12月24日
lwp011  LV27 2021年7月21日
裤裆很帅  LV16 2021年6月6日
aslyyds  LV2 2021年5月24日
gakiii  LV1 2021年4月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友