Spring mvc上传图片

本文介绍如何利用Spring MVC框架配合commons-fileupload组件实现文件上传功能。通过具体示例展示了前端表单设置、文件类型验证、后端处理逻辑及配置等关键步骤。

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

[size=medium]首先引入commons-fileupload-1.2.1.jar和commons-io-1.4.jar架包[/size]

jsp代码:
form表单必须加上[color=red]enctype="multipart/form-data"[/color]

<script type="text/javascript">
function addNewsImg(){
var img = document.getElementById("img").value;
if(img == null || img == ""){
alert("上传图片不能为空!");
return;
}else{
var length = img.lastIndexOf(".");
var postfix = img.substring(length+1,img.length);
if(postfix!="jpg"&&postfix!="gif"){
alert("上传的文件必须是jpg或gif结尾,请重新选择!");
document.getElementById("img").value="";
return ;
}else{
document.getElementById("addForm").action="${basePath}/admin/addNewsImg.html";
document.getElementById("addForm").submit();
}
}
}

</script>


<form action="" id="addForm" method="post" enctype="multipart/form-data">
<center>
<table>
<tr>
<td><span style="color: red">*</span>图片:</td>
<td>
<input type="file" name="img" id="img" size="40"/>
</td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="button" name="OK" onclick="addNewsImg();" value="添加"/>
<input type="reset" name="CANNEL" value="重置"/>
</center>
</td>
</tr>
</table>
</center>
</form>


java代码:

//添加新闻图片
@RequestMapping("/admin/addNewsImg.html")
public String addNewsImages(HttpServletRequest request,HttpServletResponse response,ModelMap model) throws IOException{
String uploadImageFilePath = request.getSession().getServletContext().getRealPath("/")+"upload/newsImg/";

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile img = (CommonsMultipartFile) multipartRequest.getFile("img");

byte[] filebyte = img.getBytes();
if (filebyte.length > 0) {
File dirPath = new File(uploadImageFilePath);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
Calendar CD = Calendar.getInstance();
String lastname = img.getOriginalFilename().substring(img.getOriginalFilename().lastIndexOf('.'));
String filename = System.currentTimeMillis() + CD.get(Calendar.MILLISECOND) + "1" + lastname;
File uploadedFile = new File(uploadImageFilePath + filename);
FileCopyUtils.copy(filebyte, uploadedFile);

NewsImgEntity entity = new NewsImgEntity();
entity.setImg("/upload/newsImg/" + filename);
entity.setCreateTime(new Date());
entity.setUpdateTime(new Date());
entity.setIsAvailable(1);
String a = imgService.saveNewsImg(entity);
if(a != null && a.trim().length()>0){
model.addAttribute("message", "success");
return "admin/imgMessage";
}else{
model.addAttribute("message", "error");
return "admin/imgMessage";
}
}else{
model.addAttribute("message", "error");
return "admin/imgMessage";
}
}


XML代码:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值