在EJB(Session)中如何将Blob(图片)返回给客户端,应该返回什么类型呢?解决马上结帐!! 不行啊,在EJB里返回Byte报错啊,好象是要返回可序列化对象的。大家帮忙啊! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package util.db;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import sun.jdbc.rowset.CachedRowSet;public class Download extends HttpServlet { //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String contentType = request.getParameter("contentType"); if (contentType == null) { contentType = "text/html; charset=GBK";//默认情况下输出的类型为中文文本 } String uploadId = request.getParameter("uploadId"); if (uploadId == null) { uploadId = ""; } String fileName = request.getParameter("fileName"); if (fileName == null) { fileName = ""; } try{ response.setContentType(contentType); ServletOutputStream out=response.getOutputStream(); CachedRowSet rs=null; String sql="select * from uploads where filename='"+fileName+"'"; System.out.println(sql); DB mydb=new DB(); rs=mydb.executeQuery(sql); if (rs.next()){ BufferedInputStream data=new BufferedInputStream(rs.getBinaryStream("binaryfile")); byte[] buf=new byte[4*1024]; int len; while((len=data.read(buf,0,buf.length))!=-1){ out.write(buf,0,len); } } else{ response.sendError(response.SC_NOT_FOUND); } } catch(Exception e){ System.out.println(e.getMessage()); } } //Clean up resources public void destroy() { }} 你可以做一个类,实现串行华里面包含一个ServletOutputStream 成员然后返回一个这个类,时时 to :gks_cn(981530)非常感谢你的回答,可是这个类怎么写啊,如何实现串行化,给个例子好么?我很菜的,谢谢。 Byte会抱错么,我记得他应该实现串行化了吧,没有?我的回去看看 public class YourObject implements Serializable { private byte[] yourBuf; public void setYourBuf(byte[] yourBuf) { this.yourBuf = yourBuf; } public byte[] getYourBuf() { return this.yourBuf; }} 我有个sql server的,你修改一下就行了:<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %><% String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; String user = "sa"; String password = "password"; InputStream in = null; ServletOutputStream sop = null; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql = "select * from pub_info where pub_id = '9901'"; ResultSet rs = stmt.executeQuery(sql); if (rs.next()) { in = rs.getBinaryStream("logo"); response.setContentType("image/jpeg"); sop = response.getOutputStream(); int len; byte[] buf = new byte[1024]; while ((len = in.read(buf, 0, 1024)) != -1) { sop.write(buf, 0, len); } } sop.close(); in.close(); rs.close(); stmt.close(); conn.close(); } catch (Exception exc) { out.println("An error occurs : " + exc); }%> 使用ejb的话,返回shihb() 所给的那个类就行了。 凡是流操作都应该implements Serializable 接口,象图象、音频、文件、等等,我们都可以作为流文件进行传输! 我做过这样的试验:import java.sql.*;import java.io.*;public class PhotoBean { Connection conn=null; ResultSet rs = null; PreparedStatement psmt=null; int filesize=0; public PhotoBean() { } private Connection getConnetion(){ String urlConn="jdbc:odbc:sample"; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println(DriverManager.getConnection(urlConn)); return DriverManager.getConnection(urlConn); } catch (Exception ex) { System.out.println("connection failed"); } return conn; } public void setBlob(String filename){ String sql="update test set text=? where id=1"; DataInputStream dis = null; try { dis = new DataInputStream(new FileInputStream(new File(filename))); filesize=dis.available(); } catch (Exception ex1) { ex1.printStackTrace(); } try{ conn = this.getConnetion(); psmt = conn.prepareStatement(sql); psmt.setBinaryStream(1,dis,filesize); psmt.executeUpdate(); psmt.close(); conn.close(); }catch(Exception ex){ ex.printStackTrace(); } } public byte[] getBlob(){ String sql="select text from test where id=1"; DataInputStream dis=null; try{ conn = this.getConnetion(); psmt = conn.prepareStatement(sql); rs=psmt.executeQuery(); rs.next(); dis=new DataInputStream(rs.getBinaryStream(1)); DataInputStream dis1 = new DataInputStream(new FileInputStream(new File("c:\\a.jpg"))); filesize=dis1.available(); byte[] bt=new byte[this.filesize]; dis.read(bt); psmt.close(); conn.close(); return bt; }catch(Exception ex){ ex.printStackTrace(); } return null; } public static void main(String[] args){ new PhotoBean().setBlob("c:\\a.jpg"); }} import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import com.lanxin.*;public class ImageServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { performTask(request, response); } //Clean up resources public void destroy() { } private void performTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PhotoBean photo = new PhotoBean(); try { byte[] buf = photo.getBlob(); if (buf != null) { resp.setContentType("image/jpg"); resp.getOutputStream().write(buf); //for(int i=0;i<buf.length ;i++) //System.out.println(buf[i]); } }catch(Exception e){ } } } <%@ page contentType="text/html; charset=GBK" %><html><head><title>photo</title></head><jsp:useBean id="photoBeanId" scope="session" class="com.lanxin.PhotoBean" /><jsp:setProperty name="photoBeanId" property="*" /><body bgcolor="#ffffff"><h1><img src="/photo/imageservlet"><img src="a.jpg"><%//response.setContentType("image/jpg");//response.getOutputStream().write(photoBeanId.getPhoto());%></h1>photo</body></html> 惭愧 Struts2配置问题求指点 java学习感觉到了瓶颈??暑假到了,求高人指点学习方向。 谁帮帮忙 告诉我这是什么错误 怎么解决呀 启动Tomcat就报错,求解 关于dialog等一些小问题 谢谢大家先 过滤器问题 求JAVA程序员试练的一些项目 关于getHibernateTemplate().update()的问题 CSDN,怎么让这种垃圾当版主? 用JAVA实现股票K线图。。。 客户端调用RMI在什么情况下会导致抛出java.rmi.UnmarshalException EJB的疑惑
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import sun.jdbc.rowset.CachedRowSet;public class Download extends HttpServlet {
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String contentType = request.getParameter("contentType");
if (contentType == null) {
contentType = "text/html; charset=GBK";//默认情况下输出的类型为中文文本
}
String uploadId = request.getParameter("uploadId");
if (uploadId == null) {
uploadId = "";
}
String fileName = request.getParameter("fileName");
if (fileName == null) {
fileName = "";
}
try{
response.setContentType(contentType);
ServletOutputStream out=response.getOutputStream();
CachedRowSet rs=null;
String sql="select * from uploads where filename='"+fileName+"'";
System.out.println(sql);
DB mydb=new DB();
rs=mydb.executeQuery(sql);
if (rs.next()){
BufferedInputStream data=new BufferedInputStream(rs.getBinaryStream("binaryfile"));
byte[] buf=new byte[4*1024];
int len;
while((len=data.read(buf,0,buf.length))!=-1){
out.write(buf,0,len);
}
}
else{
response.sendError(response.SC_NOT_FOUND);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}
里面包含一个
ServletOutputStream 成员
然后返回一个这个类,时时
非常感谢你的回答,可是这个类怎么写啊,如何实现串行化,给个例子好么?
我很菜的,谢谢。
private byte[] yourBuf;
public void setYourBuf(byte[] yourBuf) {
this.yourBuf = yourBuf;
}
public byte[] getYourBuf() {
return this.yourBuf;
}
}
<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %>
<% String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user = "sa";
String password = "password"; InputStream in = null;
ServletOutputStream sop = null;
try
{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql = "select * from pub_info where pub_id = '9901'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
{
in = rs.getBinaryStream("logo");
response.setContentType("image/jpeg");
sop = response.getOutputStream();
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf, 0, 1024)) != -1)
{
sop.write(buf, 0, len);
}
}
sop.close();
in.close();
rs.close();
stmt.close();
conn.close();
}
catch (Exception exc)
{
out.println("An error occurs : " + exc);
}
%>
import java.sql.*;
import java.io.*;public class PhotoBean {
Connection conn=null;
ResultSet rs = null;
PreparedStatement psmt=null;
int filesize=0;
public PhotoBean() {
} private Connection getConnetion(){
String urlConn="jdbc:odbc:sample";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println(DriverManager.getConnection(urlConn));
return DriverManager.getConnection(urlConn);
}
catch (Exception ex) {
System.out.println("connection failed");
}
return conn;
} public void setBlob(String filename){
String sql="update test set text=? where id=1";
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(new File(filename)));
filesize=dis.available();
}
catch (Exception ex1) {
ex1.printStackTrace();
} try{
conn = this.getConnetion();
psmt = conn.prepareStatement(sql);
psmt.setBinaryStream(1,dis,filesize);
psmt.executeUpdate(); psmt.close();
conn.close(); }catch(Exception ex){
ex.printStackTrace();
}
} public byte[] getBlob(){
String sql="select text from test where id=1";
DataInputStream dis=null;
try{
conn = this.getConnetion();
psmt = conn.prepareStatement(sql);
rs=psmt.executeQuery();
rs.next();
dis=new DataInputStream(rs.getBinaryStream(1));
DataInputStream dis1 = new DataInputStream(new FileInputStream(new File("c:\\a.jpg")));
filesize=dis1.available();
byte[] bt=new byte[this.filesize];
dis.read(bt); psmt.close();
conn.close();
return bt; }catch(Exception ex){
ex.printStackTrace();
} return null;
} public static void main(String[] args){
new PhotoBean().setBlob("c:\\a.jpg");
}}
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.lanxin.*;public class ImageServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
performTask(request, response);
}
//Clean up resources
public void destroy() {
} private void performTask(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
PhotoBean photo = new PhotoBean(); try
{
byte[] buf = photo.getBlob();
if (buf != null)
{
resp.setContentType("image/jpg");
resp.getOutputStream().write(buf);
//for(int i=0;i<buf.length ;i++)
//System.out.println(buf[i]);
}
}catch(Exception e){ }
}
}
<head>
<title>
photo
</title>
</head>
<jsp:useBean id="photoBeanId" scope="session" class="com.lanxin.PhotoBean" />
<jsp:setProperty name="photoBeanId" property="*" />
<body bgcolor="#ffffff">
<h1>
<img src="/photo/imageservlet">
<img src="a.jpg"><%
//response.setContentType("image/jpg");
//response.getOutputStream().write(photoBeanId.getPhoto());
%></h1>
photo
</body>
</html>