java向数据库中 传图片

本文介绍了一个使用Java实现的商品添加功能,涉及文件上传、图片处理及数据库操作等环节,并展示了具体的代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CommodityController.java

@RequestMapping(value = "addCommodity", method = RequestMethod.POST)
	public String executeAddCommodity(Model model, @RequestParam(value = "file", required = false) MultipartFile file
			, @RequestParam(value = "file1", required = false) MultipartFile file1
			, @RequestParam(value = "file2", required = false) MultipartFile file2
			, @RequestParam(value = "file3", required = false) MultipartFile file3
			, @RequestParam(value = "file4", required = false) MultipartFile file4
			, @RequestParam(value = "file5", required = false) MultipartFile file5
			, @RequestParam(value = "file6", required = false) MultipartFile file6,
			@RequestParam(value = "attachments", required = false) MultipartFile attachments, HttpSession session, @Valid @ModelAttribute("commodityForm") CommodityForm commodityForm, BindingResult results) throws SQLException, IOException {
		log.info("添加商品信息");
		if(results.hasErrors()){
			List<Item> commodityType = itemListComponent.getCommodityType();
	    	model.addAttribute("commodityType", commodityType);
			List<Item> supplierList = itemListComponent.getSupplierList();
	    	model.addAttribute("supplierList", supplierList);
			return "manager/commodity/addCommodity";}
		UVO uvo = (UVO)session.getAttribute("UVO");
		commodityForm.setUpdateUser(uvo.getUserName());
		Date date = new Date();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		commodityForm.setUpdateTime(dateformat.format(date));
		commodityForm.setPicture(file.getBytes());
		commodityForm.setPicture1(file1.getBytes());
		commodityForm.setPicture2(file2.getBytes());
		commodityForm.setPicture3(file3.getBytes());
		commodityForm.setPicture4(file4.getBytes());
		commodityForm.setPicture5(file5.getBytes());
		commodityForm.setPicture6(file6.getBytes());
		boolean result = commodityService.addCommodity(commodityForm);
		if(!result) {
			throw new SQLException("商品信息添加失败!");
		}
	
		model.addAttribute("list", commodityService.searchCommodityList());
		if (!attachments.isEmpty()) {
    		String suffix = attachments.getOriginalFilename().substring(attachments.getOriginalFilename().lastIndexOf("."));
        	File targetFile = new File(env.getProperty("upload.file.path"), commodityForm.getCommodityId() + suffix);
        	if(!targetFile.exists()){
                targetFile.mkdirs();
            }
        	attachments.transferTo(targetFile);
    	}
        return "manager/commodity/commodityList";
	}

CommodityService.java

public boolean addCommodity(CommodityForm frm) {	
if (frm.getPicture().length != 0) {
Integer sequee0 = queryDao.executeForObject("Commodity.getSeq", null, Integer.class);
String commodityId0 = (String) (frm.getUpdateTime().substring(0, 4) + String.format("%011d", sequee0));
frm.setPictureId(commodityId0);
int picResult = updateDao.execute("Commodity.addPicture", frm);
if (picResult != 1) {
return false;
}
}
}




转载于:https://my.oschina.net/u/2412052/blog/491926

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值