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

一、运行效果

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

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

二、思路流程

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

三、功能实现

(一)项目结构

主要类的作用以及以上界面所用到的jsp,注意在webapp下新建了一个upload文件夹用于存放上传下载的文件:
在这里插入图片描述 在这里插入图片描述

(二)实体

mysql
-》建数据库ssm_mysql(UTF-8解析),
-》建表:file_info,
-》列:file_id(int,主键自增)、file_name(varchar)、file_url(varchar):
在这里插入图片描述
在这里插入图片描述
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包)
    在这里插入图片描述

    		<!-- MySQL相关包 -->
    		<!-- dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.26</version>
    		</dependency-->
    		<dependency>
    			<groupId>mysql</groupId>
          	    <artifactId>mysql-connector-java</artifactId>
            	<version>8.0.11</version>
          	</dependency>
    
  • sping-mvc.xml:(文件上传sping处理器)

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    	
        <!-- 配置SpringMVC -->
        <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->  
    	<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>
    	
    	<!-- JSP视图 -->
    	<!-- 定义跳转的文件的前后缀 ,视图模式配置--> 
    	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<!-- controller中return时会自动加上前缀和后缀,变成一个完整的url -->
    		<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();
            }
        }
    }
    
    

四、界面及功能实现

(一)界面

在这里插入图片描述

页面名称作用操作名称操作结果
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}即可指定当前路径)
在这里插入图片描述

<%@ 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 here</title>
</head>
<body>
	<h1>Helloworld</h1>
	<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:
在这里插入图片描述

<%@ 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);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <!-- Required meta tags -->
    <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">success</div>
    	欢迎您,${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);
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</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:
在这里插入图片描述

<%@ 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);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Upload Test</title>
</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:
在这里插入图片描述

<%@ 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);
%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</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>

(二)功能

在这里插入图片描述

  • FileInfo.java
    前面给出了,这里不再重复;
    在这里插入图片描述
  • 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:整个前端页面对后端请求的操作,注意这里封装了上传和下载的操作:
    上传部分:
    在这里插入图片描述
    下载部分:
    在这里插入图片描述
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

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值