`
wj196
  • 浏览: 297778 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

enctype="multipart/form-data" 传递text参数,后台获取不到

    博客分类:
  • java
 
阅读更多
后台代码:




public String cooperationFileUpload(HttpServletRequest req, User user) {
		
		// SmartUpload su = new SmartUpload();//新建一个SmartUpload对象
	   //String mediaId = su.getRequest().getParameter("fileup_mediaId" );//取单个参数单个值
	   //String cooperationId = su.getRequest().getParameter("fileup_cooperationId" );//取单个参数单个值

		 String mediaId ="";
		 String cooperationId = "";
		 	
		 String fileName="";
		 String newFileName ="";
		 
		 
		 int id = mediaCooperationFileDao.queryId();
		/** 上传文件保存目录 */
		File uploadPath = new File(Constants.COOPERATION_FILE_PATH);
		if (!uploadPath.exists()) {
			uploadPath.mkdirs();
		}
		/** 临时文件目录 */
		File tempPathFile = new File(Constants.EXCEL_PATH + "temp");
		if (!tempPathFile.exists()) {
			tempPathFile.mkdirs();
		}

		/** 创建磁盘文件对象 */
		DiskFileItemFactory factory = new DiskFileItemFactory();

		/** 为对象赋值 */

		/** 设置缓冲区大小 */
		factory.setSizeThreshold(1024 * 1024 * 100);
		/** 设置缓冲区目录 */
		factory.setRepository(tempPathFile);

		/** 创建文件获取对象 */
		ServletFileUpload upload = new ServletFileUpload(factory);

		/** 设置文件对象尺寸 */
		upload.setSizeMax(1024 * 1024 * 100);

		/** 得到所有的文件 */
		List<FileItem> items = null;
		

		try {

			items = upload.parseRequest(req);
		} catch (FileUploadException e) {

			e.printStackTrace();
		}
		Iterator<FileItem> i = items.iterator();
		while (i.hasNext()) {
			try {
				FileItem item = (FileItem) i.next();

				// 检查当前项目是普通表单项目还是上传文件。
				String fieldName = item.getFieldName();
				 if(item.isFormField()){
					if (fieldName.equals("fileup_mediaId")) {
						mediaId = item.getString();// 显示表单内容。
					} else if (fieldName.equals("fileup_cooperationId")) {
						cooperationId = item.getString();
					} 
				 }else if (fieldName.equals("fileName")) {
						fileName = item.getName();
						if (fileName != null) {
							newFileName = fileName.substring(fileName
									.lastIndexOf("\\") + 1, fileName.length());

							newFileName= getSaveName(newFileName, id);
							File savedFile = new File(uploadPath, newFileName);
							System.out.println(savedFile.getAbsolutePath());
							item.write(savedFile);

						}
					

				}

			} catch (Exception e) {

				e.printStackTrace();
			}
		}
		MediaCooperationFile mcf = new MediaCooperationFile();
		mcf.setId(id);
		mcf.setFileName(newFileName);
		mcf.setFilePath(uploadPath + "\\" + newFileName);
		mcf.setCooperationId(Integer.parseInt(cooperationId));
		mcf.setMediaId(Integer.parseInt(mediaId));
		mcf.setCreator(user.getUserName());
		mediaCooperationFileDao.insert(mcf);
		return mediaId;
	}













<form name="form_media_cooperation_fileup" id="dddd" action="/aMsite/media/cooperationFileUpload.jhtml" method="post"
					enctype="multipart/form-data">

		<input type="hidden"	name="fileup_cooperationId"  id="fileup_cooperationId"/>
		<input type="hidden"    name="fileup_mediaId"	id="fileup_mediaId"   value="${media.id}" />
	
	<table>
		<tr>
			<td>
				选择文件:
			</td>
			<td>
				<input type="file" name="fileName" id="fileName" />
			</td>

			<td>
				<input type="submit" value="上传" />
			</td>
			</tr>
		</table>
	</form>

分享到:
评论
2 楼 wj196 2012-12-04  
brosnan2800 写道
解决了没有,求解。

解决了啊,上面就是解决后的代码
1 楼 brosnan2800 2012-11-19  
解决了没有,求解。

相关推荐

Global site tag (gtag.js) - Google Analytics