spring2.5文件上传 fileUpload

12 篇文章 0 订阅
11 篇文章 0 订阅

 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" οnclick="addNewRow();" value="添加更多"/>
				</td>
			</tr>
			<tr>
				<td class="label">应用名称</td>
				<td colspan="3">
					<select  id="appName" name="appName" οnchange="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="取消" οnclick="changeEnabled()"><input type="button" name="return" οnclick="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;
	}

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值