当前页面: 开发资料首页 → J2EE 专题 → JSP如何将图片存入数据库
摘要: 如何将图片存入数据库及取出
<%@ page contentType="text/html; charset=GBK" %>
<%@ page java.io.*" %>
<%@ page import="com.jspsmart.upload.*" %>
<%
SmartUpload su = new SmartUpload();
// 上传初始化
su.initialize(pageContext);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// su.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// su.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// su.setAllowedFilesList("jpg,jpeg");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
// su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
// 上传文件
//System.out.println("test 1");
su.upload();
//System.out.println("test 2");
byte[] fileBytes = null;
for (int i = 0; i < su.getFiles().getCount(); i++) {
//System.out.println("test 3");
com.jspsmart.upload.File uploadFile = su.getFiles().getFile(i);
if (!uploadFile.isMissing()) {
//System.out.println("test 4");
//xmlFileName = uploadFile.getFilePathName();
fileBytes = new byte[uploadFile.getSize()];
for (int j = 0; j < uploadFile.getSize(); j++) {
//System.out.println("test 5");
fileBytes[j] = uploadFile.getBinaryData(j);
} break;
}
}
%>
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.io.*"%>
<%@ page import="com.Info" %>
<% String id=fi.getGbk(request.getParameter("id"));
Info info= dbex.findById(id);
out.clearBuffer();
response.setContentType("image/jpg");
byte[] photo=info.getPhoto();//photo为从数据库里取出的图片的字节数组
ByteArrayOutputStream imageStream=new ByteArrayOutputStream();
if(photo!=null){
imageStream.write(photo);
}
ServletOutputStream streamOut=response.getOutputStream();
imageStream.writeTo(streamOut);
streamOut.close();
%>