旅游管理系统:文件的上传和下载

时间:2022-10-23 14:46:27

Spring中文件的上传与下载

1.文件上传业务分析

1)将文件上传到服务器,然后存储到服务器的某个位置.
2)将已上传的文件相关信息存储到数据库.例如文件名,
文件大小,文件摘要信息,文件在服务器上的地址等.

2.SSM架构中文件上传的实现?
1)添加文件上传依赖?

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>

2)spring-mvc中添加文件上传解析配制?

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>

3)设置文件上传表单?
a)post请求
b)enctype=”multipart/form-data”

例如:
<form action="" method="post"
enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="upfile"/>
</form>

4)Spring controller中数据的接收?
在spring中可以借助此MultipartFile类型
的对象接收页面表单中上传的数据.

例如

public JsonResult doUpload(String title,
MultipartFile upfile){
…..
}

Spring中文件的下载

1.设置下载时的响应头(必须设置,固定件名可能有乱码,通过如下语句对文件名编码

 response.setContentType("appliction/octet-stream");
File fileName=new String(fileName.getBytes("iso-8859-1"),"utf-8");
String fileName=URLEncoder.encode(
a.getFileName(),"utf-8");
response.setHeader("Content-disposition",
"attachment;filename="+fileName);

2.返回要下载数据,交给浏览器下载
根据文件路径构建一个Path
Path path=Paths.get(a.getFilePath());
读取指定路径下的文件字节
return Files.readAllBytes(path);

实例

1 实体

package cn.tedu.ttms.attachment.entity;

import java.io.Serializable;
import java.util.Date;
/**附件实体对象*/
public class Attachment implements Serializable{
private static final long serialVersionUID = -2066834040491586051L;
private Integer id;
/**附件标题*/
private String title;
/**文件名称(实际的文件名)*/
private String fileName;
/**文件内容类型*/
private String contentType;
/**文件路径(实际存储的路径)*/
private String filePath;
/**文件摘要信息(一般为MD5对内容加密以后的一个结果)*/
private String fileDisgest;
/**归属类型(例如附件属于哪个对象的附件,是产品的还是分销商)*/
private Integer athType;
/**具体归属对象(例如具体归属哪个产品)*/
private Integer belongId;

private Date createdTime;
private Date modifiedTime;
private String createdUser;
private String modifiedUser;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileDisgest() {
return fileDisgest;
}
public void setFileDisgest(String fileDisgest) {
this.fileDisgest = fileDisgest;
}
public Integer getAthType() {
return athType;
}
public void setAthType(Integer athType) {
this.athType = athType;
}
public Integer getBelongId() {
return belongId;
}
public void setBelongId(Integer belongId) {
this.belongId = belongId;
}
public Date getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
public Date getModifiedTime() {
return modifiedTime;
}
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
}
public String getCreatedUser() {
return createdUser;
}
public void setCreatedUser(String createdUser) {
this.createdUser = createdUser;
}
public String getModifiedUser() {
return modifiedUser;
}
public void setModifiedUser(String modifiedUser) {
this.modifiedUser = modifiedUser;
}

}

2 Dao与Mapper

public interface AttachmentDao {
int insertObject(Attachment entity);
/**根据摘要信息获取记录数*/
int getRowCountByDigest(String fileDisgest);
/**获得所有上传的文件信息*/
List<Attachment> findObjects();
/**根据id查找某个对象*/
Attachment findObjectById(Integer id);

}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tedu.ttms.attachment.dao.AttachmentDao">
<!-- 根据摘要统计记录个数,
结果为0则表示没有找到对应摘要的记录 -->

<select id="getRowCountByDigest"
resultType="int">

select count(*)
from tms_attachements
where fileDisgest=#{fileDisgest}
</select>

<select id="findObjects"
resultType="cn.tedu.ttms.attachment.entity.Attachment">

select *
from tms_attachements;
</select>

<select id="findObjectById"
parameterType="integer"
resultType="cn.tedu.ttms.attachment.entity.Attachment">

select *
from tms_attachements
where id=#{id}
</select>
<insert id="insertObject"
parameterType="cn.tedu.ttms.attachment.entity.Attachment">

insert into tms_attachements(
title,
fileName,
contentType,
filePath,
fileDisgest,
athType,
belongId,
createdUser,
modifiedUser,
createdTime,
modifiedTime
)values(
#{title},
#{fileName},
#{contentType},
#{filePath},
#{fileDisgest},
#{athType},
#{belongId},
#{createdUser},
#{modifiedUser},
NOW(),
NOW()
);
</insert>

</mapper>

Service与实现类

