SSM框架(三)文件上传与下载详解

目录

  • 一、运行效果
  • 二、思路流程
  • 三、功能实现
    • (一)项目结构
    • (二)实体
    • (三)配置
  • 四、界面及功能实现
    • (一)界面
    • (二)功能

一、运行效果

如果看运行效果,没有兴趣的话,就可以关掉这篇文章啦!

-》 主页登录-》登陆成功(返回用户名)选择你的操作
SSM框架(三)文件上传与下载详解_第1张图片 SSM框架(三)文件上传与下载详解_第2张图片
-》【1、文件上传(上传完成返回主页面)】【2、查看所有文件(支持下载功能)】
SSM框架(三)文件上传与下载详解_第3张图片 SSM框架(三)文件上传与下载详解_第4张图片
-》 点击实现下载功能:
在这里插入图片描述
数据库记录:(url是文件真实存储路径加上传时间标签重命名)
SSM框架(三)文件上传与下载详解_第5张图片
控制台记录:
SSM框架(三)文件上传与下载详解_第6张图片

二、思路流程

上传:
SSM框架(三)文件上传与下载详解_第7张图片
下载:
SSM框架(三)文件上传与下载详解_第8张图片
用户上传-》(名称、存储路径、任意格式转2进制格式)到数据库中,数据库记录本次操作
在这里插入图片描述
下载-》从数据中提出(名称、存储路径、2进制格式)、转换文本格式、http协议下载

三、功能实现

(一)项目结构

主要类的作用以及以上界面所用到的jsp,注意在webapp下新建了一个upload文件夹用于存放上传下载的文件:
SSM框架(三)文件上传与下载详解_第9张图片 SSM框架(三)文件上传与下载详解_第10张图片

(二)实体

mysql
-》建数据库ssm_mysql(UTF-8解析),
-》建表:file_info,
-》列:file_id(int,主键自增)、file_name(varchar)、file_url(varchar):
在这里插入图片描述
SSM框架(三)文件上传与下载详解_第11张图片
FileInfo.java:
在这里插入图片描述

package com.cungudafa.spingmvc01.bean;
/*
 * 文件类,对应数据库文件file_Info
 * file_id
 * file_name
 * file_url
 * */
public class FileInfo {
	private Integer fileId;
	private String fileName;
	private String fileUrl;
	public Integer getFileId() {
		return fileId;
	}
	public void setFileId(Integer fileId) {
		this.fileId = fileId;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFileUrl() {
		return fileUrl;
	}
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
}

(三)配置

  • pom.xml:(我的数据库为mysql8.0版本,引入的mysql8.0版本jar包)
    SSM框架(三)文件上传与下载详解_第12张图片

    		
    		
    		<dependency>
    			<groupId>mysqlgroupId>
          	    <artifactId>mysql-connector-javaartifactId>
            	<version>8.0.11version>
          	dependency>
    
  • sping-mvc.xml:(文件上传sping处理器)

    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    	
    	
    	<mvc:annotation-driven>mvc:annotation-driven>
    	
        
          
    	<context:component-scan base-package="com.cungudafa.spingmvc01" use-default-filters="false">
    		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    	context:component-scan>
    	
    	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">bean>
    	
    	
    	 
    	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	
    		<property name="prefix" value="/WEB-INF/">property>
    		<property name="suffix" value=".jsp">property>
    	bean>
    beans>
    
  • DBManager.java(提供对数据库连接)
    在这里插入图片描述

