给你段代码,是用来在ie上显示的(servlet):
public void doGet(>
在OracleQueryBean类中增加一个函数,来进行读取,具体代码如下:
/
根据在数据库中的ID进行读取
@param strID 字段ID
@param w 需要缩到的宽度
@param h 需要缩到高度
@return
/
public byte[] GetImgByteById(String strID, int w, int h){
//Systemoutprintln("Get img data which id is " + nID);
if(myConnection == null)
thisgetConnection();
byte[] data = null;
try {
Statement stmt = myConnectioncreateStatement();
ResultSet myResultSet = stmtexecuteQuery("select " + thisstrIDName + " from " + thisstrTabName + " where " + thisstrIDName + "=" + strID);
StringBuffer myStringBuffer = new StringBuffer();
if (myResultSetnext()) {
javasqlBlob blob = myResultSetgetBlob(thisstrImgName);
InputStream inStream = blobgetBinaryStream();
try {
long nLen = bloblength();
int nSize = (int) nLen;
//Systemoutprintln("img data size is :" + nSize);
data = new byte[nSize];
inStreamread(data);
inStreamclose();
} catch (IOException e) {
Systemoutprintln("获取数据失败,原因:" + egetMessage());
}
data = ChangeImgSize(data, w, h);
}
Systemoutprintln(myStringBuffertoString());
myConnectioncommit();
myConnectionclose();
} catch (SQLException ex) {
Systemoutprintln(exgetMessage());
}
return data;
}
页面使用OracleQueryBean来根据用户提供的id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:
<%@ page language="java" contentType="text/html;;charset=gbk" %>
<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemoOracleQueryBean" />
<%
responsesetContentType("image/jpeg");
//在数据库中的 ID
String strID = requestgetParameter("id");
//要缩略或放大的宽度
String strWidth = requestgetParameter("w");
//要缩略或放大的高度
String strHeight = requestgetParameter("h");
byte[] data = null;
if(strID != null){
int nWith = IntegerparseInt(strWidth);
int nHeight = IntegerparseInt(strHeight);
//获取的byte数据
data = OrcleQueryGetImgByteById(strID, nWith, nHeight);
ServletOutputStream op = responsegetOutputStream();
opwrite(data, 0, datalength);
opclose();
op = null;
responseflushBuffer();
//清除输出流,防止释放时被捕获异常
outclear();
out = pageContextpushBody();
}
%>
楼上挺全了!!
不过说些题外话
就是
数据库中存取文件的做法是不大好的!!
建议文件存放在硬盘中,数据库存放文件的相对路径。
显示的时候直接读取路径信息。
您好:读取bmp文件到BufferedImage中
File file2 = new File("c:\\testimages\\tttt" + "bmp");
// BufferedImage bi = backstoregetBufferedImage();
try {
output = ImageIOread(file2);
} catch (IOException e) {
eprintStackTrace();
}
输出bmp文件
File file2 = new File("c:\\testimages\\tttt" + "bmp");
ImageIOwrite(bi, "bmp", file2);
Byte[]输出到文件
byte[] buf =UtilZipzipObjectToByte(cache);
File file2 = new File("c:\\testimages\\cachebin");
FileOutputStream fos =new FileOutputStream(file2);
foswrite(buf);
fosflush();
fosclose();
读文件到Byte[]中
File file2 = new File("c:\\testimages\\cachebin");
FileInputStream fis = new FileInputStream(file2);
byte[] buf = new byte[(int) file2length()];
fisread(buf);
fisclose();
填充颜色到整个画布
BufferedImage bi = backstoregetBufferedImage();
Graphics g2 = bigetGraphics();
g2setColor(Colorred);
g2fillRect(0, 0, Commonwidth,
Commonheight);
图像变灰 *** 作
public finalBufferedImage getGrayPicture(BufferedImage originalPic) {
int imageWidth = originalPicgetWidth();
int imageHeight = originalPicgetHeight();
BufferedImage newPic = new BufferedImage(imageWidth, imageHeight,
BufferedImageTYPE_3BYTE_BGR);
ColorConvertOp cco = new ColorConvertOp(ColorSpace
getInstance(ColorSpaceCS_GRAY), null);
ccofilter(originalPic, newPic);
return newPic;
}
ImageIO
javaximageioImageIO lets you save and restore Images to disk in a platform independent format It works using plug-in modules that handle various formats including "gif", "png" and "jpeg" (all lower case, or all upper case, but not mixed) "jpeg" or "jpg" is acceptable Use ImageIO getWriterFormatNames() to find out which types are supported on your platform:
import javaximageioImageIO;
public class Jai
{
public static void main ( String[] args )
{
String[] names = ImageIOgetWriterFormatNames();
for ( String name: names )
{
Systemoutprintln( name );
}
}
}
Loading an Image from raw bytes
Here raw bytes represent the image of a gif, png or
// raw bytes to BufferedImage
import javaawtimageBufferedImage;
import javaioByteArrayInputStream;
import javaximageioImageIO;
BufferedImage image = ImageIOread ( new ByteArrayInputStream ( rawImageBytes ) );
Saving an Image to raw bytes
// BufferedImage to raw bytes
import javaximageioImageIO;
import javaawtimageBufferedImage;
import javaioByteArrayOutputStream;
// O P E N
ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 );
// W R I T E
ImageIOwrite( aBufferedImage, "jpeg" ,
baos );
// C L O S E
baosflush();
byte[] resultImageAsRawBytes = baostoByteArray();
baosclose();
Loading a BufferedImage from a file
// file to BufferedImage
import javaawtimage BufferedImage;
import javaioFile;
import javaximageioImageIO;
BufferedImage image = ImageIOread( new File( "rabbitjpg" ) );
Saving a BufferedImage to a file
// BufferedImage to File
import javaximageioImageIO;
import javaawtimageBufferedImage;
import javaioFile;
ImageIOwrite( aBufferedImage, "jpeg" ,
new File ( "snapjpg" ) );
ImageWriteParam is a way of controlling exactly how the image in encoded There is currently no PNG support for it This is not for injecting meta info
Loading a BufferedImage from an URL
// url to BufferedImage
import javaawtimageBufferedImage;
import javaximageioImageIO;
BufferedImage image = null;
try
{
image = ImageIOread( url );
}
catch ( IOException e )
{
Systemoutprintln( "image missing" );
}
Converting Image to BufferedImage
// Image to BufferedImage
import javaawtimageBufferedImage;
import javaawtImage;
BufferedImage bufferedImage = new BufferedImage ( imageWidth,
imageHeight,
BufferedImageTYPE_INT_BGR );
bufferedImagecreateGraphics()drawImage( image, 0, 0, this );
frameshow()这句后面缺一个";"
我在我电脑上运行了,可以显示出来
你应该是路径写错了
你可以用下面的代码测试下是不是路径写错了
Systemoutprintln(new File("1jpg")exists());
其实在大多数的实际项目中 数据库中存放的是 的url地址 直接将放置到你的项目中的一个专门的文件夹先 在页面显示的时候直接按照 url来显示 这样既方便由能够缓解服务器负担增加性能 你可以尝试一下
<img src="你从数据库中获得的的url">
想必读取数据库你应该会把
以上就是关于java实现图片上传至服务器并显示,如何做全部的内容,包括:java实现图片上传至服务器并显示,如何做、java网页怎样从后台读取背景图片显示在前台页面、java提取数据库中blob类型的图片,如何全部显示在jsp页面等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)