Java使用servlet实现文件上传至数据库和从数据库下载文件

Java 发表评论

一、前台

1.上传页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="../UploadHandleServlet" enctype="multipart/form-data" method="post">
	上传文件1:<input type="file" name="file1">
	<input type="submit" value="提交">
</form>
</body>
</html>

2、下载页面原理同上

二、上传

package MainServlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import Bean.FlieDao;
import Bean.ImgDao;
import Bean.TB_Flie;
import Bean.TB_Img;

/**
 * Servlet implementation class UploadHandleServlet
 */
@WebServlet(name="UploadHandleServlet",urlPatterns="/UploadHandleServlet")
public class UploadHandleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	 //消息提示
	//private String massage="";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadHandleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到上传时生成的临时文件保存目录
		String tempPath=this.getServletContext().getRealPath("/WEB-INF/temp");
		/**
		 * 得到上传文件的保存目录的两种方法
		 * 方法1的目录生成在tomcat目录下,一旦tomcat清除项目,此文件就会消失,不为考虑
		 *1、String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
		 *方法2的AttFilePath位webxml里面配置的路径名称,此目录为固定硬盘目录,不会因为项目移除而消失,稳定可靠
		 *2、String savePath = this.getServletContext().getInitParameter("AttFilePath");
		 */		
		//得到上传文件的保存目录
		//String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
		//得到上传文件的保存目录
		String savePath = this.getServletContext().getInitParameter("AttFilePath");
		File tmpFile=new File(tempPath);
		//如果临时文件不存在,创建临时目录e
		if(!tmpFile.exists()){
			 tmpFile.mkdir();
		}
        String message = "";
        try{
		 //创建一个DiskFileItemFactory工厂
		DiskFileItemFactory factory=new DiskFileItemFactory();
		//设置缓冲区的大小为100KB,如果不指定,那么缓冲区的大小默认是10KB
		factory.setSizeThreshold(1024*100);
		//设置上传时生成的临时文件的保存目录
		factory.setRepository(tmpFile);
		//创建一个文件上传解析器
        ServletFileUpload upload = new ServletFileUpload(factory);
        //监听文件上传进度
        upload.setProgressListener(new ProgressListener(){
            public void update(long pBytesRead, long pContentLength, int arg2) {
                System.out.println("文件大小为:" + pContentLength + ",当前已处理:" + pBytesRead);
            }
        });
        
        //设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
        upload.setFileSizeMax(1024*1024);
        //设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为10MB
        upload.setSizeMax(1024*1024*10);
        //4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
        List<FileItem> list = upload.parseRequest(request);
        for(FileItem item : list){
            //如果fileitem中封装的是普通输入项的数据
            if(item.isFormField()){
                String name = item.getFieldName();
                //解决普通输入项的数据的中文乱码问题
                String value = item.getString("UTF-8");
                //value = new String(value.getBytes("iso8859-1"),"UTF-8");
                System.out.println(name + "=" + value);
            }else{
                //得到上传的文件名称,
                String filename = item.getName();
                System.out.println(filename);
                if(filename==null || filename.trim().equals("")){
                    continue;
                }
                //解决上传文件名的中文乱码
                upload.setHeaderEncoding("UTF-8"); 
                //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:  c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                //处理获取到的上传文件的文件名的路径部分,只保留文件名部分
                filename = filename.substring(filename.lastIndexOf("\\")+1);
                /**
                 * 将上传的文件保存到数据库
                 * @author baicai
                 * time上传时间
                 * filename文件名
                 * savePath文件路径
                 * */
    			Date date=new Date();
    		    DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm");
    			String time=format.format(date);
            	ImgDao imgDao=new ImgDao();
            	TB_Img tb_Img=new TB_Img(filename,savePath,time);
            	imgDao.UpImg(tb_Img);
            	
                //得到上传文件的扩展名
                String fileExtName = filename.substring(filename.lastIndexOf(".")+1);
                //如果需要限制上传的文件类型,那么可以通过文件的扩展名来判断上传的文件类型是否合法
                System.out.println("上传的文件的扩展名是:"+fileExtName);
                //获取item中的上传文件的输入流
                InputStream in = item.getInputStream();
                //得到文件保存的名称
                String saveFilename = makeFileName(filename);
                //得到文件的保存目录
                String realSavePath = makePath(saveFilename, savePath);
                //创建一个文件输出流
                FileOutputStream out = new FileOutputStream(realSavePath + "\\" + saveFilename);
                //创建一个缓冲区
                byte buffer[] = new byte[1024];
                //判断输入流中的数据是否已经读完的标识
                int len = 0;
                //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                while((len=in.read(buffer))>0){
                    //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                    out.write(buffer, 0, len);
                }
                //关闭输入流
                in.close();
                //关闭输出流
                out.close();
                //删除处理文件上传时生成的临时文件
                //item.delete();
                response.getWriter().println("<script type='text/javascript'>alert('上传失败!')</script>");
            }
        }
        }
        catch (FileUploadBase.FileSizeLimitExceededException e) {
	        e.printStackTrace();
	        request.setAttribute("message", "单个文件超出最大值!!!");
	        request.getRequestDispatcher("/message.jsp").forward(request, response);
	        return;
	    }catch (FileUploadBase.SizeLimitExceededException e) {
	        e.printStackTrace();
	        response.getWriter().println("<script type='text/javascript'>alert('上传文件的总的大小超出限制的最大值!!!')</script>");
	        return;
	    }catch (Exception e) {
	    	response.getWriter().println("<script type='text/javascript'>alert('上传失败!')</script>");
	        e.printStackTrace();
	    }
}
	 /**
	    * @Method: makeFileName
	    * @Description: 生成上传文件的文件名,文件名以:uuid+"_"+文件的原始名称
	    * @param filename 文件的原始名称
	    * @return uuid+"_"+文件的原始名称
	    */ 
	    private String makeFileName(String filename){  //2.jpg
	        //为防止文件覆盖的现象发生,要为上传文件产生一个唯一的文件名
	        return UUID.randomUUID().toString() + "_" + filename;
	    }
	    
	    /**
	     * 为防止一个目录下面出现太多文件,要使用hash算法打散存储
	    * @Method: makePath
	    * @Description:
	    *
	    * @param filename 文件名,要根据文件名生成存储目录
	    * @param savePath 文件存储路径
	    * @return 新的存储目录
	    */ 
	    private String makePath(String filename,String savePath){	    	
	    	//用日期得到文件名的
	    	Calendar date=Calendar.getInstance();		 
	 		SimpleDateFormat format1=new SimpleDateFormat( "yyyy-MM-dd"); 
	 		String name=format1.format(date.getTime());
	 		String dir = savePath + "\\" + name;  //upload\2\3  upload\3\5
	 		File file=new File(dir);
	 		//如果目录不存在
	        if(!file.exists()){
	            //创建目录
	            file.mkdirs();
	        }
	        return dir;
	    }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

