form表单中文件域和非文件域的上传

前端代码

	<form action="<%=contextPath%>/foodServlet?method=addFood" method="post" enctype="multipart/form-data">
		<!-- 本段标题(分段标题) -->
		<div class="ItemBlock_Title">
        	<img width="4" height="7" border="0" src="<%=basePath %>/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>
                            <select id="cid" name="foodType_id" style="width:80px">
	                           
			   					
                              </select>
                             *</td>
						</tr>
						<tr>
							<td width="80px">菜名</td>
							<td><input type="text" name="foodName" class="InputStyle" value=""/> *</td>
						</tr>
						<tr>
							<td>价格</td>
							<td><input type="text" name="price" class="InputStyle" value=""/> *</td>
						</tr>
                        <tr>
							<td>会员价格</td>
							<td><input type="text" name="mprice" class="InputStyle" value=""/> *</td>
						</tr>
						
						<tr>
							<td>简介</td>
							<td><textarea name="introduce" class="TextareaStyle"></textarea></td>
						</tr>
						<tr>
							<td width="80px">菜品图片</td>
							<td>
								
								<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>

控制层代码

        
                Integer id=null;
		Integer foodType_id=null;
		String foodName=null;
		Double price=null;
		Double mprice=null;
		String remark=null;
		String img=null;
		try {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
			if (servletFileUpload.isMultipartContent(request)) {
				List<FileItem> list = servletFileUpload.parseRequest(request);
				//System.out.println(list.get(0).getString("UTF-8"));
				for (FileItem fileItem : list) {
					
					if (fileItem.isFormField()) {
						String fieldName = fileItem.getFieldName();
						String value = fileItem.getString("UTF-8");
						//System.out.println(fieldName+"-----"+value);
						if ("foodType_id".equals(fieldName)) {
							foodType_id=Integer.parseInt(value);
							
						}else if ("foodName".equals(fieldName)) {
							foodName=value;
						}else if ("price".equals(fieldName)) {
							price=Double.parseDouble(value);
						}else if ("mprice".equals(fieldName)) {
							mprice=Double.parseDouble(value);
						}else if ("introduce".equals(fieldName)) {
							remark=value;
						}else if ("id".equals(fieldName)) {
							id=Integer.parseInt(value);
						}
					}else {
						String fieldName = fileItem.getFieldName();
						String name = fileItem.getName();
						name=UUID.randomUUID().toString().replaceAll("-", "")+"_"+name;
						String realPath = request.getServletContext().getRealPath("/upload");
						File file = new File(realPath);
						if (!file.exists()) {
							file.mkdir();
						}
						
						
						img= "D:\\img/"+name;
						
						fileItem.write(new File(file, name));
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		Food food=new Food(id, foodName, foodType_id, null, price, mprice, remark, img);
		foodService.update(food);
		getFoodList(request, response);


System.out.println(fieldName+"-----"+value);

打印的是

foodType_id-----3

foodName-----臭豆腐
price-----122
mprice-----100
introduce-----ezsdfsdf
upload/c669081362164a43a2f128a333db9ef7_baiqieji.jpg

猜你喜欢

转载自blog.csdn.net/amor_fatii/article/details/81061504