首页 > 代码库 > springmvc和servlet下的文件上传和下载(存文件目录和存数据库Blob两种方式)

springmvc和servlet下的文件上传和下载(存文件目录和存数据库Blob两种方式)

项目中涉及了文件的上传和下载,以前在struts2下做过,今天又用springmvc做了一遍,发现springmvc封装的特别好,基本不用几行代码就完成了,下面把代码贴出来:


FileUpAndDown.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>using commons Upload to upload file</title>
</head>
<script type="text/javascript">
function downFile(){
	var fileId = document.getElementById("fileId").value;
	location.href="fileDownload?fileId=" + fileId;
}
</script>
<style>
* {
	font-family: "宋体";
	font-size: 14px
}
</style>
<body>
	<p align="center">文件上传下载</p>
	<form id="form1" name="form1" method="post" action="fileUpload" enctype="multipart/form-data">
		<table border="0" align="center">
			<tr>
				<td>上传文件:</td>
				<td><input name="file" type="file" size="20"></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" value="提交"> <input type="reset" name="reset" value="重置"></td>
			</tr>
		</table>
	</form>
	<div align="center">
	<input type="text" id="fileId"><input type="button" value="根据Id下载文件" onclick="javascript:downFile()">
	</div>
</body>
</html>

FileUpAndDownController.java

@RequestMapping(value = http://www.mamicode.com/"/fileUpload")>
FileUpAndDownMapper.xml(对应的数据库为db2,保存blob类型)

<?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="com.xx.persistence.FileUpAndDownMapper">

	<resultMap id="fileBean" type="com.xx.web.FileUpAndDown">
		<id column="ID" property="id" jdbcType="INTEGER" />
		<result column="FILENAME" property="fileName" jdbcType="VARCHAR" />
	    <result column="TESTA" property="testa" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
	    <result column="FILESTREAM" property="fileStream" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
	</resultMap>  
	
	<insert id="saveFileInfo" parameterType="java.util.HashMap">
	INSERT INTO BLOBTEST(FILENAME, FILESTREAM)
	VALUES(#{fileName}, #{byt, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler})
	</insert>

    <select id="getFileByPk" resultMap="fileBean" parameterType="int">
      SELECT * FROM BLOBTEST WHERE ID=${value}
     </select>
</mapper>

uploadResult.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>uploadResult</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
	<a href=http://www.mamicode.com/"fileManagePage">上传文件 ${requestScope['upload.message'] }>
以上为springmvc下上传文件的Demo,其中很关键的一步是,spring的配置文件中要加入文件上传的支持:

<!-- 支持上传文件 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

文件的保存做了两种方式,一种是直接存服务器的文件目录;另一种是把文件流存入数据库blob字段内(项目的特需要求)

下面是文件下载的代码:

@ResponseBody
	@RequestMapping(value = http://www.mamicode.com/"/fileDownload")>
springmvc下文件上传下载很简单明了吧?不必像servlet下那么多的代码。