三、下载

package MainServlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ImgDown
 */
@WebServlet(urlPatterns="/ImgDown",name="ImgDown")
public class ImgDown extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /*
     * @see HttpServlet#HttpServlet()
     */
    public ImgDown() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //得到要下载的文件名
        String fileName = request.getParameter("flieName");
        fileName = new String(fileName.getBytes("iso8859-1"),"UTF-8");
        //上传的文件都是保存在AttFilePath(D:/Code)目录下的子目录当中
        String savePath = request.getParameter("fliePath");
        //通过文件名找出文件的所在目录
        String path = findFileSavePathByFileName(fileName,savePath);
               //得到要下载的文件
        File file = new File(path + "\\" + fileName);
        //如果文件不存在
        if(!file.exists()){
        	request.setCharacterEncoding("utf-8");
        	response.getWriter().println("<script type='text/javascript'>alert('您要下载的资源被删除啦!')</script>");
        }
        //处理文件名
        String realname = fileName.substring(fileName.indexOf("_")+1);
        //设置响应头,控制浏览器下载该文件
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
        //读取要下载的文件,保存到文件输入流
        FileInputStream in = new FileInputStream(path + "\\" + fileName);
        //创建输出流
        OutputStream out = response.getOutputStream();
        //创建缓冲区
        byte buffer[] = new byte[1024];
        int len = 0;
        //循环将输入流中的内容读取到缓冲区当中
        while((len=in.read(buffer))>0){
            //输出缓冲区的内容到浏览器,实现文件下载
            out.write(buffer, 0, len);
        }
        //关闭文件输入流
        in.close();
        //关闭输出流
        out.close();
    }
    
    /*
    * @Method: findFileSavePathByFileName
    * @Description: 通过文件名和存储上传文件根目录找出要下载的文件的所在路径
    * @param filename 要下载的文件名
    * @param saveRootPath 上传文件保存的根目录,也就是/WEB-INF/upload目录
    * @return 要下载的文件的存储目录
    */
    public String findFileSavePathByFileName(String filename,String saveRootPath){
    	//用日期得到文件名的
    	Calendar date=Calendar.getInstance();		 
 		SimpleDateFormat format1=new SimpleDateFormat( "yyyy-MM-dd"); 
 		String name=format1.format(date.getTime());
 		String dir = saveRootPath + "\\" + name;
 		File file=new File(dir);
 		//如果目录不存在
        if(!file.exists()){
            //创建目录
            file.mkdirs();
        }
        return dir;
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

数据库部分

1、实体

package Bean;

public class TB_Img {
	private int id;
	private String flieName;
	private String fliePath;
	private String date;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFlieName() {
		return flieName;
	}
	public void setFlieName(String flieName) {
		this.flieName = flieName;
	}
	public String getFliePath() {
		return fliePath;
	}
	public void setFliePath(String fliePath) {
		this.fliePath = fliePath;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getDat() {
		return date;
	}
	public TB_Img(){}
	public TB_Img(int id,String flieName,String fliePath,String date){
		this.id=id;
		this.flieName=flieName;
		this.fliePath=fliePath;
		this.date=date;
	}
	public TB_Img(String flieName,String fliePath,String date){
		this.flieName=flieName;
		this.fliePath=fliePath;
		this.date=date;
	}
}

2,数据层

package Bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import SqlDB.SqlDB;

public class ImgDao {
	private Connection conn=SqlDB.getConnection();
	TB_Img tb_Img=null;
	List<TB_Img> tb_Imgs=null;
	/**
	 * 上传文件图片
	 * */
	public void UpImg(TB_Img tb_Img) throws SQLException{
		String sql="insert into Img(flieName,fliePath,date)values(?,?,?)";
		PreparedStatement ptmt=conn.prepareStatement(sql);
		ptmt.setString(1, tb_Img.getFlieName());
		ptmt.setString(2, tb_Img.getFliePath());
		ptmt.setString(3, tb_Img.getDat());
		ptmt.executeUpdate();
	}
	/**
	 *得到所有数据
	 * */
	public List<TB_Img> AllImg() throws SQLException{
		tb_Imgs=new ArrayList<>();
		String sql="select * from Img";
		PreparedStatement ptmt=conn.prepareStatement(sql);
		ResultSet rs=ptmt.executeQuery();
		while(rs.next()){
			tb_Img=new TB_Img();
			tb_Img.setId(rs.getInt("id"));
			tb_Img.setFlieName(rs.getString("flieName"));
			tb_Img.setFliePath(rs.getString("fliePath"));
			tb_Img.setDate(rs.getString("date"));
			tb_Imgs.add(tb_Img);
		}
		return tb_Imgs;
	}
	/**
	 *下载查看文件 
	 * */
	public List<TB_Img> FindImg(String context) throws SQLException{
		tb_Imgs=new ArrayList<>();
		String sql="select * from Img where flieName like '%"+context +"%' or fliePath like '%"+context +"%' or id like '%"+context +"%'";
		PreparedStatement ptmt=conn.prepareStatement(sql);
		ResultSet rs=ptmt.executeQuery();
		while(rs.next()){
			tb_Img=new TB_Img();
			tb_Img.setId(rs.getInt("id"));
			tb_Img.setFlieName(rs.getString("flieName"));
			tb_Img.setFliePath(rs.getString("fliePath"));
			tb_Imgs.add(tb_Img);
		}
		return tb_Imgs;
	}
}

3.链接数据库

package SqlDB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SqlDB {
	private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=Text";
	private static final String user= "sa";
	private static final String password="123456";
	 /** 
     * 获取连接 
     * @return   
     */  
    public static Connection getConnection(){
        try {  
            Class.forName(DRIVER);  
            Connection conn = DriverManager.getConnection(URL,user,password);  
            return conn;  
        } catch (SQLException e) {  
            System.out.println(e.getMessage()); 
        } 
        catch (ClassNotFoundException e) {  
            System.out.println(e.getMessage());
        }  
        
        return null;  
    }  
    /* 
     * 关闭数据库连接,注意关闭的顺序 
     */  
    public void close(ResultSet rs, PreparedStatement ps, Connection conn) {  
        if(rs!=null){  
            try{  
                rs.close();  
                rs=null;  
            }catch(SQLException e){  
                e.printStackTrace();  
            }  
        }  
        if(ps!=null){  
            try{  
                ps.close();  
                ps=null;  
            }catch(SQLException e){  
                e.printStackTrace();  
            }  
        }  
        if(conn!=null){  
            try{  
                conn.close();  
                conn=null;  
            }catch(SQLException e){  
                e.printStackTrace();  
            }  
        }  
    }  
} 

发表评论

邮箱地址不会被公开。 必填项已用*标注

昵称 *