欢迎来到刘认真的博客

It's not me that's wrong. It's the whole world

SSM整合 上传下载之添加商品

上传下载细节:

 导入xml配置文件!!

 

 

 

Controller中要配置存储路径,调用transferto上传文件

 

 

上传图片 要将图片的类设置为 MultipartFile

 

 

 

 

图片下载:

源码:

页面展示:

 

源码:

 

一、建表:

 1 DROP TABLE IF EXISTS `t_house`;
 2 CREATE TABLE `t_house` (
 3   `h_id` int(11) NOT NULL AUTO_INCREMENT,
 4   `h_name` varchar(64) NOT NULL,
 5   `h_rect` double(10,2) NOT NULL,
 6   `h_date` date NOT NULL,
 7   `h_loc1` varchar(255) NOT NULL,
 8   `h_loc2` varchar(255) NOT NULL,
 9   `h_img` varchar(120) NOT NULL,
10   `h_userId` int(11) NOT NULL,
11   PRIMARY KEY (`h_id`)
12 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

 

二、首先注入maven依赖:

pom.xml

 1 <dependency>
 2       <groupId>org.projectlombok</groupId>
 3       <artifactId>lombok</artifactId>
 4       <version>1.18.4</version>
 5       <scope>provided</scope>
 6     </dependency>
 7     <!--文件上传下载 -->
 8     <dependency>
 9       <groupId>commons-io</groupId>
10       <artifactId>commons-io</artifactId>
11       <version>2.4</version>
12     </dependency>
13     <!--用来帮助进行IO功能开发 -->
14     <dependency>
15       <groupId>commons-fileupload</groupId>
16       <artifactId>commons-fileupload</artifactId>
17       <version>1.3</version>
18     </dependency>

 

三、添加配置:

springmvc.xml

1 <!-- 多部分文件上传 M kb byte-->
2     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3         <property name="maxUploadSize" value="104857600"/>
4         <property name="defaultEncoding" value="UTF-8"></property>
5     </bean>

 

四、编辑插入语句:

 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="com.etc.dao.HouseDAO">
 4     <insert id="add">
 5  INSERT INTO `t_house` (
 6     `h_name`,
 7     `h_rect`,
 8     `h_date`,
 9     `h_loc1`,
10     `h_loc2`,
11     `h_img`,
12     `h_userId`
13 )
14 VALUES
15     (
16         #{hName},
17         #{hRect},
18         #{hDate},
19         #{hLoc1},
20         #{hLoc2},
21         #{hImg},
22         #{userId}
23     );
24 </insert>
25 
26 
27 </mapper>

 

五、建立实体类:

HouseVO :
 1 package com.etc.Vo;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 import org.springframework.format.annotation.DateTimeFormat;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 import java.util.Date;
10 
11 @Data
12 @AllArgsConstructor
13 @NoArgsConstructor
14 public class HouseVO {
15     private String title;
16     private Double floorage;
17     @DateTimeFormat(pattern = "yyyy-MM-dd")
18     private Date houseDate;
19     private String districtId;
20     private String streetId;
21     private MultipartFile hImg;
22 }
House :
 1 package com.etc.entity;
 2 
 3 import lombok.AllArgsConstructor;
 4 import lombok.Data;
 5 import lombok.NoArgsConstructor;
 6 
 7 import java.util.Date;
 8 
 9 @Data
10 @AllArgsConstructor
11 @NoArgsConstructor
12 public class House {
13     private Integer hId;
14     private String hName;
15     private Double hRect;
16     private Date hDate;
17     private String hLoc1;
18     private String hLoc2;
19     private String hImg;
20     private int userId;
21 }
HouseConverter :
 1 package com.etc.converter;
 2 
 3 import com.etc.Vo.HouseVO;
 4 import com.etc.entity.House;
 5 
 6 public class HouseConverter {
 7     public static House convert(HouseVO houseVO, String img,int id) {
 8         House house = new House();
 9         house.setHName(houseVO.getTitle());
10         house.setHRect(houseVO.getFloorage());
11         house.setHDate(houseVO.getHouseDate());
12         house.setHLoc1(houseVO.getDistrictId());
13         house.setHLoc2(houseVO.getStreetId());
14         house.setHImg(img);
15         house.setUserId(id);
16         return house;
17     }
18 }

 

六、编写接口:

1 package com.etc.dao;
2 
3 import com.etc.entity.House;
4 
5 public interface HouseDAO {
6 
7     void add(House house);
8 
9 }

 

七、编写service:

 1 package com.etc.servise;
 2 
 3 import com.etc.dao.HouseDAO;
 4 import com.etc.entity.House;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 @Service
 9 public class HouseService {
10 
11     @Autowired
12     private HouseDAO houseDAO;
13 
14     public void add(House house){
15         houseDAO.add(house);
16     }
17 
18 
19 }

 

八、编写控制层:

 

 1 package com.etc.controller;
 2 
 3 import com.etc.Vo.HouseSearchVO;
 4 import com.etc.Vo.HouseVO;
 5 import com.etc.common.Constant;
 6 import com.etc.converter.HouseConverter;
 7 import com.etc.entity.House;
 8 import com.etc.entity.HouseSearch;
 9 import com.etc.entity.User;
10 import com.etc.servise.HouseService;
11 import com.github.pagehelper.PageHelper;
12 import com.github.pagehelper.PageInfo;
13 import org.apache.commons.io.IOUtils;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.stereotype.Controller;
16 import org.springframework.ui.Model;
17 import org.springframework.util.StringUtils;
18 import org.springframework.web.bind.annotation.ModelAttribute;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 
21 import javax.servlet.http.HttpServletResponse;
22 import javax.servlet.http.HttpSession;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.util.List;
27 
28 @Controller
29 @RequestMapping("/house")
30 public class HouseController {
31 
32     public static final String UPLOAD_DIR = "D:\\images\\";
33 
34     @RequestMapping("/images")
35     public String images(String imgName, HttpServletResponse response) throws IOException {
36         //将图片的输入流,复制到response的输出流中,
37         IOUtils.copy(new FileInputStream(UPLOAD_DIR+imgName),response.getOutputStream());
38         return null;
39     }
40 
41 
42     @Autowired
43     private HouseService houseService;
44 
45     @RequestMapping("/add")
46     public String add(HouseVO houseVO, HttpSession session) throws IOException {
47         //获取session中的user对象
48         User user = (User) session.getAttribute("user");
49         //拿到user对象的id
50         int userId = user.getUserId();
51         //设置文件上传的路径
52         houseVO.getHImg().transferTo(new File(UPLOAD_DIR + houseVO.getHImg().getOriginalFilename()));
53         //建立house对象
54         House house = HouseConverter.convert(houseVO, houseVO.getHImg().getOriginalFilename(), userId);
55         //调用添加方法
56         houseService.add(house);
57         return "list";
58     }

 

 

 

 

九、页面展示:

 

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
 2 <!-- saved from url=(0044)http://localhost:8080/HouseRent/page/add.jsp -->
 3 <HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>布谷租房 -发布房屋信息</TITLE>
 4 <META content="text/html; charset=utf-8" http-equiv=Content-Type><LINK 
 5 rel=stylesheet type=text/css href="../css/style.css">
 6 <META name=GENERATOR content="MSHTML 8.00.7601.17514"></HEAD>
 7 <BODY>
 8 <DIV id=header class=wrap>
 9 <DIV id=logo><IMG src="../images/logo.gif"></DIV></DIV>
10 <DIV id=regLogin class=wrap>
11 <DIV class=dialog>
12 <DL class=clearfix>
13   <DT>新房屋信息发布</DT>
14   <DD class=past>填写房屋信息</DD></DL>
15 <DIV class=box>
16 <FORM action="/house/add"  method="post" enctype="multipart/form-data">
17     <%--enctype="multipart/form-data"--%>
18 <DIV class=infos>
19 <TABLE class=field>
20   <TBODY>
21   <TR>
22     <TD class=field>标  题:</TD>
23     <TD><INPUT id=add_action_title class=text type=text name=title> </TD></TR>
24   <TR>
25     <TD class=field>户  型:</TD>
26     <TD><SELECT class=text name=type_id><OPTION selected 
27         value=1000>一室一厅</OPTION></SELECT></TD></TR>
28   <TR>
29     <TD class=field>面  积:</TD>
30     <TD><INPUT id=add_action_floorage class=text type=text 
31 name=floorage></TD></TR>
32   <TR>
33     <TD class=field>价  格:</TD>
34     <TD><INPUT id=add_action_price class=text type=text name=price> </TD></TR>
35   <TR>
36     <TD class=field>房产证日期:</TD>
37     <TD><INPUT class=text type=text name=houseDate></TD></TR>
38   <TR>
39     <TD class=field>位  置:</TD>
40     <TD>区:<SELECT class=text name=districtId><OPTION selected
41         value=1004>海淀区</OPTION></SELECT> 街:<SELECT class=text 
42         name=streetId><OPTION selected value=1001>中关村大街</OPTION></SELECT> </TD></TR><!--
43                         <tr>
44                             <td class="field">坐  标:</td>
45                             <td><input type="text" class="text" name="point" />
46                             </td>
47                         </tr>
48                         --><!--  <tr>
49                             <td class="field">Y 坐  标:</td>
50                             <td><input type="text" class="text" name="point.y" /></td>
51                         </tr>-->
52   <TR>
53     <TD class=field>联系方式:</TD>
54     <TD><INPUT id=add_action_contact class=text type=text name=contact> </TD></TR>
55   <TR>
56     <TD class=field>图片:</TD>
57     <TD><INPUT id=img class=text type=file name=hImg> </TD></TR>
58   <TR>
59     <TD class=field>详细信息:</TD>
60     <TD><TEXTAREA name=description></TEXTAREA></TD></TR></TBODY></TABLE>
61 <DIV class=buttons><INPUT value=立即发布 type=submit>
62 </DIV></DIV></FORM></DIV></DIV></DIV>
63 <DIV id=footer class=wrap>
64 <DL>
65   <DT>布谷租房 © 2010 布谷租房 京ICP证1000001号</DT>
66   <DD>关于我们 · 联系方式 · 意见反馈 · 帮助中心</DD></DL></DIV></BODY></HTML>
fabu.jsp

 

 1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
 3 <!-- saved from url=(0030)http://localhost:8080/House-2/ -->
 4 <HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>布谷租房 - 首页</TITLE>
 5 <META content="text/html; charset=utf-8" http-equiv=Content-Type>
 6 <LINK rel=stylesheet type=text/css href="../css/style.css">
 7 <META name=GENERATOR content="MSHTML 8.00.7601.17514"></HEAD>
 8 <BODY>
 9 <DIV id=header class=wrap>
10 <DIV id=logo><IMG src="../images/logo.gif"></DIV></DIV>
11 <DIV id=navbar class=wrap>
12 <DL class="search clearfix">
13   <FORM id=sform method=post action=/house/serach>
14       <input type="hidden" id="pageNum" name="pageNum">
15   <DT>
16   <UL>
17     <LI class=bold>房屋信息</LI>
18     <LI>标题:<INPUT class=text type=text name=houseName value="${vo.houseName}"> <LABEL class=ui-blue>
19               <INPUT value=搜索房屋 type=submit name=search></LABEL>
20     </LI></UL></DT>
21     <DD>
22       <UL>
23         <LI class=first>面积 </LI>
24         <LI><SELECT name=floorage> <OPTION ${vo.floorage==""?"selected":""} selected value="">不限</OPTION> <OPTION
25                 value=0-40 ${vo.floorage=="0-40"?"selected":""}>40以下</OPTION> <OPTION value=40-500 ${vo.floorage=="40-500"?"selected":""}>40-500</OPTION> <OPTION
26                 value=500-1000000 ${vo.floorage=="500-1000000"?"selected":""}>500以上</OPTION></SELECT> </LI></UL></DD>
27   <DD>
28   <UL>
29     <LI class=first>价格 </LI>
30     <LI><SELECT name=price> <OPTION  selected value="">不限</OPTION> <OPTION
31       value=0-100 >100元以下</OPTION> <OPTION value=100-200 >100元—200元</OPTION>
32       <OPTION value=200-1000000 >200元以上</OPTION></SELECT> </LI></UL></DD>
33   <DD>
34   <UL>
35     <LI class=first>房屋位置</LI>
36     <LI><SELECT id=street name=street_id> <OPTION selected 
37       value="">不限</OPTION> <OPTION value=1000>知春路</OPTION> <OPTION 
38       value=1001>中关村大街</OPTION> <OPTION value=1002>学院路</OPTION> <OPTION 
39       value=1003>朝阳路</OPTION></SELECT> </LI></UL></DD>
40   <DD>
41   <UL>
42     <LI class=first>房型</LI>
43     <LI><SELECT name=type_id> <OPTION selected value="">不限</OPTION> <OPTION 
44       value=1000>一室一厅</OPTION> <OPTION value=1001>一室两厅</OPTION> <OPTION 
45       value=1002>两室一厅</OPTION> <OPTION value=1003>两室两厅</OPTION></SELECT> 
46   </LI></UL></DD>
47 </FORM></DL></DIV>
48 <DIV class="main wrap">
49   <TABLE class=house-list>
50     <TBODY>
51     <c:forEach items="${pageInfo.list}" var="house">
52       <TR>
53         <TD class=house-thumb><span><A href="../../details.jsp" target="_blank"><img src="/house/images?imgName=${house.HImg}" width="100" height="75" alt=""></a></span></TD>
54         <TD>
55           <DL>
56             <DT><A href="../../details.jsp" target="_blank">${house.HName}</A></DT>
57             <DD>${house.HLoc1}${house.HLoc2},${house.HRect}平米<BR>联系方式:3456 </DD></DL></TD>
58         <TD class=house-type>一室一厅</TD>
59         <TD class=house-price><SPAN>346.0</SPAN>元/月</TD></TR>
60     </c:forEach>
61     </TBODY></TABLE>
62   <DIV class=pager>
63     <UL>
64       <LI class=current><A href="javascript:void(0)" onclick="form(${pageInfo.firstPage})">首页</A></LI>
65       <LI><A href="javascript:void(0)" onclick="form(${pageInfo.hasPreviousPage?pageInfo.prePage:pageInfo.pageNum})">上一页</A></LI>
66       <LI><A href="javascript:void(0)" onclick="form(${pageInfo.hasNextPage?pageInfo.nextPage:pageInfo.pageNum})">下一页</A></LI>
67       <LI><A href="javascript:void(0)" onclick="form(${pageInfo.lastPage})">末页</A></LI></UL><SPAN
68           class=total>${pageInfo.pageNum}/${pageInfo.pages}页</SPAN> </DIV></DIV>
69 <DIV id=footer class=wrap>
70 <DL>
71   <DT>布谷租房 © 2010 布谷租房 京ICP证1000001号</DT>
72   <DD>关于我们 · 联系方式 · 意见反馈 · 帮助中心</DD></DL></DIV></BODY><script>
73      function form(pageNum){
74          var page=document.getElementById("pageNum");
75          page.value=pageNum;
76          var form=document.getElementById("sform");
77          form.submit();
78     }
79 
80 </script></HTML>
list.jsp

 

posted @ 2019-07-26 17:35  刘认真  阅读(334)  评论(0编辑  收藏  举报