jsp文件上传与java文件保存到tomcat

jsp 部分代码:


<form id="inputForm" action="${pageContext.request.contextPath}${fns:getFrontPath()}/background" method="post"  enctype="multipart/form-data" >
<label><input checked="checked" name="indexImg" type="radio" value="banner" />banner</label> 
<label><input name="indexImg" type="radio" value="mw" />mw</label> 
<label><input name="indexImg" type="radio" value="bp" />bp</label> 
<label><input name="indexImg" type="radio" value="feng" />feng</label> 
<input class="file" type = "file" name = "file"/>
<input class="save" type = "submit" vlaue = "保存"/>
</form>


java部分代码:


@RequestMapping(value = "background")
public String saveBackground(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request,ModelMap model, String indexImg){

        String imagePath = request.getSession().getServletContext().getRealPath("/") + "/static/images/";
        if("banner".equals(indexImg)) {
        imagePath += "index-banner.png";
        }
        if("mw".equals(indexImg)) {
        imagePath += "index-img1.png";
        }
        if("bp".equals(indexImg)) {
        imagePath += "index-img2.png";
        }
        if("feng".equals(indexImg)) {
        imagePath += "index-img3.png";
        }
        File targetFile = new File(imagePath);
        if(!targetFile.exists()){  
        try {
targetFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
        }  
  
        //保存  
        try {  
            file.transferTo(targetFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } 
        model.addAttribute("img", "ok");
        return "redirect:" + adminPath;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值