java向數據庫中 傳圖片

CommodityController.java
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.javasession

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;
}
}
}
相關文章
相關標籤/搜索