jsp 如何从数据库中读取二进制文件(图片)并显示

 我来答
若以下回答无法解决问题,邀请你更新回答
百度网友a86ed8402
2006-05-09 · TA获得超过445个赞
知道答主
回答量:104
采纳率:0%
帮助的人:0
展开全部
提供的jsp页面代码:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="struts/html" prefix="html" %>
<%@ taglib uri="struts/bean" prefix="bean" %>
<%@ taglib uri="struts/logic" prefix="logic" %>
<link rel="stylesheet" href="/dbsd/public.css" type="text/css">
<html:html>
<head><title>上传照片</title></head>
<body>
<%
String type=String.valueOf(request.getParameter("type"));
int i;
if("old".equals(type)){
i=0;
}else{
i=1;
}
%>
<script language="javascript">
function addInfo(){
if(document.PhotosForm.explain.value==""){
alert("请填写说明内容!");
return false;
}
if(document.PhotosForm.schoolPhoto.value==""){
alert("请选择照片文件!");
return false;
}

if((document.PhotosForm.schoolPhoto.value.indexOf(".jpg"))>0||(document.PhotosForm.schoolPhoto.value.indexOf(".bmp")>0)||(document.PhotosForm.schoolPhoto.value.indexOf(".gif")>0)||(document.PhotosForm.schoolPhoto.value.indexOf(".JPG"))>0||(document.PhotosForm.schoolPhoto.value.indexOf(".BMP")>0)||(document.PhotosForm.schoolPhoto.value.indexOf(".GIF")>0)){
return true;
}else{
alert("请选择照片格式(.bmp或.jpg或.gif文件)!");
return false;
}
}
</script>

<html:form action="uploadSchoolPhoto.do" method="post" enctype="multipart/form-data">

<table align="center" width=778 align=center bgcolor="#f0faff" cellspacing=0 border=0>
<tr>
<td align="center" ><font size="5" >上传照片</font></td>
</tr>
<tr>
<td>
<table align="center">
<tr>
<td>
<a href=/dbsd/schoolPhoto/schoolPhoto.jsp>  师大首页</a>-->

<%
if("old".equals(type)) out.print("<a href=\"/dbsd/queryOldPhoto.do\">校园老照片</a>-->上传老照片");
else out.print("<a href=\"/dbsd/queryNewPhoto.do\">校园新貌</a>-->上传新照片");
%>

</td>
</tr>

<tr>
<td><div align="right">照 片:</div></td>
<td><html:file property="schoolPhoto" size="50"/></td>
</tr>
<tr>
<td><html:errors property="schoolPhotoa"/></td><td><html:errors property="schoolPhotob"/></td> </tr>
<tr>

<td><input type="hidden" name="state" value="<%=i%>"/></td>

</tr>
<tr>
<td><div align="right">注释内容:</div></td>
<td><html:text property="explain" size="50"/></td>
</tr>
<tr>
<td></td>
<td ><html:errors property="explain"/></td>
</tr>
<td colspan="2">
<div align="center">
<html:submit onclick="return(addInfo())">上传</html:submit>
<html:reset>重填</html:reset>
</div></td>
</tr>
</table>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>

主要的java类:
package com.toowell.schoolPhoto.action;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.Action;
import org.apache.struts.upload.FormFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.util.Vector;
import java.io.*;

import com.toowell.common.DbBean;
import com.toowell.common.Util;
import com.toowell.common.Upload;
import com.toowell.common.UserUtil;
import com.toowell.common.page.PageBean;
import com.toowell.schoolPhoto.form.PhotosForm;
import com.toowell.schoolPhoto.model.PhotosBean;
import com.toowell.schoolPhoto.model.PhotosVo;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-10-27
* Time: 9:11:05
* To change this template use Options | File Templates.
*/
public class PhotoShowAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

String parameter= mapping.getParameter();

