摘自: http://limingnihao.iteye.com/blog/1069503

环境:maven+SpringMVC + Spring + MyBatis + MySql

本文主要说明如何使用input上传文件到服务器指定目录,或保存到数据库中;如何从数据库下载文件,和显示图像文件并实现缩放。

将文件存储在数据库中,一般是存文件的byte数组,对应的数据库数据类型为blob。

首先要创建数据库,此处使用MySql数据库。

注意:文中给出的代码多为节选重要片段,并不齐全。

1.  前期准备

使用maven创建一个springMVC+spring+mybatis+mysql的项目。

关于如何整合Spring+mybatis+mysql,请见MyBatis简介与配置MyBatis+Spring+MySql:

MyBatis学习 之 一、MyBatis简介与配置MyBaits+Spring+MySql

关于SpringMVC环境的搭建请见:使用Eclipse构建Maven的SpringMVC项目:

使用Eclipse构建Maven的SpringMVC项目

在前台html中,form的enctype为multipart/form-data。注意input、select的name要和StudentForm中成员一一对应。

上传的url为addAction.do,此action方法的参数中使用StudentForm来映射提交的数据。此时就可以获取到提交的文件的数据。然后我们就对文件进行操作。

创建PHOTO_TBL表:PHOTO_DATA字段用于存放文件,类型为MyBatis的longblob;然后写Mapper的Java接口PhotoMapper:包括增删改查;mapper的xml文件:对应JAVA接口的sql语句。

并且需要Spring配置文件添加一个bean的声明。

下面给出html、action、StudentForm的代码片段;创建PHOTO_TBL表的sql、PhotoMapper.java接口代码、PhotoMapper.xml文件代码。

1.1 html的form表单写法

  1. <form action="<c:url value='addAction.do' />" method="post" enctype="multipart/form-data">
  2. <table>
  3. <tr>
  4. <td width="100" align="right">照片:</td>
  5. <td><input type="file" name="studentPhoto"/></td>
  6. </tr>
  7. </table>
  8. <input type="submit">
  9. </form>

1.2 action方法

  1. /**
  2. * 新增 - 提交
  3. */
  4. @RequestMapping(value = "addAction.do")
  5. public String add_action(ModelMap model, StudentForm form) {
  6. }

1.3 StudentForm类

  1. package liming.student.manager.web.model;
  2. import org.springframework.web.multipart.MultipartFile;
  3. public class StudentForm extends GeneralForm {
  4. private String studentName;
  5. private int studentSex;
  6. private String studentBirthday;
  7. private MultipartFile studentPhoto;
  8. }

1.4 创建PHOTO_TBL

  1. CREATE TABLE PHOTO_TBL
  2. (
  3. PHOTO_ID     VARCHAR(100) PRIMARY KEY,
  4. PHOTO_DATA   LONGBLOB,
  5. FILE_NAME    VARCHAR(10)
  6. );

1.5  PhotoMapper接口

  1. @Repository
  2. @Transactional
  3. public interface PhotoMapper {
  4. public void createPhoto(PhotoEntity entity);
  5. public int deletePhotoByPhotoId(String photoId);
  6. public int updatePhotoDate(@Param("photoId") String photoId, @Param("photoDate") byte[] photoDate);
  7. public PhotoEntity getPhotoEntityByPhotoId(String photoId);
  8. }

1.6  PhotoMapper.xml文件

