方法1、将图片存在数据库里
方法2、用session控制,如未登陆从定向到登陆页
解决方案 »
- <input type="button" 提交表單問題
- 新手问个程序中遇到的小问题
- 求救啊!找不到页面的问题!!!!!!!
- 系统汉化谁能帮帮忙(急!!!)
- 再问关于jxl的问题,大家有用过的请帮忙,郁闷快一个月了
- jsp浏览服务器上的文件
- 新手~请教TomCat5.5.20 + SQL2000 jsp访问数据库出错问题
- weblogic10 发布web程序报错
- 最后的问题,我的这个BEAN能在一个JSP中使用,但另一个JSP中就连import netoffice.*(BEAN所在包)都出错,说找不到此包,非常不解。
- jsp的前景???
- java.sql.SQLException: Unable to gather error information(用jdbc连Lotus报的错)
- jsp页面间的共享问题
直接键入文件地址下载没法用页面session控制呀
<%@ page contentType="text/html;charset=gb2312"%>
<html><head><title><center>添加图片</center></title></head>
<body> <center>添加图片<br/></center>
<form method="post" action="InsertImg.jsp">
员工编码:<input name="id2" size="10"> <br/>
图像ID:<input name="id" size="10">
<br/>
选择图像:<input type="file" name="image">
<br/>
<input type="submit" value="上传" name="submit" size="25">
<input type="reset" value="清除" name="clear" size="25">
<br/>
<A HREF="../../rsda/insert_img/ShowImg2.jsp">显示图片</A> <a href="../../../Noname6.rar">下载图片(单击右键下载)</a>
<HR>
</form>
2.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<html>
<body>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@10.10.10.155:1521:ycrz";
Connection con=DriverManager.getConnection(url,"rsgl","rsgl");
//插入数据库
//String sql="insert into test_img values (?,?,?)";
//获取传值ID
String ls="ss";
String id=request.getParameter("id");
//获取image的路径
String kk=request.getParameter("image");
//转换成file格式
File filename=new File(kk);
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改
FileInputStream str=new FileInputStream(filename);
String sql="insert into img(id,name,pic) values('"+id+"',?,?)";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,ls);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//out.println("Success,You Have Insert an Image Successfully");
out.println("上传图片成功!");
%>
3.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.jsp.*"%>
<html>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@10.0.5.36:1521:ycrz2";
Connection conn=DriverManager.getConnection(url,"rsgl","rsgl");
//其中mysql为你数据库的名字,user为你连接数据库的用户,password为你连接数据库用户的密码,可自己改
String yg_hm="1";
String sql = "select pic from img where id='"+yg_hm+"'";
Statement stmt=null;
ResultSet rs=null;
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException e){}
try {
while(rs.next()) {
response.reset();
//response.setContentType("image/jpeg");
response.setContentType("application/octet-stream");
ServletOutputStream sout = response.getOutputStream();
InputStream in = rs.getBinaryStream(1);
//byte b[] = new byte[0x7a120];
byte b[] = new byte[1024];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
in.read(b);
}
sout.flush();
sout.close();
}
}
catch(Exception e){System.out.println(e);}
%>
<div id="Layer1" style="position:absolute; left:190px; top:61px; width:306px; height:115px; z-index:1"><img name="yg_pic" src="" width="227" height="102" alt=""></div>
</body>
</html>
/*
可以在客户端用javascript判断
//javascript
//***********************
function getImageWidthHeight(w,h)
{
alert(img.width);
alert(img.height);
img.style.width=w;
img.style.height=h;
}<img src="**.gif" id="img" name="img" onload="setImageWidthHeight(100,200);">
(
将那个文件设为虚拟目录,这个目录名要用较长的不规则字符组合(这在Tomcat中设置),别人是怎么也猜不到的吧。要下载文件时,下载页面的链接做成图片链接,点击后重定向到执行下载任务的页面(记录下载信息的页面),下载任务的页面重定向到虚拟目录中的文件。
下载任务的页面是不会显示出来的,它会立即重定向到下载文件,弹出的下载框是没有下载完整的下载地址的,只会显示“文件名 来自 IP”。那么别人就不知到你的虚拟目录是哪个。除非下载的文件找不到,地址栏才显示地址。
另外,每隔一段时间修改一次虚拟目录名,像修改密码那样,那别人更猜不到啦。
但这个方法对图片来说不能奏效。
用流操作时怎样使输出的图片显示在web页面中一个表格的单元格中?