---
title: "enctype=\"multipart/form-data\"  传递text参数，后台获取不到"
author: "wj196"
date: "2012-08-10T16:57:27Z"
tags: []
categories: ["Web前端"]
slug: "enctype-multipart-form-data-text"
draft: false
lastmod: "2012-08-10T16:57:27Z"
id: 1628465
url: "https://wj196.iteye.com/blog/1628465"
---

后台代码： \[code="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 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; } \[/code] \[code="java"]

选择文件:

\[/code]