我在JSP里写了一个FILE 提交到另一个页面但得到的值为空,为什么啊,谁知道?
代码如下:大家帮忙运行一下,看看什么问题
POSTBLOB。JSP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form action="testblob.jsp" method="get" >
<table width="291" border="1">
<tr>
<td width="107">id </td>
<td width="168"><input name="id" type="text" /></td>
</tr>
<tr>
<td>file</td>
<td><input name="file" type="file" /></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="text" value="" name="good" /></td>
</tr>
</table>
</form>
</body>
</html>
TESTBLOB。JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<% String id=null;
String file=null;
id=request.getParameter("id");
file=request.getParameter("file");
String s= request.getParameter("good");
out.print(id);System.out.println(id);
out.print(file);System.out.println(file);
out.print(s);
// FileInputStream str=new FileInputStream(file);
// out.print(str.available());
// java.sql.Connection conn;
// java.lang.String strConn;
// Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// conn= java.sql.DriverManager.getConnection("jdbc:oracle:thin:@10.71.200.198:1521:test","ghx","123456");
// String sql="insert into test(id,pic) values(?,?)";
// PreparedStatement pstmt=conn.prepareStatement(sql);
// pstmt.setString(1,id);
// pstmt.setBinaryStream(2,str,str.available());
//pstmt.execute();
//out.println("Success,You Have Insert an Image Successfully");
// pstmt.close();
%>
<a href="readblob.jsp">查看图片</a>
<a href="postblob.html">返回</a>
</body>
</html>
READBLOB。JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<%
java.sql.Connection conn;
ResultSet rs=null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn= java.sql.DriverManager.getConnection("jdbc:oracle:thin:@10.71.200.198:1521:test","ghx","123456");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery("select * from mytest where id='1'");
if(rs.next())
{
Blob b = rs.getBlob("pic");
int size =(int)b.length();
out.print(size);
InputStream in=b.getBinaryStream();
byte[] by= new byte[size];
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
int bytesRead = 0;
while ((bytesRead = in.read(by)) != -1) {
sos.write(by, 0, bytesRead);
}
in.close();
sos.flush();
}
%>
</body>
</html>
代码如下:大家帮忙运行一下,看看什么问题
POSTBLOB。JSP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<form action="testblob.jsp" method="get" >
<table width="291" border="1">
<tr>
<td width="107">id </td>
<td width="168"><input name="id" type="text" /></td>
</tr>
<tr>
<td>file</td>
<td><input name="file" type="file" /></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td><input type="text" value="" name="good" /></td>
</tr>
</table>
</form>
</body>
</html>
TESTBLOB。JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<% String id=null;
String file=null;
id=request.getParameter("id");
file=request.getParameter("file");
String s= request.getParameter("good");
out.print(id);System.out.println(id);
out.print(file);System.out.println(file);
out.print(s);
// FileInputStream str=new FileInputStream(file);
// out.print(str.available());
// java.sql.Connection conn;
// java.lang.String strConn;
// Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// conn= java.sql.DriverManager.getConnection("jdbc:oracle:thin:@10.71.200.198:1521:test","ghx","123456");
// String sql="insert into test(id,pic) values(?,?)";
// PreparedStatement pstmt=conn.prepareStatement(sql);
// pstmt.setString(1,id);
// pstmt.setBinaryStream(2,str,str.available());
//pstmt.execute();
//out.println("Success,You Have Insert an Image Successfully");
// pstmt.close();
%>
<a href="readblob.jsp">查看图片</a>
<a href="postblob.html">返回</a>
</body>
</html>
READBLOB。JSP
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<%
java.sql.Connection conn;
ResultSet rs=null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn= java.sql.DriverManager.getConnection("jdbc:oracle:thin:@10.71.200.198:1521:test","ghx","123456");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery("select * from mytest where id='1'");
if(rs.next())
{
Blob b = rs.getBlob("pic");
int size =(int)b.length();
out.print(size);
InputStream in=b.getBinaryStream();
byte[] by= new byte[size];
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
int bytesRead = 0;
while ((bytesRead = in.read(by)) != -1) {
sos.write(by, 0, bytesRead);
}
in.close();
sos.flush();
}
%>
</body>
</html>
这里不对,FILE类型的数据不能使用这个方法获取的,它必须使用流的方式进行获取,建议看看那些上传组件的使用代码
用这个 可以查查这个的使用 FILE和别的HTML控件不同