    package com.cungudafa.spingmvc01.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DBManager {
        public static String dbUrl="jdbc:mysql://localhost:3306/ssm_mysql?useSSL=false&serverTimezone=UTC";
        public static String dbUserName="root";
        public static String dbPassword="wy123456";
        public static String CLASSNAME="com.mysql.cj.jdbc.Driver";
    	/**
    	 * 静态加载驱动
    	 */
    	static{
    		try {
    			Class.forName(CLASSNAME);
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	/**
    	 * 获取对数据库的连接
    	 */
        public static Connection getConnection() throws SQLException {
        	Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
            return con;
        }
        public static void closeConnection(Connection con) {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

四、界面及功能实现

(一)界面

SSM框架(三)文件上传与下载详解_第13张图片

页面名称 作用 操作名称 操作结果
index.jsp 登录界面 dologin. do -》user.jsp
user.jsp 登录成功显示,提供可选操作入口 upload. do、files. do -》upload.jsp上传、files.jsp下载页面
upload.jsp 上传文件界面 doUpload. do 上传文件到tomcat中,操作记录在数据库
files.jsp 所有文件显示界面、下载界面 download. do 下载操作

index.jsp:(注意第3-7句是避免绝对路径和相对路径带来的干扰,使用直接在action="${basePath }dologin.do"加上${bashPath}即可指定当前路径)
SSM框架(三)文件上传与下载详解_第14张图片

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
session.setAttribute("basePath", basePath);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
	<h1>Helloworldh1>
	<form action="${basePath }dologin.do" method="post">
		<input type="text" name="userName"/><br/>
		<input type="text" name="userPassword"/><br/>
		<input type="submit" value="添加"/>
	form>
body>
html>

user.jsp:
SSM框架(三)文件上传与下载详解_第15张图片

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
session.setAttribute("basePath", basePath);
%>

<html>
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>success!title>
  head>
  <body>
    <div class="alert alert-success" role="alert">successdiv>
    	欢迎您,${user.userName }<br>
    <h3>请选择你的操作:h3>
	<a href="${basePath }file/upload.do">文件上传a><br>
	<a href="${basePath }file/files.do">查看所有文件a><br>

 body>
html>

login.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
session.setAttribute("basePath", basePath);
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<h3>用户登陆h3>
<form action="${basePath }dologin.do" method="post">
	<input type="text" name="userName">
	<button type="submit">登陆button>
form>
body>
html>

upload.jsp:
SSM框架(三)文件上传与下载详解_第16张图片

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
session.setAttribute("basePath", basePath);
%>

<html>
<head>
<meta charset="UTF-8">
<title>File Upload Testtitle>
head>
<body>
	<h1>文件上传h1>
	<form action="${basePath }file/doUpload.do" enctype="multipart/form-data" method="post">
		<input type="file" name="uploadFile">
		<button type="submit">上传button>
	form>
body>
html>

files.jsp:
SSM框架(三)文件上传与下载详解_第17张图片

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
session.setAttribute("basePath", basePath);
%>


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<h3>所有文件h3>
<table>
	<c:forEach items="${list }" var="f">
		<tr>
			<td>${f.fileId }td>
			<td>${f.fileName }td>
			<td><a href="${basePath }file/download.do?fileId=${f.fileId }">下载a>td>
		tr>
	c:forEach>
table>

body>
html>

(二)功能

SSM框架(三)文件上传与下载详解_第18张图片

  • FileInfo.java
    前面给出了,这里不再重复;
    SSM框架(三)文件上传与下载详解_第19张图片
  • FileUtil.java:文件处理函数
package com.cungudafa.spingmvc01.util;

import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件操作的工具类
 * @author Administrator
 *
 */
public class FileUtil {

	/**
	 * 生成文件上传的时间戳,用于文件保存在服务器的路径的一部分
	 * @return
	 */
	public static String createFileTimestamp(){
		Date date = new Date();
		long time = date.getTime();
		return String.valueOf(time);
	}
	
	public static void writeFileToUrl(MultipartFile file,String fileUrl) throws IOException{
		FileOutputStream fos = new FileOutputStream(new File(fileUrl));
		fos.write(file.getBytes());
		fos.flush();
		fos.close();
	}
	
//	public static byte[] readFileFromUrl(String fileUrl) throws FileNotFoundException{
//		FileInputStream fis = new FileInputStream(new File(fileUrl));
//	}
	
	public static void main(String[] args) {
		System.out.println(FileUtil.createFileTimestamp());
	}
	
}


  • FileInfoDao.java:主要有3种方式接口
package com.cungudafa.spingmvc01.dao;

import java.util.List; 
import java.sql.SQLException;

import com.cungudafa.spingmvc01.bean.FileInfo;

/**
 * DataAccessObject数据访问对象
 * @author Administrator
 *
 */
public interface FileInfoDao {
	/**
	 * 添加文件
	 * @param fileInfo
	 */
	public void addFileInfo(FileInfo fileInfo) throws SQLException;
	/**
	 * 查询所有的文件
	 * @return
	 */
	public List<FileInfo> findFiles() throws SQLException;
	/**
	 * 根据id查询文件
	 * @param fileId
	 * @return
	 */
	public FileInfo findFileById(Integer fileId) throws SQLException;
}

  • FileInfoDaoImpl.java:实例操作、sql语句
package com.cungudafa.spingmvc01.dao.impl;

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 com.cungudafa.spingmvc01.bean.FileInfo;
import com.cungudafa.spingmvc01.dao.FileInfoDao;
import com.cungudafa.spingmvc01.util.DBManager;

public class FileInfoDaoImpl implements FileInfoDao{

	@Override
	public void addFileInfo(FileInfo fileInfo) throws SQLException {
		String sql = "insert into file_info(file_name,file_url) values(?,?)";
		//获得连接
		Connection con = DBManager.getConnection();
		//获得PreparedStatement
		PreparedStatement pstm = con.prepareStatement(sql);
		//给问号赋值
		pstm.setString(1, fileInfo.getFileName());
		pstm.setString(2, fileInfo.getFileUrl());
		//执行sql
		pstm.executeUpdate();
		//关闭连接
		pstm.close();
		con.close();
	}

	@Override
	public List<FileInfo> findFiles() throws SQLException {
		String sql = "select * from file_info";
		Connection con = DBManager.getConnection();
		PreparedStatement pstm = con.prepareStatement(sql);
		//执行查询,返回结果集
		ResultSet rs = pstm.executeQuery();
		List<FileInfo> files = new ArrayList<>();
		while(rs.next()){//循环遍历结果集
			//file_id
			Integer fileId = rs.getInt(1);
			//file_name
			String fileName = rs.getString(2);
			//file_url
			String fileUrl = rs.getString(3);
			//每一条记录封装到一个FileInfo对象中
			FileInfo fileInfo = new FileInfo();
			fileInfo.setFileId(fileId);
			fileInfo.setFileName(fileName);
			fileInfo.setFileUrl(fileUrl);
			//将fileInfo对象放入集合中
			files.add(fileInfo);
		}
		pstm.close();
		con.close();
		return files;
	}

	@Override
	public FileInfo findFileById(Integer fileId) throws SQLException {
		String sql = "select * from file_info where file_id = ?";
		Connection con = DBManager.getConnection();
		PreparedStatement pstm = con.prepareStatement(sql);
		pstm.setInt(1, fileId);
		//执行查询,返回结果集
		ResultSet rs = pstm.executeQuery();
		FileInfo fileInfo = null;
		if(rs.next()){//如果结果集中有下一条记录
			fileInfo = new FileInfo();
			//file_id
			fileId = rs.getInt(1);
			//file_name
			String fileName = rs.getString(2);
			//file_url
			String fileUrl = rs.getString(3);
			fileInfo.setFileId(fileId);
			fileInfo.setFileName(fileName);
			fileInfo.setFileUrl(fileUrl);
		}
		pstm.close();
		con.close();
		return fileInfo;
	}
}
  • FileController.java:整个前端页面对后端请求的操作,注意这里封装了上传和下载的操作:
    上传部分:
    SSM框架(三)文件上传与下载详解_第20张图片
    下载部分:
    SSM框架(三)文件上传与下载详解_第21张图片
package com.cungudafa.spingmvc01.controller;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.cungudafa.spingmvc01.bean.FileInfo;
import com.cungudafa.spingmvc01.dao.FileInfoDao;
import com.cungudafa.spingmvc01.dao.impl.FileInfoDaoImpl;
import com.cungudafa.spingmvc01.util.FileUtil;

/**
 * 处理文件的类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/file")
public class FileController {
	
	@RequestMapping("/uploadtest")
	public String upload(MultipartFile uploadFile) throws IOException{
		System.out.println("uploadFile = " + uploadFile);
		//获得上传字节数
		byte[] buf = uploadFile.getBytes();
		System.out.println("上传文件大小:" + buf.length);
		//获得上传文件名称
		System.out.println("上传文件名称getName = " + uploadFile.getName());
		System.out.println("上传文件名称getOriginalFilename = " 
		+ uploadFile.getOriginalFilename());
		return "index";
	}
	
	@RequestMapping("/upload")
	public String toUpload(){
		return "upload";
	}
	
	@RequestMapping("/files")
	public String files(HttpSession session){
		FileInfoDao dao = new FileInfoDaoImpl();
		try {
			List<FileInfo> files = dao.findFiles();
			session.setAttribute("list", files);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "files";
	}
	
	@RequestMapping("/doUpload")
	public String upload(MultipartFile uploadFile,HttpServletRequest request) throws IOException{
		System.out.println("uploadFile = " + uploadFile);
		//获得文件
		byte[] buf = uploadFile.getBytes();
		System.out.println("文件长度" + buf.length);
		//文件名
		System.out.println("文件名getName = " + uploadFile.getName());
		System.out.println("文件名getOriginalFilename = " 
		+ uploadFile.getOriginalFilename());
		
		//文件名    a.txt
		String originalFileName = uploadFile.getOriginalFilename();
		//时间戳
		String time = FileUtil.createFileTimestamp();
		//文件url		/upload/1231231231231a.txt
		String fileUrl = "/upload/" + time + originalFileName;
		fileUrl = request.getServletContext().getRealPath(fileUrl);
		System.out.println("fileUrl = " + fileUrl);
		//向url地址存储文件
		FileUtil.writeFileToUrl(uploadFile, fileUrl);
		
		//向数据库中保存文件信息
		FileInfo fileInfo = new FileInfo();
		fileInfo.setFileName(originalFileName);
		fileInfo.setFileUrl(fileUrl);
		
		FileInfoDao dao = new FileInfoDaoImpl();
		try {
			dao.addFileInfo(fileInfo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "user";
	}
	
	@RequestMapping("/download")
	public ResponseEntity<byte[]> download(Integer fileId){
		FileInfoDao dao = new FileInfoDaoImpl();
		try {
			FileInfo fileInfo = dao.findFileById(fileId);
			String fileUrl = fileInfo.getFileUrl();
			String fileName = fileInfo.getFileName();
			HttpHeaders headers = new HttpHeaders();
			headers.setContentDispositionFormData("attachment", fileName);
			headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
			
			
			ResponseEntity<byte[]> entity = 
					new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(fileUrl)), headers,HttpStatus.CREATED);
			return entity;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}

ok,运行结果如最前面说述!


附:
源码下载地址:https://download.csdn.net/download/cungudafa/11186829

你可能感兴趣的:(SSM,Java前后端开发学习)