boolean isAdmin=UserUtil.isAdmin(request);
boolean isAudit=UserUtil.isAudit(request);
//查询老照片
if("old".equalsIgnoreCase(parameter)) {
String sql="select * from schoolPhoto where state=0 and audit=1 order by id desc";
if(isAdmin){
sql=null;
sql="select * from schoolPhoto where state=0 order by id desc";
}
System.out.println(sql);
PageBean pageBean =new PageBean();
pageBean.pageshow(request,sql,"com.toowell.schoolPhoto.model.PhotosList",16);

request.setAttribute("type","old");
return mapping.findForward("success");
}
//查询新照片
if("new".equalsIgnoreCase(parameter)) {
String sql="select * from schoolPhoto where state=1 and audit=1 order by id desc";
if(isAdmin){
sql=null;
sql="select * from schoolPhoto where state=1 order by id desc";
}
System.out.println(sql);
PageBean pageBean =new PageBean();
pageBean.pageshow(request,sql,"com.toowell.schoolPhoto.model.PhotosList",16);
request.setAttribute("type","new");
return mapping.findForward("success");
}
//
if("add".equalsIgnoreCase(parameter)) {
String servletDir = servlet.getServletContext().getRealPath("/schoolPhoto/images");
PhotosForm photos=(PhotosForm)form;
String explain=Util.changeCode(photos.getExplain());

String state=photos.getState();
int type=Integer.parseInt(state);
int id=0;
try{
id = PhotosBean.getNowID()+1;
} catch(Exception e){
return mapping.findForward("imageError");
}
//获取上传的图片文件
FormFile formFile = photos.getSchoolPhoto();
//上传位置加上文件名,不含扩展名。回为扩展名可以自动判断
String dir = servletDir + "/" +id;

Upload up = new Upload(formFile,dir);
up.upload();
String imageType =up.getFileType();
PhotosVo photosVo=new PhotosVo();
photosVo.setId(id);
photosVo.setImageType(imageType);
photosVo.setExplain(explain);
try{
PhotosBean.add(photosVo,state);
}catch(Exception e){
return mapping.findForward("imageError");
}
if(type==0){

request.setAttribute("type","old");
return mapping.findForward("oldSuccess");
} else{
request.setAttribute("type","new");
return mapping.findForward("newSuccess");
}
}

if("delete".equalsIgnoreCase(parameter)) {
String servletDir = servlet.getServletContext().getRealPath("/schoolPhoto/images");
String id=request.getParameter("id");
ResultSet rs=null;
int state=0;
//删除照片
String sqlImageType="select imageType,state from schoolPhoto where id="+id;
try{
rs= DbBean.getRs(sqlImageType);
rs.next();
String imageType=rs.getString(1);
state=rs.getInt(2);
String imageName = id+imageType;

File file = new File(servletDir,imageName);
file.delete();
DbBean.close();
}catch(Exception e){
return mapping.findForward("imageError");
}
//删除照片数据库记录
String sql="delete from schoolPhoto where id="+id;
try{
DbBean.execute(sql);
DbBean.close();
}catch(Exception e){
return mapping.findForward("imageError");
}
if(state==0){

request.setAttribute("type","old");
return mapping.findForward("oldSuccess");
} else{
request.setAttribute("type","new");
return mapping.findForward("newSuccess");
}

}

if("modify".equalsIgnoreCase(parameter)) {
ResultSet rs=null;
Vector list=new Vector();
String id=request.getParameter("id");
String sql="select * from schoolPhoto where id="+id;
String servletDir = servlet.getServletContext().getRealPath("/schoolPhoto/images");
String state=request.getParameter("type");
try{
rs=DbBean.getRs(sql);

while(rs.next()){
PhotosVo photosVo=new PhotosVo();
photosVo.setId(rs.getInt(1));
photosVo.setImageType(rs.getString(2));
photosVo.setExplain(rs.getString(3));
photosVo.setState(rs.getInt(6));
list.add(photosVo);
}
}catch(Exception e){
return mapping.findForward("imageError");
}

request.setAttribute("listPhoto",list);
if(state.equals("old")){

request.setAttribute("type","old");

} else{
request.setAttribute("type","new");

}
return mapping.findForward("success");
}
if("modifyOne".equalsIgnoreCase(parameter)) {
String id=request.getParameter("id");

PhotosForm photo=(PhotosForm)form;
String explain=Util.changeCode(photo.getExplain());
int state=Integer.parseInt(photo.getState());
System.out.println("----=-=-=-=-=--"+state);
String sql="update schoolPhoto set explain='"+explain+"',audit=0 where id="+id;
System.out.println(sql);
try{
DbBean.execute(sql);
DbBean.close();
}catch(Exception e){
return mapping.findForward("imageError");
}
if(state==0){

request.setAttribute("type","old");
return mapping.findForward("oldSuccess");
} else{
request.setAttribute("type","new");
return mapping.findForward("newSuccess");
}
}
if("audit".equalsIgnoreCase(parameter)) {
String id=request.getParameter("id");
String state=request.getParameter("type");
String sql="update schoolPhoto set audit=1 where id="+id;
// System.out.println(sql);
try{
DbBean.execute(sql);
DbBean.close();
}catch(Exception e){
return mapping.findForward("imageError");
}
if(state.equals("old")){

request.setAttribute("type","old");
return mapping.findForward("oldSuccess");
} else{
request.setAttribute("type","new");
return mapping.findForward("newSuccess");
}
}
return null;
}
}

以上代码仅供参考,恕不能提供更多的代码;
祝好运
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式