@Service
public class AttachmentServiceImpl implements AttachmentService {
@Autowired
private AttachmentDao attachmentDao;
/**实现文件上传
* 1)将文件存储到服务器
* 2)将文件信息存储到数据库
* */

@Override
public Attachment findObjectById(Integer id) {
if(id==null)
throw new ServiceException("id不能为空");
Attachment a=attachmentDao.findObjectById(id);
if(a==null)
throw new ServiceException("对象已经不存在");
return a;
}
@Override
public void saveOject(String title,
MultipartFile mFile) {
//1.验证参数的有效性
if(StringUtils.isEmpty(title))
throw new ServiceException("title 不能为空");
if(mFile==null)
throw new ServiceException("请选择上传文件");
if(mFile.isEmpty())
throw new ServiceException("不允许上传空文件");
//2.判定文件是否已上传(根据摘要信息)
//2.1)根据mFile内容生成摘要信息(MD5)
String digest=null;
try{
byte []bytes=mFile.getBytes();
digest=//摘要字符串
DigestUtils.md5DigestAsHex(bytes);
}catch(Exception e){
e.printStackTrace();
throw new ServiceException("文件上传失败");
}
//2.2)根据摘要信息进行数据库查询
int count=
attachmentDao.getRowCountByDigest(digest);
//2.3)根据查询的结果判定文件是否已上传
if(count>0)
throw new ServiceException("文件已经上传");
//3.假如文件不在则上传文件
SimpleDateFormat sdf=
new SimpleDateFormat("yyyy/MM/dd");
String dateDir=sdf.format(new Date());
File fileDir=new File("e:/uploads/"+dateDir);
//判定目录是否存在,不存在则创建
if(!fileDir.exists())fileDir.mkdirs();
//构建文件对象(fileDir为目录,mFile.getOriginalFilename()文件名)
File dest=new File(fileDir,
mFile.getOriginalFilename());
try{
//上传文件
mFile.transferTo(dest);
}catch(Exception e){
e.printStackTrace();
throw new ServiceException("文件上传失败");
}
//4.将文件信息保存到数据库
Attachment a=new Attachment();
a.setTitle(title);
a.setFileName(mFile.getOriginalFilename());
a.setFileDisgest(digest);
a.setFilePath(dest.getPath());
a.setContentType(mFile.getContentType());
int rows=attachmentDao.insertObject(a);
//5.验证保存结果
if(rows<=0)
throw new ServiceException("数据保存失败");
}
@Override
public List<Attachment> findObjects() {
return attachmentDao.findObjects();
}

}

controller

@RequestMapping("/attachment/")
@Controller
public class AttachmentController {
@Autowired
private AttachmentService attachmentService;
@RequestMapping("attachmentUI")
public String listUI(){
return "attachment/attachment";
}
@RequestMapping("doUpload")
@ResponseBody
public JsonResult doUpload(String title,
MultipartFile mFile)throws IOException{
attachmentService.saveOject(title, mFile);
return new JsonResult("upload ok");
}
@RequestMapping("doFindObjects")
@ResponseBody
public JsonResult doFinObjects(){
List<Attachment> attchements=
attachmentService.findObjects();
return new JsonResult(attchements);
}
@RequestMapping("doDownload")
@ResponseBody
public byte[] doDownload(Integer id,
HttpServletResponse response) throws IOException{
//1.根据id获得对象
Attachment a=
attachmentService.findObjectById(id);
//2.设置下载内容类型以及响应头(固定格式)
response.setContentType("appliction/octet-stream");
//File fileName=new String(fileName.getBytes("iso-8859-1"),"utf-8");
//中文文件名可能有乱码,通过如下语句对文件名编码
String fileName=URLEncoder.encode(
a.getFileName(),"utf-8");
response.setHeader("Content-disposition",
"attachment;filename="+fileName);
//根据文件路径构建一个Path
Path path=Paths.get(a.getFilePath());
//读取指定路径下的文件字节
return Files.readAllBytes(path);
}

}

页面

<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="basePath" value="${pageContext.request.contextPath}"></c:set>
<script type="text/javascript" src="${basePath}/ttms/attach/attach.js"></script>
<script type="text/javascript" src="${basePath}/ttms/common/page.js"></script>
<div class="container">
<!-- 页面导航 -->
<div class="page-header">
<div class="page-title" style="padding-bottom: 5px">
<ol class="breadcrumb">
<li class="active">产品附件信息管理</li>
</ol>
</div>
<div class="page-stats"></div>
</div>
<form method="post" id="uploadFormId"
enctype="multipart/form-data">

<!-- 查询表单 -->
<div class="row page-search">
<div class="col-md-12">
<ul class="list-unstyled list-inline">
<li><input type="text" name="title" class="form-control"placeholder="附件标题"></li>
<li><input type="file" name="mFile" class="form-control"></li>
<li class='O1'><button type="button" class="btn btn-primary btn-upload" >上传</button></li>
</ul>
</div>
</div>
<!-- 列表显示内容 -->
<div class="row col-md-12">
<table class="table table-bordered">
<thead>
<tr>
<th>标题</th>
<th>文件名</th>
<th>文件类型</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tbodyId"></tbody>
</table>
</div>
</form>
</div>

页面对应JS

$(document).ready(function(){
$("#uploadFormId")
.on("click",".btn-upload",doUpload)
.on("click",".btn-down",doDownload)
doGetObjects();
});
function doDownload(){
var id=$(this).parent().parent().data("id");
var url="attachment/doDownload.do?id="+id;
document.location.href=url;
}
function doGetObjects(){
var url="attachment/doFindObjects.do";
$.getJSON(url,function(result){
if(result.state==1){
setTableBodyRows(result.data);
}else{
alert(result.message);
}
});
}
function setTableBodyRows(list){
var tBody=$("#tbodyId");tBody.empty();
for(var i in list){
var tr=$("<tr></tr>");
tr.data("id",list[i].id);
tr.append("<td>"+list[i].title+"</td>");
tr.append("<td>"+list[i].fileName+"</td>");
tr.append("<td>"+list[i].contentType+"</td>");
tr.append('<td><button type="button" class="btn btn-default btn-down">down</button></td>')
tBody.append(tr);
}
}
/*点击文件上传按钮执行此函数*/
function doUpload(){
//异步提交表单($.ajaxSubmit为异步提交表单)
//使用此函数时需要在页面引入(jquery.form.js )
$("#uploadFormId").ajaxSubmit({
type:"post",
url:"attachment/doUpload.do",
dataType:"json",
success:function(result){
alert(result.message);
doGetObjects();
}
});
//$("#uploadFormId").resetForm();
return false;//防止表单重复提交的一种方式
}