FileUpAndDownController.java
package com.xx.persistence;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUpAndDownController {
@RequestMapping(value = "/fileUpload")
public String upload(
@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, ModelMap model) throws IOException {
Map<String, Object> insertMap = new HashMap<String, Object>();
insertMap.put("byt", file.getBytes());
insertMap.put("fileName", file.getOriginalFilename());
int flag = fileUpAndDownMapper.saveFileInfo(insertMap);
if(flag > 0)
model.addAttribute("upload.message", "success");
else
model.addAttribute("upload.message", "failure");
return "/core/param/businessparam/uploadResult";
}
}
FileUpAndDownMapper.xml
<?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>
xml加的
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
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>
fileUpload.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="fileManagePage">上传文件</a> ${requestScope['upload.message'] }
</body>
</html>
你这个fileUpAnddownMapper是哪个来的?这个根本就没定义,编辑器肯定报错嘛,如果是类的话,请注意大小写,
没有注入fileUpAnddownMapper这个类啊,看看配置文件 或者@autowired
fileUpAnddownMapper在哪儿定义的,把整个类贴出来看看
没有这个类,所以我不知道怎么弄诶,我想的是上传文件到数据库中,以二进制数据流保存到数据库中,这是网上找的,不知道怎么写,求大神指教
这里有一篇文章,应该可以解决你的问题。
https://blog.csdn.net/u014475796/article/details/49893261
大致思路:
@Serivice
public class FileUpAnddownMapper {
public int saveFileInfo(Map parm) {
String fileName=(String)parm.get("fileName");
Byte[] byts=(Byte[]) (parm.get("byt"));
//插入的sql语句
String sql=("INSERT INTO ... ");
。。。。
return 1;
}
}
没有这个类 , 这个类 ,是一个 Mybaties 的Mapper 接口应该 , 不建议看这种没有什么大用处的代码 , txt 文件放到文件系统里 , 不比数据库里要快更多, 楼主还是抓住时间看别的吧
我理解那个地方应该就是他录入数据库的部分,表结构应该为一个包含下面两个列和类型的表名称为test1(举例)
byt blob
fileName varchar
你定义好表之后通过mybatisGenerator生成对应的mapper。这样的话直接可以在最上面使用
@Autowired
private Test1Mapper test1Mapper;
报错的地方改为
Test1 record = new Test1();
record.setByt(file.getBytes());
record.setFilename(file.getOriginalFileName());
int flag = test1Mapper.insert()
namespace="com.xx.persistence.FileUpAndDownMapper" 这个命名空间指向的是你的Dao层接口
在com.xx.persistence这个包下面创建一个FileUpAndDownMapper 接口
并且接口中的方法名参数返回值对应你的XML文件的方法。
例如insert方法在接口中
void saveFileInfo(Map map)
在你的FileUpAndDownController 类里面声明
@Autowired
private FileUpAndDownMapper downMapper;
package com.xx.persistence;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUpAndDownController {
@RequestMapping(value = "/fileUpload")
public String upload(
@RequestParam(value = "file", required = false) MultipartFile file,
HttpServletRequest request, ModelMap model) throws IOException {
Map<String, Object> insertMap = new HashMap<String, Object>();
insertMap.put("byt", file.getBytes());
insertMap.put("fileName", file.getOriginalFilename());
int flag = fileUpAndDownMapper.saveFileInfo(insertMap);
if(flag > 0)
model.addAttribute("upload.message", "success");
else
model.addAttribute("upload.message", "failure");
return "/core/param/businessparam/uploadResult";
}
}
请同学把错误贴出来,好分析