欧美特黄不卡,涩涩视频在线,成人国产精品一区二区网站,亚洲一区二区三区欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

從數據庫中讀取并生成圖片的Servlet

從數據庫中讀取并生成圖片的Servlet

更新時間:2022-05-10 文章作者:未知 信息來源:網絡 閱讀次數:

大體思路
1)創建ServletOutputStream對象out,用于以字節流的方式輸出圖像
2)查詢數據庫,用getBinaryStream方法返回InputStream對象in
3)創建byte數組用作緩沖,將in讀入buf[],再由out輸出

注:下面的例程中數據庫連接用了ConnectionPool,以及參數的獲得進行了預處理

package net.seasky.music;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import net.seasky.util.*;
import net.seasky.database.DbConnectionManager;

public class CoverServlet extends HttpServlet {
private static final String CONTENT_TYPE = "image/gif";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void doGet(HttpServletRequest request, HttpServletResponse response
) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
int albumID;
ServletOutputStream out = response.getOutputStream();
try {
albumID = ParamManager.getIntParameter(request,"albumID",0);
}
catch (Exception e) {
response.sendRedirect("../ErroePage.jsp");
return;
}
try {
InputStream in=this.getCover(albumID);
int len;
byte buf[]=new byte[1024];
while ((len=in.read(buf,0,1024))!=-1) {
out.write(buf,0,len);
}
}
catch (IOException ioe) {
ioe.printStackTrace() ;
}
}

private InputStream getCover(int albumID) {
InputStream in=null;
Connection cn = null;
PreparedStatement pst = null;
try {
cn=DbConnectionManager.getConnection();
cn.setCatalog("music");
pst=cn.prepareStatement("SELECT img FROM cover where ID =?");
pst.setInt(1,albumID);
ResultSet rs=pst.executeQuery();
rs.next() ;
in=rs.getBinaryStream("img");
}
catch (SQLException sqle) {
System.err.println("Error in CoverServlet : getCover()-" + sqle);
sqle.printStackTrace() ;
}
finally {
try {
pst.close() ;
cn.close() ;
}
catch (Exception e) {
e.printStackTrace();
}
}
return in;
}

public void destroy() {
}

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: 台南县| 鹤岗市| 汝南县| 鹤岗市| 阳江市| 成武县| 苏州市| 望奎县| 庆阳市| 和平区| 平乡县| 德江县| 中阳县| 青田县| 龙陵县| 新沂市| 永胜县| 天祝| 常德市| 乃东县| 大化| 高阳县| 德清县| 鹤岗市| 苏州市| 白沙| 织金县| 黎川县| 平泉县| 孝义市| 德阳市| 泸溪县| 大庆市| 大厂| 崇仁县| 同心县| 金寨县| 横山县| 沈阳市| 巴楚县| 保德县|