day14 七牛云上传 ssm框架 图片上传

SSM框架 图片上传

图片是通过 (反正是file形式的)

 <input type="file" name="houseImg5" id="inter5"/>

传到controller的参数 使用MultipartFile 形式 好像上传七牛云比较方便 (网上这么说的

 @RequestMapping("toHousePost2")
    public String toHousePost2(@Param(value = "houseTitle")String houseTitle,
                               @Param(value = "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 Exception{
        House house = new House();
        house.setHouseTitle(houseTitle);
        house.setHousePrice(housePrice);
        house.setHouseAddress(houseAddress);
        house.setPriceUnit(priceUnit);

        //存放图片
        Map<String,byte[]> images= new HashMap<>();
        if(houseHeadimg.getSize() != 0){
            byte[] houseHeadimgBytes = houseHeadimg.getBytes();
            String picName = YiJuUtil.getPicName();
            house.setHouseHeadimg(picName);
            images.put(picName,houseHeadimgBytes);
        }
        if(housePlanimg1.getSize() != 0) {
            byte[] planimg1Byte = housePlanimg1.getBytes();
            String planimg1 = YiJuUtil.getPicName();
            house.setHousePlanimg1(planimg1);
            images.put(planimg1,planimg1Byte);
        }

        if(housePlanimg2.getSize() != 0) {
            byte[] planimg2Bytes = housePlanimg2.getBytes();
            String planimg2 = YiJuUtil.getPicName();
            house.setHousePlanimg2(planimg2);
            images.put(planimg2,planimg2Bytes);
        }

        if(houseImg1.getSize() != 0) {
            byte[] img1Bytes = houseImg1.getBytes();
            String img1 = YiJuUtil.getPicName();
            house.setHouseImg1(img1);
            images.put(img1,img1Bytes);
        }

        if(houseImg2.getSize() != 0) {
            byte[] img2Bytes = houseImg2.getBytes();
            String img2 = YiJuUtil.getPicName();
            house.setHouseImg2(img2);
            images.put(img2,img2Bytes);
        }

        if(houseImg3.getSize() != 0) {
            byte[] img3Bytes = houseImg3.getBytes();
            String img3 = YiJuUtil.getPicName();
            house.setHouseImg3(img3);
            images.put(img3,img3Bytes);
        }

        if(houseImg4.getSize() != 0) {
            byte[] img4Bytes = houseImg4.getBytes();
            String img4 = YiJuUtil.getPicName();
            house.setHouseImg4(img4);
            images.put(img4,img4Bytes);
        }

        if(houseImg5.getSize() != 0) {
            byte[] img5Bytes = houseImg5.getBytes();
            String img5 = YiJuUtil.getPicName();
            house.setHouseImg5(img5);
            images.put(img5,img5Bytes);
        }

        if(houseImg6.getSize() != 0) {
            byte[] img6Bytes = houseImg6.getBytes();
            String img6 = YiJuUtil.getPicName();
            house.setHouseImg6(img6);
            images.put(img6,img6Bytes);
        }
        house.setUserId(10000);
        houseService.insertHouse(house);

        return "";

    }

七牛云上传方法

public class test {
    public static void main(String[] args) {
        Configuration cfg = new Configuration(Zone.zone1());
        UploadManager uploadManager = new UploadManager(cfg);

        String ak="----------------------------------------";//用钥匙使劲刮--- 可以获得 ak sk秘钥
        String sk="----------------------------------------";

        String bucket = "hualili";

        Auth auth = Auth.create(ak,sk);
        String uploadToken = auth.uploadToken(bucket);

        String file = "G:\\桌面壁纸\\d1eea48093c18fcc03f4e0ef26c8577e.jpg";
        String key = "123123123.jpg";
        try {
            Response response = uploadManager.put(file, key, uploadToken);
            System.out.println(response);
        } catch (QiniuException e) {
            e.printStackTrace();
        }
    }
}

命名工具类

public class YiJuUtil {

    public static String getPicName(){
        return new SimpleDateFormat("yyyyMMdd").format(new Date())+ UUID.randomUUID().toString();
    }

}
上一篇:如何将购物车信息存到Redis中?


下一篇:SprintBoot单元测试