住房信息平台添加发布房源功能
POST1
继day26
连接七牛云将图片传送到七牛云上 发布房源
导jar包,放pom里
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>7.2.24<ersion>
</dependency>
HouseViewController
@Controller
@RequestMapping("house")
public class HouseViewController {
@Autowired
private IHouseViewService houseViewService;
@RequestMapping("findFourHouse.do")
public ModelAndView findFourHouse(){
ModelAndView mv=new ModelAndView();
List<HouseView> newHouses=houseViewService.findFourHouseByType(0);
List<HouseView> oldHouses=houseViewService.findFourHouseByType(1);
List<HouseView> rentHouses=houseViewService.findFourHouseByType(2);
mv.addObject("newHouses",newHouses);
mv.addObject("oldHouses",oldHouses);
mv.addObject("rentHouses",rentHouses);
mv.setViewName("../main");
return mv;
}
@RequestMapping("findHouseById.do")
public ModelAndView findHouseById(int houseId){
HouseView house=houseViewService.findHouseByHouseId(houseId);
ModelAndView mv=new ModelAndView();
mv.addObject("houseInfo",house);
mv.setViewName("details");
return mv;
}
@RequestMapping("findHouse.do")
public ModelAndView findHouseByType(@RequestParam(defaultValue = "1") int currentPage, int houseType){
ModelAndView mv=new ModelAndView();
List<HouseView> houses=houseViewService.findHouseByType(currentPage,houseType);
List<HouseView> fh=houseViewService.findFourHouseByType(houseType);
PageInfo<HouseView> pageInfo=new PageInfo<>(houses);
mv.addObject("pageInfo",pageInfo);
mv.addObject("fh",fh);
if (houseType==0){
mv.setViewName("newhouse");
}else if (houseType==1){
mv.setViewName("oldhouse");
}else {
mv.setViewName("renthouse");
}
return mv;
}
@RequestMapping("toHousePost2.do")
public ModelAndView toHousePost2(@Param("houseTitle") String houseTitle,
@Param("houseAddress") String houseAddress,
@Param(value = "housePrice") BigDecimal housePrice,
@Param(value = "priceUnit") String priceUnit,
@Param(value = "houseHeadimg") MultipartFile houseHeadimg,
@Param(value = "housePlanimg1") MultipartFile housePlanimg1,
@Param(value = "housePlanimg2") MultipartFile housePlanimg2,
@Param(value = "houseImg1") MultipartFile houseImg1,
@Param(value = "houseImg2") MultipartFile houseImg2,
@Param(value = "houseImg3") MultipartFile houseImg3,
@Param(value = "houseImg4") MultipartFile houseImg4,
@Param(value = "houseImg5") MultipartFile houseImg5,
@Param(value = "houseImg6") MultipartFile houseImg6) throws IOException {
House house=new House();
house.setHouseTitle(houseTitle);
house.setHouseAddress(houseAddress);
house.setHousePrice(housePrice);
house.setPriceUnit(priceUnit);
house.setUserId(10005);
Map<String, MultipartFile> images=new HashMap<>();
if (houseHeadimg.getSize()!=0){
String picName=YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseHeadimg);
}
if (housePlanimg1.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,housePlanimg1);
}
if (housePlanimg2.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,housePlanimg2);
}
if (houseImg1.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg1);
}
if (houseImg2.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg2);
}
if (houseImg3.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg3);
}
if (houseImg4.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg4);
}
if (houseImg5.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg5);
}
if (houseImg6.getSize()!=0){
String picName = YijuUtil.getPicName();
house.setHouseHeadimg(picName);
images.put(picName,houseImg6);
}
Set<String> keys=images.keySet();
for (String key:keys){
MultipartFile file=images.get(key);
YijuUtil.upload(file.getBytes(),key);
}
houseViewService.postHouse(house);
return null;
}
}
IHouseView和IHouseViewService加入void postHouse(House house);
HouseViewService中抛出异常后添加以下代码
@Override
public void postHouse(House house) {
houseViewDao.postHouse(house);
}
YijuUtil
public class YijuUtil {
public static void main(String[] args) {
// String local="C:\\Users\\xcdn\\Desktop\\picture.jpeg";
// String upload="wly.jpeg";
String picName=getPicName();
System.out.println(picName);
}
public static String getPicName(){
SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
String picName=sf.format(new Date()+ UUID.randomUUID().toString());
return picName;
}
public static void upload(byte[] file, String upload){
final String AK="V86-ao-ntbpWZrtv7QF4YQ-8CM6sA_TSx1qB8Gyc";
final String SK="4C2-dUzRqQJ7-LEKR7ON4ey5GKVZw83QyGf4gWQE";
final String BUCKET="hualili1021";
Configuration cfg=new Configuration(Zone.zone1()) ;
UploadManager uploadManager=new UploadManager(cfg);
Auth auth=Auth.create(AK,SK);
String uptake = auth.uploadToken(BUCKET);
try {
Response response = uploadManager.put(file, upload, uptake);
System.out.println(response);
} catch (QiniuException e) {
e.printStackTrace();
}
}
}
HouseMapper中添加以下代码
<insert id="postHouse" parameterType="com.yiju.bean.House">
insert into tb_house(user_id, house_type, house_title, house_headimg, house_planimg1, house_planimg2, house_img1, house_img2, house_img3, house_img4, house_img5, house_img6, house_price, price_unit, house_address, is_delete, create_time, update_time)
values (#{userId}, #{houseType}, #{houseTitle}, #{houseHeadimg}, #{housePlanimg1}, #{housePlanimg2}, #{houseImg1}, #{houseImg2}, #{houseImg3}, #{houseImg4}, #{houseImg5}, #{houseImg6}, #{housePrice}, #{priceUnit}, #{houseAddress}, #{isDelete}, #{createTime}, #{updateTime})
</insert>
在main.jsp中发布房源信息这一句里加上这句代码