请问一个有关Oracle数据库的问题。

发布网友 发布时间:2022-04-23 18:20

我来回答

2个回答

热心网友 时间:2022-05-05 00:13

工具类:
package org.blog.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageUtil {
private static File file = null;
/**
* 读取图像的二进制流
*
* @param infile
* @return
*/
public static FileInputStream getByteImage(String infile) {
FileInputStream inputImage = null;
file = new File(infile);
try {
inputImage = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return inputImage;
}
/**
* 输出图片
* @param inputStream
* @param path
*/
public static void readBlob(FileInputStream inputStream, String path) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(path);
byte[] buf = new byte[1024];
int len = 0;
while ((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);// 写
}
inputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

servlet源码
package servlet;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Blob;
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.blog.util.ImageUtil;
import org.hibernate.Hibernate;
public class Image extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
FileInputStream in = ImageUtil.getByteImage("D:\\me.jpg");
Blob blob = Hibernate.createBlob(in);
InputStream inputStream = blob.getBinaryStream();// IO流
int length = (int) blob.length();
byte[] b = new byte[length];
inputStream.read(b, 0, length);
PrintWriter out = resp.getWriter();
InputStream is = new ByteArrayInputStream(b);
int a = is.read();
while (a != -1) {
out.print((char) a);
a = is.read();
}
out.flush();
out.close();
/*OutputStream outputStream = resp.getOutputStream();// 从response中获取getOutputStream
outputStream.write(b);// 写
inputStream.close();
outputStream.close();*/
} catch (Exception e) {
System.out.println("error");
}
}
}
jsp源码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'image.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
-->
</head>

<body>
<div style="border: solid red ;" mce_style="border: solid red ;"> <img src="image.do" mce_src="image.do"></div>
</body>
</html>

热心网友 时间:2022-05-05 01:31

一般不建议把图片存在数据库。。。  图片可以上传到服务器本地的某个目录,数据库表里的字段里面存图片的地址就可以了。。。取的时候  直接通过File类就可以。。。。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com