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

阅读更多
后台代码:




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 items = null;
		

		try {

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

			e.printStackTrace();
		}
		Iterator 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;
	}













选择文件:

你可能感兴趣的:(enctype="multipart/form-data" 传递text参数,后台获取不到)