java实现图片上传至服务器并显示,如何做?希望要具体的代码实现
目标就是通过JSP页面让用户选择本地图片文件,提交就上传到服务器保存。上传成功的话JSP页面上就显示刚上传的图片。...
目标就是通过JSP页面让用户选择本地图片文件,提交就上传到服务器保存。上传成功的话JSP页面上就显示刚上传的图片。
展开
7个回答
展开全部
很简单。
可以手写IO读写(有点麻烦)。
怕麻烦的话使用FileUpload组件 在servlet里doPost嵌入一下代码
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
//设置保存上传文件的目录
String uploadDir =getServletContext().getRealPath("/up");
System.out.println(uploadDir);
if (uploadDir == null)
{
out.println("无法访问存储目录!");
return;
}
//根据路径创建一个文件
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()){
if(!fUploadDir.mkdir())//如果UP目录不存在 创建一个 不能创建输出...
{
out.println("无法创建存储目录!");
return;
}
}
if (!DiskFileUpload.isMultipartContent(request))
{
out.println("只能处理multipart/form-data类型的数据!");
return ;
}
DiskFileUpload fu = new DiskFileUpload();
//最多上传200M数据
fu.setSizeMax(1024 * 1024 * 200);
//超过1M的字段数据采用临时文件缓存
fu.setSizeThreshold(1024 * 1024);
//采用默认的临时文件存储位置
//fu.setRepositoryPath(...);
//设置上传的普通字段的名称和文件字段的文件名所采用的字符集编码
fu.setHeaderEncoding("gb2312");
//得到所有表单字段对象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);//解析request对象中上传的文件
}
catch (FileUploadException e)
{
out.println("解析数据时出现如下问题:");
e.printStackTrace(out);
return;
}
//处理每个表单字段
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField()){
String content = fi.getString("GB2312");
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}else{
try
{
String pathSrc = fi.getName();
if(pathSrc.trim().equals("")){
continue;
}
int start = pathSrc.lastIndexOf('\\');
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName);
fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}catch (Exception e){
out.println("存储文件时出现如下问题:");
e.printStackTrace(out);
return;
}
finally //总是立即删除保存表单字段内容的临时文件
{
fi.delete();
}
}
}
注意 JSP页面的form要加enctype="multipart/form-data" 属性, 提交的时候要向服务器说明一下 此页面包含文件。
如果 还是麻烦,干脆使用Struts 的上传组件 他对FileUpload又做了封装,使用起来更傻瓜化,很容易掌握。
-----------------------------
以上回答,如有不明白可以联系我。
可以手写IO读写(有点麻烦)。
怕麻烦的话使用FileUpload组件 在servlet里doPost嵌入一下代码
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
//设置保存上传文件的目录
String uploadDir =getServletContext().getRealPath("/up");
System.out.println(uploadDir);
if (uploadDir == null)
{
out.println("无法访问存储目录!");
return;
}
//根据路径创建一个文件
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()){
if(!fUploadDir.mkdir())//如果UP目录不存在 创建一个 不能创建输出...
{
out.println("无法创建存储目录!");
return;
}
}
if (!DiskFileUpload.isMultipartContent(request))
{
out.println("只能处理multipart/form-data类型的数据!");
return ;
}
DiskFileUpload fu = new DiskFileUpload();
//最多上传200M数据
fu.setSizeMax(1024 * 1024 * 200);
//超过1M的字段数据采用临时文件缓存
fu.setSizeThreshold(1024 * 1024);
//采用默认的临时文件存储位置
//fu.setRepositoryPath(...);
//设置上传的普通字段的名称和文件字段的文件名所采用的字符集编码
fu.setHeaderEncoding("gb2312");
//得到所有表单字段对象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);//解析request对象中上传的文件
}
catch (FileUploadException e)
{
out.println("解析数据时出现如下问题:");
e.printStackTrace(out);
return;
}
//处理每个表单字段
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField()){
String content = fi.getString("GB2312");
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}else{
try
{
String pathSrc = fi.getName();
if(pathSrc.trim().equals("")){
continue;
}
int start = pathSrc.lastIndexOf('\\');
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName);
fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}catch (Exception e){
out.println("存储文件时出现如下问题:");
e.printStackTrace(out);
return;
}
finally //总是立即删除保存表单字段内容的临时文件
{
fi.delete();
}
}
}
注意 JSP页面的form要加enctype="multipart/form-data" 属性, 提交的时候要向服务器说明一下 此页面包含文件。
如果 还是麻烦,干脆使用Struts 的上传组件 他对FileUpload又做了封装,使用起来更傻瓜化,很容易掌握。
-----------------------------
以上回答,如有不明白可以联系我。
展开全部
有两种方法一是用上传的组建jspSmartUpload的Request,
还有一种不用组建,但在form表单中不能加入ENCTYPE= "multipart/form-data "
我给你的案例吧
建立后台数据库
if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[p]
GO
CREATE TABLE [dbo].[p] (
[picid] [int] IDENTITY (1, 1) NOT NULL ,
[picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[pic] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
3.向数据库存储二进制图片
启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。
<%@ page contentType="text/html;charset=gb2312"%>
<%
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 'InputImage.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">
-->
</head>
<body>
<form action="testimage.jsp" method="POST"><br>
题目<input name="picname" type="text"><br>
图片<input name="pic" type="file"><br>
<input type="Submit" name="button1" value="提交"><br>
</form>
</body>
</html>
将此文件保存为InputImage.jsp文件,其中testimage.jsp文件是用来将图片数据存入数据库的,具体代码如下所示:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
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 'testimage.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">
-->
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
//建立Statement对象
String picname=request.getParameter("picname");
String pic=request.getParameter("pic");
//获得所要显示图片的标题、存储路径、内容,并进行中文编码
FileInputStream str=new FileInputStream(pic);
String sql="insert into p(picname,pic) values(?,?)";
PreparedStatement pstmt=conn.getPreparedStatement(sql);
pstmt.setString(1,picname);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//将数据存入数据库
out.println("Success,You Have Insert an Image Successfully");
%>
</body>
</html>
4. 网页中动态显示图片
接下来我们要编程从数据库中取出图片,其代码如下所示。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
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 'testimageout.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">
-->
</head>
<body>
<%
int id= Integer.parseInt(request.getParameter("picid"));
String sql = "select pic from p WHERE picid="+id;
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
ServletOutputStream sout = response.getOutputStream();
//图片输出的输出流
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
//将缓冲区的输入输出到页面
in.read(b);
}
sout.flush();
//输入完毕,清除缓冲
sout.close();
}
%>
</body>
</html>
将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用HTML标记:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
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 'lookpic.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">
-->
</head>
<body>
<%
String sql = "select * from p";
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
%>
<ccid_file values="testimageout" % />" width="100" height="100">
<br>
<%
}
rs.close();
%>
</body>
</html>
还有一种不用组建,但在form表单中不能加入ENCTYPE= "multipart/form-data "
我给你的案例吧
建立后台数据库
if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[p]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[p]
GO
CREATE TABLE [dbo].[p] (
[picid] [int] IDENTITY (1, 1) NOT NULL ,
[picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[pic] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
3.向数据库存储二进制图片
启动Dreamweaver MX后,新建一个JSP文件。其代码如下所示。
<%@ page contentType="text/html;charset=gb2312"%>
<%
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 'InputImage.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">
-->
</head>
<body>
<form action="testimage.jsp" method="POST"><br>
题目<input name="picname" type="text"><br>
图片<input name="pic" type="file"><br>
<input type="Submit" name="button1" value="提交"><br>
</form>
</body>
</html>
将此文件保存为InputImage.jsp文件,其中testimage.jsp文件是用来将图片数据存入数据库的,具体代码如下所示:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
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 'testimage.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">
-->
</head>
<body>
<%
request.setCharacterEncoding("gb2312");
//建立Statement对象
String picname=request.getParameter("picname");
String pic=request.getParameter("pic");
//获得所要显示图片的标题、存储路径、内容,并进行中文编码
FileInputStream str=new FileInputStream(pic);
String sql="insert into p(picname,pic) values(?,?)";
PreparedStatement pstmt=conn.getPreparedStatement(sql);
pstmt.setString(1,picname);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//将数据存入数据库
out.println("Success,You Have Insert an Image Successfully");
%>
</body>
</html>
4. 网页中动态显示图片
接下来我们要编程从数据库中取出图片,其代码如下所示。
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
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 'testimageout.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">
-->
</head>
<body>
<%
int id= Integer.parseInt(request.getParameter("picid"));
String sql = "select pic from p WHERE picid="+id;
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
ServletOutputStream sout = response.getOutputStream();
//图片输出的输出流
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
//将缓冲区的输入输出到页面
in.read(b);
}
sout.flush();
//输入完毕,清除缓冲
sout.close();
}
%>
</body>
</html>
将此文件保存为testimageout.jsp文件。下一步要做的工作就是使用HTML标记:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="conn" scope="page" class="dbconn.DBResult"/>
<%
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 'lookpic.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">
-->
</head>
<body>
<%
String sql = "select * from p";
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
%>
<ccid_file values="testimageout" % />" width="100" height="100">
<br>
<%
}
rs.close();
%>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己上apache找common-fileupload,此包可单独用,struts也把它整合了,官网上的例子简单明了,另外楼上那位的smartupload已经破产许久了...一上传大文件就卡死
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一步,在SQL-Server中创建表
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[picTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[picTable]
GO
CREATE TABLE [dbo].[picTable] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[pic] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
第二步:上传页面
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
index
</title>
</head>
<body bgcolor="#ffffff">
<form action="saveit.jsp" enctype="multipart/form-data" method="POST">
<input type="file" name="image" size="20" maxlength="20"/><p>
<input type="submit" value="上传"/>
</form>
<br>
<a href="servlet3">显示图片</a>
</body>
</html>
3.保存图片的jsp
<%@ page contentType="text/html; charset=GBK" import="upload.*"%>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<%
db.addPic(pageContext,application,out);
out.println("图片添加成功!");
%>
<br>
<a href="index.jsp">返回继续添加</a>
</a>
</body>
</html>
调用一个JavaBean中的addpic方法
package upload;
import com.jspsmart.upload.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class db {
private static Connection conn=null;
public db() {
}
public static void InitConnection(String dbName)
{
}
public static void addPic(PageContext pageContext,ServletContext application,JspWriter out) {
try
{
//将上传文件存入应用程序目录
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.setMaxFileSize(5*1024*1024);
su.upload();//到服务器的内存
com.jspsmart.upload.File f=su.getFiles().getFile(0);
String filename=f.getFileName();
filename=application.getRealPath("/")+filename;
f.saveAs(filename);
//通过流方式,读取文件,存入数据库中
java.io.File file=new java.io.File(filename);
FileInputStream fis=new FileInputStream(file);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strCon="jdbc:odbc:pubs";
conn=DriverManager.getConnection(strCon,"sa","");
Statement st=conn.createStatement();
String sql="insert into picTable (pic,type) values(?,?)";
//预编译命令
PreparedStatement ps=conn.prepareStatement(sql);
ps.setBinaryStream(1,fis,(int)file.length());
ps.setString(2,filename.substring(filename.lastIndexOf(".")+1));
ps.executeUpdate();
//删除文件
fis.close();
boolean isOK=file.delete();
if(!isOK)
{
System.out.println("文件删除失败");
}
ps.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void display(HttpServletResponse response) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strCon="jdbc:odbc:pubs";
conn=DriverManager.getConnection(strCon,"sa","");
Statement st=conn.createStatement();
//显示图片
String sql = "select [type],pic from picTable";
st=conn.createStatement();
ResultSet rs = st.executeQuery(sql);
response.reset();
while(rs.next())
{
String type = rs.getString(1);
//读取二进制流,将文件内容写入二进制文件
InputStream in = rs.getBinaryStream(2);
byte[] buffer =new byte[1444];
int byteread=0;
while ((byteread=in.read(buffer))!=-1)
{
response.getOutputStream().write(buffer,0,byteread);
}
}
response.getOutputStream().close();
rs.close();
}
}
显示图片调用display
Tip:记住别忘了导包:jspsmartupload 这个包。
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[picTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[picTable]
GO
CREATE TABLE [dbo].[picTable] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[pic] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
第二步:上传页面
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
index
</title>
</head>
<body bgcolor="#ffffff">
<form action="saveit.jsp" enctype="multipart/form-data" method="POST">
<input type="file" name="image" size="20" maxlength="20"/><p>
<input type="submit" value="上传"/>
</form>
<br>
<a href="servlet3">显示图片</a>
</body>
</html>
3.保存图片的jsp
<%@ page contentType="text/html; charset=GBK" import="upload.*"%>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<%
db.addPic(pageContext,application,out);
out.println("图片添加成功!");
%>
<br>
<a href="index.jsp">返回继续添加</a>
</a>
</body>
</html>
调用一个JavaBean中的addpic方法
package upload;
import com.jspsmart.upload.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
public class db {
private static Connection conn=null;
public db() {
}
public static void InitConnection(String dbName)
{
}
public static void addPic(PageContext pageContext,ServletContext application,JspWriter out) {
try
{
//将上传文件存入应用程序目录
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.setMaxFileSize(5*1024*1024);
su.upload();//到服务器的内存
com.jspsmart.upload.File f=su.getFiles().getFile(0);
String filename=f.getFileName();
filename=application.getRealPath("/")+filename;
f.saveAs(filename);
//通过流方式,读取文件,存入数据库中
java.io.File file=new java.io.File(filename);
FileInputStream fis=new FileInputStream(file);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strCon="jdbc:odbc:pubs";
conn=DriverManager.getConnection(strCon,"sa","");
Statement st=conn.createStatement();
String sql="insert into picTable (pic,type) values(?,?)";
//预编译命令
PreparedStatement ps=conn.prepareStatement(sql);
ps.setBinaryStream(1,fis,(int)file.length());
ps.setString(2,filename.substring(filename.lastIndexOf(".")+1));
ps.executeUpdate();
//删除文件
fis.close();
boolean isOK=file.delete();
if(!isOK)
{
System.out.println("文件删除失败");
}
ps.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void display(HttpServletResponse response) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String strCon="jdbc:odbc:pubs";
conn=DriverManager.getConnection(strCon,"sa","");
Statement st=conn.createStatement();
//显示图片
String sql = "select [type],pic from picTable";
st=conn.createStatement();
ResultSet rs = st.executeQuery(sql);
response.reset();
while(rs.next())
{
String type = rs.getString(1);
//读取二进制流,将文件内容写入二进制文件
InputStream in = rs.getBinaryStream(2);
byte[] buffer =new byte[1444];
int byteread=0;
while ((byteread=in.read(buffer))!=-1)
{
response.getOutputStream().write(buffer,0,byteread);
}
}
response.getOutputStream().close();
rs.close();
}
}
显示图片调用display
Tip:记住别忘了导包:jspsmartupload 这个包。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用FCKEditor多好,一个插件搞定一系列需求
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询