`
qxmcool
  • 浏览: 90851 次
  • 性别: Icon_minigender_1
  • 来自: 石家庄
社区版块
存档分类
最新评论

数据库中读取文件

    博客分类:
  • jsp
阅读更多
======================================================================

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(){

}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics