1.前台通过jsp上传文件,后台controller接收文件,将其保存至mysql数据库中
2.在jsp页面查看数据库中的文件(在线预览)
3.下载文件到本地
上传
在springMVC.xml中配置
<!-- 设置上传文件最大值 5M=5*1024*1024(B)=5242880 bytes -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5242880" />
</bean>
jsp页面代码
提交至后台saveFile.action
<form action="${pageContext.request.contextPath }/file/saveFile.action" method="post" enctype="multipart/form-data" >
<span>上传资料</span>
<br>
<input type="file" id="upfile" name="upfile" >
<input id="submit" type="submit" value="提交" >
</form>
后台controller代码
@Controller
@RequestMapping("file")
public class FileController {
@Autowired
private FileService fileService;
@RequestMapping("saveFile")
public String saveFile(@Param("upfile") MultipartFile upfile) {
FileBin fileBin = new FileBin();
if (!upfile.isEmpty()) {
// 所上传的文件名
String upfileName = upfile.getOriginalFilename();
fileBin.setBinName(upfileName.substring(0, upfileName.lastIndexOf(".") - 1));
// 将 MultipartFile 类型转为 File 类型
DiskFileItem dfi = (DiskFileItem)((CommonsMultipartFile) upfile).getFileItem();
File result = dfi.getStoreLocation();
// 文件转换为二进制字节数组
fileBin.setBinFile(FileUtils.getBytesFromFile(result));
// 文件后缀
fileBin.setBinSuffix(upfileName.substring(upfileName.lastIndexOf(".") + 1));
fileBin.setBinType(upfile.getContentType().substring(upfile.getContentType().lastIndexOf("/") + 1));
this.fileService.saveFileBin(fileBin);
}
return "index";// 自定义的返回界面
}
}
其中的转换方法 getBytesFromFile 代码如下:
public static byte[] getBytesFromFile(File f) {
if (f == null) {
return null;
}
try {
FileInputStream stream = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = stream.read(b)) != -1)
out.write(b, 0, n);
stream.close();
out.close();
return out.toByteArray();
} catch (IOException e) {
}
return null;
}
在mapper.xml中书写sql数据库代码
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="nwu.cl.project.mapper.FileMapper">
<!-- 保存文件 -->
<insert id="saveFileBin" parameterType="nwu.cl.project.pojo.FileBin">
INSERT INTO file_bin
(binName,binFile,binSuffix,binType)
VALUES
(#{binName},#{binFile},#{binSuffix},#{binType})
</insert>
</mapper>
中间的service层和mapper层代码省略。
查看
jsp页面代码:
<a href="${pageContext.request.contextPath }/file/lookFile.action?binId=3">对文艺学的研究</a>
这里的binId和文件名都是写死的,后面用的时候用el表达式获取。
controller代码:
@RequestMapping("lookFile")
public void lookFile(@Param("binId") Integer binId, HttpServletResponse response) {
if (binId != null && !"".equals(binId)) {
FileBin fileBin = this.fileService.getFileBin(binId);
OutputStream output;
try {
output = response.getOutputStream();
output.write(fileBin.getBinFile());
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下载
jsp代码:
<a href="${pageContext.request.contextPath }/file/downloadPdfFile.action?binId=3"><input type="button" value="下载" ></a>
binId依然是写死的
controller代码:
@RequestMapping("downloadPdfFile")
public void downloadPdfFile(HttpServletRequest request, HttpServletResponse response,
@Param("binId") Integer binId) {
OutputStream outputStream = null;
try {
request.setCharacterEncoding("UTF-8");
response.reset();
FileBin fileBin = this.fileService.getFileBin(binId);
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(fileBin.getBinName() + "." + fileBin.getBinSuffix(), "UTF-8"));
outputStream = response.getOutputStream();
outputStream.write(fileBin.getBinFile());
outputStream.flush();
} catch (Exception e) {
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}