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 Files\apache-tomcat-7.0.62\webapps\springmvcImage\14386803406181764242780.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 Files\apache-tomcat-7.0.62\webapps\springmvc\
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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM