1.需要在mvc-servlet.xml中配置 文件上传的功能
<!-- 启用文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制上传文件大小 5M -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
2.加入文件上传的架包
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- 包含公共的JSP代码片段 -->
<title>无线点餐平台</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="style/js/jquery.js"></script>
<script type="text/javascript" src="style/js/page_common.js"></script>
<link href="style/css/common_style_blue.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="style/css/index_1.css" />
</head>
<body>
<!-- 页面标题 -->
<div id="TitleArea">
<div id="TitleArea_Head"></div>
<div id="TitleArea_Title">
<div id="TitleArea_Title_Content">
<img border="0" width="13" height="13" src="style/images/title_arrow.gif"/> 更新新菜品
</div>
</div>
<div id="TitleArea_End"></div>
</div>
<!-- 文件上传
表单必须是post
设置编码格式 enctype="multipart/form-data(支持上传)
文件选择框 input type="file"
-->
<!-- 主内容区域(数据列表或表单显示) -->
<div id="MainArea">
<!-- 表单内容 -->
<form action="${pageContext.request.contextPath}/food/${param.foodid}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="put">
<!-- 本段标题(分段标题) -->
<div class="ItemBlock_Title">
<img width="4" height="7" border="0" src="style/images/item_point.gif"> 菜品信息
</div>
<!-- 本段表单字段 -->
<div class="ItemBlockBorder">
<div class="ItemBlock">
<div class="ItemBlock2">
<table cellpadding="0" cellspacing="0" class="mainForm">
<tr>
<td width="80px">菜名</td>
<td> <input type="text" name="foodname" class="InputStyle" value="${param.foodname}">*</td>
</tr>
<tr>
<td>价格</td>
<td><input type="text" name="price" class="InputStyle" value="${param.price}"> *</td>
</tr>
<tr>
<td width="80px">菜品图片</td>
<td>
<img style='max-width:68px;width:68px;width:expression(width>68?"68px":width "px");max-width: 68px;'
src="${pageContext.request.contextPath}/images/${param.imagepath}">
<input type="hidden" name="image" value="baizhuoxia.jpg">
<!-- 文件上传框 -->
<input type="file" name="imageUrl"/> *
</td>
</tr>
</table>
</div>
</div>
</div>
<!-- 表单操作 -->
<div id="InputDetailBar">
<input type="submit" value="修改" class="FunctionButtonInput">
<a href="javascript:history.go(-1);" class="FunctionButton">返回</a>
</div>
</form>
</div>
</body>
</html>
//文件上传
@RequestMapping(value="/food/{foodid}",method=RequestMethod.POST)
public String updateFood(@PathVariable String foodid,String foodname,String price,MultipartFile imageUrl,Model model) throws Exception{
//获取文件名
String filename=imageUrl.getOriginalFilename();
//获取存储文件的路径
String abs="D:\\file";
//上传
imageUrl.transferTo(new File(abs+"\\"+filename));
service.updateFoodtype(foodid, foodname, price, filename);
return queryFood(null,1, model);
}
//文件下载
@RequestMapping(value="/download",method=RequestMethod.GET)
public ResponseEntity<byte[]> download( String imagePath) throws Exception{
String absPath="D:\\file";
//获取文件名
String filename=imagePath;
//需要下载的目标文件
File file= new File(absPath);
//设置响应头
HttpHeaders hh= new HttpHeaders();
//设置下载的文件的名称
hh.setContentDispositionFormData("attachment", URLEncoder.encode(filename,"UTF-8"));
//读取目标文件为二进制数组
byte[] fileByte=FileCopyUtils.copyToByteArray(file);
//构建ResponseEntity对象
ResponseEntity<byte[]> re=new ResponseEntity<byte[]>(fileByte,hh,HttpStatus.CREATED);
return re;
}