• Maven+SpringMVC+MyBatis 上传图片


      上传文件我一直都觉得很难,好吧,所有涉及文件操作的我都觉得不容易。然后今天尝试了从网页上传图片保存到服务器。这个例子的前提是搭建好了服务器端框架:Maven+Spring MVC+MyBatis。当然必要的准备我也要提及。

      首先是jar包,上传文件必不可少的jar包:commons-fileupload和commons-io。这两个是apache的开源jar包。

      Maven配置:

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    Maven配置

      有了这两个jar包基本上就行了。

      

      还有一个准备工作是对SpringMVC上传文件的配置:

    <!-- 对上传文件的配置 -->
    <bean id="multipartResolver"
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <property name="maxUploadSize">
            <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>
    Show Code

      

      接下来是上传文件的jsp页面,当然一般的html页面也是可以的。特别要注意的是,form表单里面有文件上传时,必须要指定enctype属性值为multipart/form-data,意思是以二进制流的形式传输文件。否则会上传不了,会报错的。

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html lang="en">
     5 <head>
     6     <meta charset="UTF-8">
     7     <title>Document</title>
     8 </head>
     9 <body>
    10     <form action="appuser/uploadHeadPhoto.do"
    11     method="POST" enctype="multipart/form-data" >
    12         <input type="file" name="headPhotoFile">
    13         <input type="text" name="userId" value="1" readonly>
    14         <button type="submit" value="提交">提交</button>
    15     </form>
    16 </body>
    17 </html>
    index.jsp

      

      后台Controller处理文件上传的接口,主要过程是获取文件二进制流,然后写入新创建的jpg,返回新创建的jpg的地址,将地址写入数据库。数据库里面最终存放的jpg图片路径是D:Program Filesapache-tomcat-7.0.62webappsspringmvcImage14386803406181764242780.jpg。

     1 @RequestMapping("/uploadHeadPhoto")
     2     public String uploadHeadPhoto(@RequestParam MultipartFile headPhotoFile, HttpServletRequest request) throws IOException{
     3         String userId = request.getParameter("userId");
     4         
     5         if(headPhotoFile.isEmpty()){
     6             System.out.println("文件未上传");
     7         }else{
     8             String contentType = headPhotoFile.getContentType();
     9             System.out.println(contentType);//输出image/jpeg
    10             if(contentType.startsWith("image")){
    11                 //获取Web项目的全路径
    12                 String realPath = request.getSession().getServletContext().getRealPath("/");
    13                 System.out.println(realPath);//输出D:Program Filesapache-tomcat-7.0.62webappsspringmvc
    14                 realPath = realPath.replace("springmvc", "springmvcImage");
    15                 realPath.concat("user");
    16                 String newFileName = new Date().getTime()+""+new Random().nextInt()+".jpg";
    17                 FileUtils.copyInputStreamToFile(headPhotoFile.getInputStream(), new File(realPath, newFileName));
    18                 
    19                 //将图片路径插入数据库
    20                 Map<String, Object> requestMap = new HashMap<String, Object>();
    21                 requestMap.put("userId", userId);
    22                 requestMap.put("headPhoto", realPath+newFileName);
    23                 int flag = userService.uploadHeadPhoto(requestMap);
    24                 if(flag!=0&&flag!=-1){
    25                     System.out.println("success");
    26                     return "success";
    27                 }
    28             }
    29         }
    30         
    31         return null;
    32     }
    View Code

      

  • 相关阅读:
    Elasticsearch重要文章之四:监控每个节点(ThreadPool部分)
    [翻译]Elasticsearch重要文章之四:监控每个节点(jvm部分)
    IDFA
    Python中eval函数的作用
    Android开发重点难点:RelativeLayout(相对布局)详解
    布局相关
    Nginx配置proxy_pass
    Docker部署Elasticsearch集群
    extends Thread 与 implements Runnable 的区别
    玩转 Redis缓存 集群高可用
  • 原文地址:https://www.cnblogs.com/rainmer/p/4702724.html
Copyright © 2020-2023  润新知