随笔-23  评论-0  文章-5  trackbacks-0
 
package mypack;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class DownloadServlet extends HttpServlet { 
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { 
        OutputStream out; //输出响应正文的输出流 
        InputStream in; //读取本地文件的输入流 //获得filename请求参数 
        String filename=request.getParameter("filename"); 
        if(filename==null){ 
            out=response.getOutputStream(); 
            out.write("Please input filename.".getBytes()); 
            out.close();
            return; 
        } //创建读取本地文件的输入流 
     in= getServletContext().getResourceAsStream("/store/"+filename);
     int length=in.available(); //设置响应正文的MIME类型 
    response.setContentType("application/force-download"); 
    response.setHeader("Content-Length",String.valueOf(length)); 
    response.setHeader("Content-Disposition", "attachment;filename=\""+filename +"\" ");/** 把本地文件中的数据发送给客户 */ 
    out=response.getOutputStream(); 
    int bytesRead = 0; 
    byte[] buffer = new byte[512]; 
    while ((bytesRead = in.read(buffer)) != -1)    { 
        out.write(buffer, 0, bytesRead); 
    }
     in.close(); 
    out.close(); 
    }
}
posted @ 2010-12-03 22:04 ForMeBlog 阅读(205) | 评论 (0)编辑 收藏
如果你在你的数据库中存的Blob类型的图片,你想读取它并显示为图片。你可以先建一个showimage.jsp页面,如果你连接数据库的密码和用户名跟这里不一样,记得要改一下不然就会出错。
<%@ page contentType="text/html;charset=bg2312"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>显示数据库图片测试页</title>
</head>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:denglu","root","root");
Statement stmt=con.createStatement();
String sql=new String();
sql = "select id from picturenews";
ResultSet rs=stmt.executeQuery(sql);
//显示最后一条记录的图片
rs.last();
%>
<table>
<tr><td><img src='testimageout.jsp?id=<%=rs.getInt("id")%>'></td></tr>
</table>
</body>
</html> 
在建另一页面读取数据库中图片testimageout.jsp
<%@ page contentType="text/html; charset=gbk" %>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*, javax.sql.*" %>
<%@ page import="java.util.*"%>
<%@ page import="java.math.*"%>

<%
String photo_no = request.getParameter("photo_no");

//mysql连接 
Class.forName("com.mysql.jdbc.Driver").newInstance();
String URL="jdbc:mysql://localhost:3306/todream";
Connection con = DriverManager.getConnection(URL,"root","root");


try{
// 准备语句执行对象
Statement stmt = con.createStatement();

String sql = " SELECT * FROM todream_exhibition WHERE id = "+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob("workimg");
long size = b.length();
//out.print(size);
byte[] bs = b.getBytes(1, (int)size);
response.setContentType("image/jpeg"); 
OutputStream outs = response.getOutputStream(); 
outs.write(bs);
outs.flush();
rs.close(); 
}
else {
rs.close();
response.sendRedirect("./images/error.gif");
}
}
finally{
con.close();
}
%>
转载http://wenwen.soso.com/z/q114766705.htm  如果你想尝试你必须要将数据库建好,并与这一样或将jsp页面改了跟你的一样。
posted @ 2010-11-16 13:49 ForMeBlog 阅读(1198) | 评论 (0)编辑 收藏

今天是星期四,这一星期过的还是比较好的,过的很充分,每节课都听得进去。学习的东西基本上都会。昨天定了立个计划,现在也知道自己干什么啦!这星期过后可能就要考试了,不过我不是太紧张,因为我个人认为自己学的还会可以吧!每天学习10分钟10年之后你将会成为这方面的专家。

posted @ 2009-12-03 15:38 ForMeBlog 阅读(159) | 评论 (0)编辑 收藏
仅列出标题
共3页: 上一页 1 2 3