数据库中读取文件

======================================================================

JSP
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
</head>
<body>
文件下载
<a href="UpLoad">下载链接</a>
<a href="FileDb?id=1">数据库中下载文件</a>
</body>
</html>


======================================================================

servlet

package com.upload.db;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import po.UploadBean;

public class FileDb extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//取出前台的参数名
String id = request.getParameter("id");
//判断文件是否存在
if(id == null){
PrintWriter out = response.getWriter();
out.print("没有找到文件");
}
UpLoadDB uploaddb = new UpLoadDB();
Connection con = uploaddb.getConnection();//打开数据库的连接
//通过id的值把数据库中的字段的文件路径查出来
String sql = "select filename from file where id = '"+id+"'";
UploadBean upBean = new UploadBean();
try {
ResultSet rs = con.createStatement().executeQuery(sql);
while(rs.next()){
//将将结果集的对应数据放到相对的Bean里
upBean.setFileName(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
//对文件名进行中文的处理
String fileName = new String(upBean.getFileName().getBytes(),"gb2312").toString();
//取出文件名
String newFileName = (fileName.substring(fileName.lastIndexOf("\\"),fileName.lastIndexOf(".")));
int length = newFileName.length();
String tempName = newFileName.substring(1,length);
InputStream is = new FileInputStream(fileName);
response.reset();
response.addHeader("Content-Disposition","attachment; filename=\"" + newFileName + "\"");
byte[] temp = new byte[1000];
int len = 0;
//得到流
ServletOutputStream sos = response.getOutputStream();
String validate = fileName.substring(fileName.lastIndexOf("."));
System.out.println(validate);
//判断文件的相应类型
if(validate.equals(".txt")){
response.setContentType("text/plain");
}else if(validate.equals(".dot")){
response.setContentType("application/msword");
}else{
response.setContentType("image/jpeg;charset=GB2312");
}
while((len = is.read(temp))!=-1){
sos.write(temp, 0, len);
}
sos.flush();
sos.close();
is.close();
}
}


======================================================================

数据库的连接

package com.upload.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class UpLoadDB {

public static Connection conn = null;

public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/upload?useUnicode=true&characterEncoding=gb2312",
"root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

public void closeConnection(){

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值