`
chenhua_1984
  • 浏览: 1231811 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

spring2.5文件上传 fileUpload

阅读更多

 spring的文件上传

1jar包:commons-fileupload.jar,commons-io.jar

2xml配置文件

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    	<property name="defaultEncoding"><value>UTF-8</value>  </property> 
        <property name="maxUploadSize">
            <value>30000000</value>
        </property>
</bean>

 3jsp页面

 <form name="form1" action="${submitUrl}"  method="post" enctype="multipart/form-data">
	   	<input type="hidden" name="num" id="num" value="1"/>
	   		<input type="hidden" name="application" id="application" value=""/>
		<%@include file="../common/form_message.jsp" %>
	
		<table id="dataApt" class="form-table" cellspacing="1">
			
			
			<tr>
				<td class="label">版本号</td>
				<td>
					<input type="text" name="appversion" id="appversion" value=""/>
					<span class="required-field">*</span>
				</td>
			
				<td class="label">选择文件</td>
				<td><input type="file" name="md5" id="md5" value=""/>
					<span class="required-field">*</span><input type="button" onclick="addNewRow();" value="添加更多"/>
				</td>
			</tr>
			<tr>
				<td class="label">应用名称</td>
				<td colspan="3">
					<select  id="appName" name="appName" onchange="getInnerText();" style="width: 150px">
							<option value="">请选择</option>
							<c:forEach items="${requestScope.applications}" var="result">
							<option value="${result.cuId}" >${result.appName}</option>
							</c:forEach>
							</select>
					<span class="required-field">*</span> 提示:同一个应用程序的不同版本的MD5值不同!
				</td>
			</tr>
			
			<tr>
				<td colspan="4"  id="tempDiv"><div align="center"><font color="red">提示:请将版本号和MD5值填写完整!否则该条记录不生效!</font></div></td>
				
			</tr>
			
			<tr>
				<td colspan="4" class="button">
					<input type="submit" value="提交"><input type="reset" value="取消" onclick="changeEnabled()"><input type="button" name="return" onclick="turnBack();" value="返回">
				</td>
			</tr>
		</table>
	</form>

 4java代码

@RequestMapping(value = "/dialupConfig/save.do")
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response, ModelAndView mv) throws IOException {
		File filePath = new File("war\\" + "uploadDir");
		if (!filePath.exists()) {
			filePath.mkdirs();
		}

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("md5");
		
		String fileName = filePath + File.separator
				+ multipartFile.getOriginalFilename();
		File file = new File(fileName);

		try {
			multipartFile.transferTo(file);

		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		mv.setViewName("redirect:/dialupConfig/list.do");
		return mv;
	}

 

 
  • 大小: 14.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics