1. 程式人生 > >ssm整合-圖片上傳功能(轉)

ssm整合-圖片上傳功能(轉)

需要 PE 添加用戶 名稱 simple target url 完整 dsi

本文介紹 ssm (Spring+SpringMVC+Mybatis)實現上傳功能。

以一個添加用戶的案例介紹(主要是將上傳文件)。

一、需求介紹

我們要實現添加用戶的時候上傳圖片(其實任何文件都可以)。

文件名:以 博客名+日期的年月日時分秒毫秒形式命名

如 言曌博客2017082516403213.png

路徑:上傳到 uploads 文件夾,並 生成相應的 年和月 子文件夾

如 uploads/2017/8/言曌博客2017082516403213.png

技術分享圖片

數據庫:將"年/月/"+文件名 存儲到數據表中

如 2017/8/言曌博客20170825164809907.jpg

二、導入 Jar 包

上傳功能需要額外的兩個 jar 包,如下

技術分享圖片

導入 環境中

我這裏使用是 Maven,添加依賴

 1 <!--文件上傳-->
 2 <dependency>
 3   <groupId>commons-fileupload</groupId>
 4   <artifactId>commons-fileupload</artifactId>
 5   <version>1.2.2</version>
 6 </dependency>
 7 <dependency>
 8   <groupId>commons-io</groupId>
 9
<artifactId>commons-io</artifactId> 10 <version>2.4</version> 11 </dependency>

三、代碼結構

技術分享圖片

文件上傳到如圖 uploads,如果你和博主也是使用了 Maven,文件其實是上傳到

ForestBlog\target\ForestBlog\resource\uploads\2017\8 裏面,這個沒影響的。

但是要要記得在 clean 之前把 \target\ForestBlog\resource\uploads 文件復制到

src\main\ForestBlog\resource\uploads 中

四、代碼實現

我們這裏主要看上傳部分代碼,其他的配置文件也貼一下吧

1、springmvc 配置 (springmvc.xml部分代碼)

1 <!--文件上傳-->
2 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3     <!--設置上傳最大尺寸為5MB-->
4     <property name="maxUploadSizePerFile" value="5242880"/>
5     <property name="defaultEncoding" value="UTF-8"/>
6     <property name="resolveLazily" value="true"/>
7 </bean>

2、jsp 頁面 (createUser.jsp部分代碼)

1 <form action="${pageContext.request.contextPath}/createUserSubmit.action"
2       method="post" enctype="multipart/form-data" >
3        <input type="file" name="upload_avatar">
4        <input type="submit" value="提交">
5 </form>

3、控制器代碼(UserController.java 中 添加用戶類)

//添加用戶提交
    @RequestMapping(value = "/createUserSubmit",method = RequestMethod.POST)
    public String createUserSubmit(UserCustom userCustom,MultipartFile upload_avatar ) throws Exception {
        //上傳圖片
        if(upload_avatar.getSize()!=0) {
            String newFileName = functions.uploadFile(request,upload_avatar);
            userCustom.setAvatar(newFileName);
        }
        userCustom.setLastloginip(functions.getIpAddr(request));
        userService.createUser(userCustom);
        return "redirect:userList.action";
    }

4、上傳文件代碼 (functions.java 記得要註入)

 1 //上傳文件
 2     public String  uploadFile(HttpServletRequest request,MultipartFile uploadFile) throws IOException {
 3         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
 4         String res = sdf.format(new Date());
 5         //uploads文件夾位置
 6         String rootPath =request.getServletContext().getRealPath("/resource/uploads/");
 7         //原始名稱
 8         String originalFilename = uploadFile.getOriginalFilename();
 9         //新的文件名稱
10         String newFileName = "言曌博客"+res+originalFilename.substring(originalFilename.lastIndexOf("."));
11         //創建年月文件夾
12         Calendar date = Calendar.getInstance();
13         File dateDirs = new File(date.get(Calendar.YEAR)
14             + File.separator + (date.get(Calendar.MONTH)+1));
15         //新文件
16         File newFile = new File(rootPath+File.separator+dateDirs+File.separator+newFileName);
17         //判斷目標文件所在的目錄是否存在
18         if(!newFile.getParentFile().exists()) {
19             //如果目標文件所在的目錄不存在,則創建父目錄
20             newFile.getParentFile().mkdirs();
21         }
22         System.out.println(newFile);
23         //將內存中的數據寫入磁盤
24         uploadFile.transferTo(newFile);
25         //完整的url
26         String fileUrl =  date.get(Calendar.YEAR)+ "/"+(date.get(Calendar.MONTH)+1)+ "/"+ newFileName;
27         return fileUrl;
28     }

主要關註 上傳文件的方法,屬性註入這裏就不贅述了

寫的步驟很完整、清晰

https://liuyanzhao.com/5989.html

ssm整合-圖片上傳功能(轉)