包括增、删、改、查。其中新增中的photoId使用的是mysql自定义函数自动生成主键。在操作blob时需要制定typeHandler为"org.apache.ibatis.type.BlobTypeHandler。insert、update时参数后面需要指定,resultMap中需要指定。

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="liming.student.manager.data.PhotoMapper">
  4. <resultMap type="liming.student.manager.data.model.PhotoEntity" id="photoMapper_resultMap_photoEntity">
  5. <id property="photoId"           column="PHOTO_ID"       javaType="String" jdbcType="VARCHAR" />
  6. <result property="photoData" column="PHOTO_DATA"     javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />
  7. <result property="fileName"  column="FILE_NAME"      javaType="String" jdbcType="VARCHAR" />
  8. </resultMap>
  9. <insert id="createPhoto" parameterType="liming.student.manager.data.model.PhotoEntity">
  10. <selectKey keyProperty="photoId" resultType="String" order="BEFORE">
  11. select nextval('photo')
  12. </selectKey>
  13. INSERT INTO PHOTO_TBL(PHOTO_ID,
  14. PHOTO_DATA,
  15. FILE_NAME)
  16. VALUES(#{photoId, jdbcType=VARCHAR},
  17. #{photoData, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
  18. #{fileName, jdbcType=VARCHAR})
  19. </insert>
  20. <delete id="deletePhotoByPhotoId">
  21. DELETE FROM PHOTO_TBL
  22. WHERE PHOTO_ID = #{photoId, jdbcType=VARCHAR}
  23. </delete>
  24. <update id="updatephotoData" >
  25. UPDATE PHOTO_TBL
  26. SET PHOTO_DATA = #{photoData, javaType=byte[], jdbcType=BLOB, typeHandler=org.apache.ibatis.type.BlobTypeHandler},
  27. FILE_NAME = #{fileName, jdbcType=VARCHAR}
  28. WHERE PHOTO_ID = #{photoId, jdbcType=VARCHAR}
  29. </update>
  30. <select id="getPhotoEntityByPhotoId" resultMap="photoMapper_resultMap_photoEntity">
  31. SELECT PHOTO_ID,
  32. PHOTO_DATA,
  33. FILE_NAME
  34. FROM PHOTO_TBL
  35. WHERE PHOTO_ID = #{photoId, jdbcType=VARCHAR}
  36. </select>
  37. </mapper>

1.7 spring配置文件

需要Spring配置文件添加一个org.springframework.web.multipart.commons.CommonsMultipartResolver的bean的声明。

  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  2. <property name="maxUploadSize" value="1073741824" />
  3. </bean>

2. 将文件到服务器上

  1. private static final String uploadFilePath = "d:\\temp_upload_file\\";
  2. /**
  3. * 新增 - 提交 – 只保存文件到服务器上
  4. */
  5. @RequestMapping(value = "addAction.do")
  6. public String add_action(ModelMap model, StudentForm form) {
  7. try {
  8. MultipartFile uploadFile = form.getStudentPhoto();
  9. String filename = uploadFile.getOriginalFilename();
  10. InputStream is = uploadFile.getInputStream();
  11. // 如果服务器已经存在和上传文件同名的文件,则输出提示信息
  12. File tempFile = new File(uploadFilePath + filename);
  13. if (tempFile.exists()) {
  14. boolean delResult = tempFile.delete();
  15. System.out.println("删除已存在的文件:" + delResult);
  16. }
  17. // 开始保存文件到服务器
  18. if (!filename.equals("")) {
  19. FileOutputStream fos = new FileOutputStream(uploadFilePath + filename);
  20. byte[] buffer = new byte[8192]; // 每次读8K字节
  21. int count = 0;
  22. // 开始读取上传文件的字节,并将其输出到服务端的上传文件输出流中
  23. while ((count = is.read(buffer)) > 0) {
  24. fos.write(buffer, 0, count); // 向服务端文件写入字节流
  25. }
  26. fos.close(); // 关闭FileOutputStream对象
  27. is.close(); // InputStream对象
  28. }
  29. } catch (FileNotFoundException e) {
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }

3. 将文件上传到数据库中

  1. /**
  2. * 新增 - 提交 – 保存文件到数据库
  3. */
  4. @RequestMapping(value = "addAction.do")
  5. public String add_action(ModelMap model, StudentForm form) {
  6. InputStream is = form.getStudentPhoto().getInputStream();
  7. byte[] studentPhotoData = new byte[(int) form.getStudentPhoto().getSize()];
  8. is.read(studentPhotoData);
  9. String fileName = form.getStudentPhoto().getOriginalFilename();
  10. PhotoEntity photoEntity = new PhotoEntity();
  11. photoEntity.setPhotoData(studentPhotoData);
  12. photoEntity.setFileName(fileName);
  13. this.photoMapper.createPhoto(photoEntity);
  14. }

4.下载文件

下载文件需要将byte数组还原成文件。

首先使用mybatis将数据库中的byte数组查出来,指定文件名(包括格式)。然后使用OutputStream将文件输入

  1. @RequestMapping(value = "downPhotoById")
  2. public void downPhotoByStudentId(String id, final HttpServletResponse response){
  3. PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);
  4. byte[] data = entity.getPhotoData();
  5. String fileName = entity.getFileName()== null ? "照片.png" : entity.getFileName();
  6. fileName = URLEncoder.encode(fileName, "UTF-8");
  7. response.reset();
  8. response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
  9. response.addHeader("Content-Length", "" + data.length);
  10. response.setContentType("application/octet-stream;charset=UTF-8");
  11. OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
  12. outputStream.write(data);
  13. outputStream.flush();
  14. outputStream.close();
  15. }
  1. <a href="<%=request.getContextPath() %>/downPhotoById.do?id=8000001">下载照片</a>

5. 显示byte图片文件

  1. @RequestMapping(value = "getPhotoById")
  2. public void getPhotoById (String id, final HttpServletResponse response){
  3. PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);
  4. byte[] data = entity.getPhotoData();
  5. response.setContentType("image/jpeg");
  6. response.setCharacterEncoding("UTF-8");
  7. OutputStream outputSream = response.getOutputStream();
  8. InputStream in = new ByteArrayInputStream(data);
  9. int len = 0;
  10. byte[] buf = new byte[1024];
  11. while ((len = in.read(buf, 0, 1024)) != -1) {
  12. outputSream.write(buf, 0, len);
  13. }
  14. outputSream.close();
  15. }
  1. <img src="<%=request.getContextPath() %>/getPhotoById.do?id=8000001"/>

6. 按长宽等比例缩放图片

  1. @RequestMapping(value = "getPhotoId")
  2. public void getPhotoById (String id, int width, int height, final HttpServletResponse response){
  3. PhotoEntity entity = this.photoMapper.getPhotoEntityByPhotoId(id);
  4. byte[] data = entity.getPhotoData();
  5. if (width != 0 && height != 0) {
  6. data = scaleImage(data, width, height);
  7. }
  8. response.setContentType("image/jpeg");
  9. response.setCharacterEncoding("UTF-8");
  10. OutputStream outputSream = response.getOutputStream();
  11. InputStream in = new ByteArrayInputStream(data);
  12. int len = 0;
  13. byte[] buf = new byte[1024];
  14. while ((len = in.read(buf, 0, 1024)) != -1) {
  15. outputSream.write(buf, 0, len);
  16. }
  17. outputSream.close();
  18. }
  19. public static byte[] scaleImage(byte[] data, int width, int height) throws IOException {
  20. BufferedImage buffered_oldImage = ImageIO.read(new ByteArrayInputStream(data));
  21. int imageOldWidth = buffered_oldImage.getWidth();
  22. int imageOldHeight = buffered_oldImage.getHeight();
  23. double scale_x = (double) width / imageOldWidth;
  24. double scale_y = (double) height / imageOldHeight;
  25. double scale_xy = Math.min(scale_x, scale_y);
  26. int imageNewWidth = (int) (imageOldWidth * scale_xy);
  27. int imageNewHeight = (int) (imageOldHeight * scale_xy);
  28. BufferedImage buffered_newImage = new BufferedImage(imageNewWidth, imageNewHeight, BufferedImage.TYPE_INT_RGB);
  29. buffered_newImage.getGraphics().drawImage(buffered_oldImage.getScaledInstance(imageNewWidth, imageNewHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null);
  30. buffered_newImage.getGraphics().dispose();
  31. ByteArrayOutputStream outPutStream = new ByteArrayOutputStream();
  32. ImageIO.write(buffered_newImage, "jpeg", outPutStream);
  33. return outPutStream.toByteArray();
  34. }
  1. <img src="<%=request.getContextPath() %>/getPhotoById.do?id=8000001&width=300&height=300"/>
7 
0 
分享到:  
评论
6 楼 zqb666kkk 2013-12-23  
PhotoEntity 呢?
5 楼 zqb666kkk 2013-12-23  
photoEntity.setPhotoData(studentPhotoData);

你这句话 有问题吧

实体类里 是   private MultipartFile studentPhoto;

MultipartFile 类型的
set的时候byte[] 类型 
类型不符合 编译都通不过的

4 楼 eye_dxj 2013-10-30  
outputStream.flush();          
3 楼 ddnzero 2013-05-21  
PhotoMapper.xml文件 这个正需要啊 感谢~
2 楼 我是菜鸟1号 2013-03-29  
貌似没看到 PhotoEntity  这个类吧...
1 楼 幸福人生 2011-09-23  
很好,代码直接拷贝就可以,支持一下!

最新文章

  1. 排列组合算法的javascript实现
  2. NOI2018准备Day14晚
  3. gulp教程之gulp-concat
  4. Go语言并发编程示例 分享(含有源代码)
  5. Android之ProgressBar初步应用
  6. 二进制日志BINARY LOG清理
  7. MIME类型
  8. Ajax动态滚动加载数据
  9. ios 中定时器:NSTimer, CADisplayLink, GCD
  10. java中synchronized的用法详解
  11. 手工删除oracle的方法
  12. hive字符串函数
  13. 获取表空间的语句 以及 建表和索引的ddl
  14. MongoDB覆盖索引查询
  15. 【SpringMVC】从Fastjson迁移到Jackson,以及对技术选型的反思
  16. 在macOS下使用MAXPP搭建本地开发服务器简易流程
  17. python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)
  18. copy on write,代理模式
  19. java.lang.ClassCastException: android.os.BinderProxy cannot be cast to com.test.Test
  20. 2017-2018-1 20155315 《信息安全系统设计基础》加分作业:实现mypwd

热门文章

  1. oracle 子查询和组合函数
  2. Underscore.js-精巧而强大实用功能库
  3. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
  4. export导出.xls时,在火狐的情况下出现表名乱码的情况的解决方案
  5. asp.net core集成MongoDB
  6. 后台开发常用mysql语句_v1.0
  7. Highcharts实现走势图
  8. dev devfs udev sysfs及关系
  9. python开发_python操作mysql数据库
  10. UESTC 2015dp专题 H 邱老师选妹子 数位dp