javascript
SpringMVC中的文件上传
1. 配置圖片服務器
一般圖片會單獨保存在圖片服務器上, 本文為簡化處理, 在Tomcat中配置一個路勁用于專門存放圖片
在tomcat上配置圖片虛擬目錄,在tomcat下conf/server.xml中添加:
<Context docBase="E:\temp" path="/pic" reloadable="true"/>
訪問http://localhost:8080/pic即可訪問F:\develop\upload\temp下的圖片。
?
也可以通過eclipse配置:
?
2. 導入jar包
CommonsMultipartResolver解析器依賴commons-fileupload和commons-io,加入如下jar包:
3. 配置解析器
???? <!-- 配置文件上傳 -->
??? <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
?? ??? ?<!-- 設置文件上傳的大小 -->
?? ??? ?<property name="maxUploadSize">
?? ??? ??? ?<value>5242880</value>
?? ??? ?</property>
??? </bean>
4. 圖片上傳
?//獲取提交的修改信息,進行商品修改
?? ?@RequestMapping(value="/updateitem.action",method={RequestMethod.POST, RequestMethod.GET})
?? ?public String updateItem (Items item, MultipartFile picture) throws IllegalStateException, IOException {
?? ??? ?//圖片保存
?? ??? ?//為每個圖片生成一個獨一無二的名稱
?? ??? ?String picName = UUID.randomUUID().toString();
?? ??? ?//獲取圖片的后綴名
?? ??? ?String originalFilename = picture.getOriginalFilename();
?? ??? ?String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
?? ??? ?//拼接圖片名稱
?? ??? ?String pictureName = picName.concat(extName);
?? ??? ?//將圖片保存在制定路徑下
?? ??? ?picture.transferTo(new File("E:\\temp\\" + pictureName));
?? ??? ?//將圖片名稱保存到數據庫中
?? ??? ?item.setPic(pictureName);
?? ??? ?
?? ??? ?//調用業務層修改數據
?? ??? ?itemService.update(item);
?? ??? ?return "forward:/item/itemEdit.action";
?? ?}
???
?頁面:
<!-- 上傳圖片是需要指定屬性 enctype="multipart/form-data" -->
?? ?<!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> -->
?? ?<form id="itemForm"?? ?action="${pageContext.request.contextPath }/item/updateitem.action"
?? ??? ?method="post" enctype="multipart/form-data">
?? ??? ?<input type="hidden" name="id" value="${item.id}" /> 修改商品信息:
?? ??? ?<table width="100%" border=1>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td>商品名稱</td>
?? ??? ??? ??? ?<td><input type="text" name="name" value="${item.name }" /></td>
?? ??? ??? ?</tr>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td>商品價格</td>
?? ??? ??? ??? ?<td><input type="text" name="price" value="${item.price }" /></td>
?? ??? ??? ?</tr>
?? ??? ??? ?
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td>商品生產日期</td>
?? ??? ??? ??? ?<td><input type="text" name="createtime"
?? ??? ??? ??? ??? ?value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
?? ??? ??? ?</tr>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td>商品圖片</td>
?? ??? ??? ??? ?<td>
?? ??? ??? ??? ??? ?<c:if test="${item.pic !=null}">
?? ??? ??? ??? ??? ??? ?<img src="/pic/${item.pic}" width=100 height=100/>
?? ??? ??? ??? ??? ??? ?<br/>
?? ??? ??? ??? ??? ?</c:if>
?? ??? ??? ??? ??? ?<input type="file"? name="picture"/>
?? ??? ??? ??? ?</td>
?? ??? ??? ?</tr>
?? ??? ??? ?
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td>商品簡介</td>
?? ??? ??? ??? ?<td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
?? ??? ??? ??? ?</td>
?? ??? ??? ?</tr>
?? ??? ??? ?<tr>
?? ??? ??? ??? ?<td colspan="2" align="center"><input type="submit" value="提交" />
?? ??? ??? ??? ?</td>
?? ??? ??? ?</tr>
?? ??? ?</table>
?? ?</form>
?
轉載于:https://www.cnblogs.com/rodge-run/p/6545412.html
總結
以上是生活随笔為你收集整理的SpringMVC中的文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Problem A: 编写函数:三个数的
- 下一篇: Python之第一个helloworld