Day14
ssm实现图片上传操作
1.创建单独的实体类
house,houseInfo,houseInter用于存放单独的信息
2.发布房源
跳转直接跳转jsp不需要.do
<li><a href="${pageContext.request.contextPath}/pages/housePost1.jsp">发布房源</a></li>
3.在dao,mapper中写插入语句
4.在service层和Controller层
controller层传数据要用到session传递参数
还要用@Param注解接受jsp页面传过来图片的byte[]数组
把信息都写入House实体类中
还要写一个工具类util
里面用SimpleDateFormat.format方法加random随机数生成一个名字
public class YijuUtil {
public static String getPicName(){
String picName = new SimpleDateFormat("yyyyMMdd").format(new Date())+ UUID.randomUUID().toString();
return picName;
}
}
然后还写了插入语句,因为有外键uid不能为空所以要加上,在调用插入的方法
最后再把实体类house放入session中。结束
@RequestMapping("toHousePost2.do")
public String toHousePost2(HttpSession session, @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 IOException {
House house = new House();
house.setHouseTitle(houseTitle);
house.setHouseAddress(houseAddress);
house.setHousePrice(housePrice);
house.setPriceUnit(priceUnit);
Map<String,byte[]> images = new HashMap<>();
if(houseHeadimg.getSize()!=0){
byte[] houseHeadimgBytes = houseHeadimg.getBytes();
String houseHeadimgpicName = YijuUtil.getPicName();
images.put(houseHeadimgpicName,houseHeadimgBytes);
house.setHouseHeadimg(houseHeadimgpicName);
}
if(housePlanimg1.getSize()!=0){
byte[] housePlanimg1Bytes = housePlanimg1.getBytes();
String housePlanImgpicName = YijuUtil.getPicName();
images.put(housePlanImgpicName,housePlanimg1Bytes);
house.setHousePlanimg1(housePlanImgpicName);
}
if(housePlanimg2.getSize()!=0){
byte[] housePlanimg2Bytes = housePlanimg2.getBytes();
String housePlanimg2picName = YijuUtil.getPicName();
images.put(housePlanimg2picName,housePlanimg2Bytes);
house.setHousePlanimg2(housePlanimg2picName);
}
if(houseImg1.getSize()!=0){
byte[] houseimg1Bytes = houseImg1.getBytes();
String houseimg1picName = YijuUtil.getPicName();
images.put(houseimg1picName,houseimg1Bytes);
house.setHouseImg1(houseimg1picName);
}
if(houseImg2.getSize()!=0){
byte[] houseimg2Bytes = houseImg2.getBytes();
String houseimg2picName = YijuUtil.getPicName();
images.put(houseimg2picName,houseimg2Bytes);
house.setHouseImg2(houseimg2picName);
}
if(houseImg3.getSize()!=0){
byte[] houseimg3Bytes = houseImg3.getBytes();
String houseimg3picName = YijuUtil.getPicName();
images.put(houseimg3picName,houseimg3Bytes);
house.setHouseImg3(houseimg3picName);
}
if(houseImg4.getSize()!=0){
byte[] houseimg4Bytes = houseImg4.getBytes();
String houseimg4picName = YijuUtil.getPicName();
images.put(houseimg4picName,houseimg4Bytes);
house.setHouseImg4(houseimg4picName);
}
if(houseImg5.getSize()!=0){
byte[] houseimg5Bytes = houseImg5.getBytes();
String houseimg5picName = YijuUtil.getPicName();
images.put(houseimg5picName,houseimg5Bytes);
house.setHouseImg5(houseimg5picName);
}
if(houseImg6.getSize()!=0){
byte[] houseimg6Bytes = houseImg6.getBytes();
String houseimg6picName = YijuUtil.getPicName();
images.put(houseimg6picName,houseimg6Bytes);
house.setHouseImg6(houseimg6picName);
}
session.setAttribute("house",house);
return "redirect:../pages/housePost2.jsp";
}
@RequestMapping("toHousePost3.do")
public String toHousePost3(HouseInfo houseInfo){
return "redirect:../pages/housePost3.jsp";
}
千牛云上传图片
首先就是先要注册有一个自己的千牛云账号,然后创建一个文件夹
然后有一个写了一个上传图片测试类
设置Configuration设置地区
把地区信息放去UploadManager类中
然后获取自己的密钥写入到Auth类中
通过auth类获取一个String类型的uploadToken凭证
还有文件路劲file
还有上传文件的名字
通过upload.put方法把信息放入进去。
public class Text {
public static void main(String[] args){
Configuration cfg = new Configuration(Zone.zone0());
UploadManager uploadManager = new UploadManager(cfg);
String ak="***";//这是我的密钥
String sk="***";
String name = "xiaolong";
Auth auth = Auth.create(ak,sk);
String uploadToken = auth.uploadToken(name);
String file = "D:\\我的文档\\Pictures\\123.jpg";
String names = "123.jpg";
try {
uploadManager.put(file,names,uploadToken);
} catch (QiniuException e) {
e.printStackTrace();
}
}
}