我用commons.fileupload上传文件文件是上传成功了但是我要把文本框里的数据和上传路径入库request中拿不到表单里的值
我在网上查了以后得知用getString()方法可以拿到但是只能拿到一个文本框里的值我要入库的话得需要2个值这怎么办啊~~
这是页面
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<form action="fileupload" method="post" enctype="multipart/form-data"
name="form1">
文件名:
<input type="text" name="name" >
<br>
类别:
<select id="select" name="select">
<option value="系统安全">
系统安全
</option>
<option value="网络电视">
网络电视
</option>
<option value="影音播放">
影音播放
</option>
<option value="即时聊天">
即时聊天
</option>
<option value="其他常用">
其他常用
</option>
</select>
<br>
地址:
<input type="file" name="file">

<br>
<input type="submit" name="Submit" value="上传">
</form>
</body>
</html>
下边是servlet
package cn.com.servlet;import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;import java.util.Date;
import java.util.Iterator;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;import cn.com.dao.UploadDao;public class Upload extends HttpServlet { /**
 * Constructor of the object.
 */ public Upload() {
super();
} /**
 * Destruction of the servlet. <br>
 */
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
 * The doGet method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request, response); } /**
 * The doPost method of the servlet. <br>
 * 
 * This method is called when a form has its tag value method equals to
 * post.
 * 
 * @param request
 *            the request send by the client to the server
 * @param response
 *            the response send by the server to the client
 * @throws ServletException
 *             if an error occurred
 * @throws IOException
 *             if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
DiskFileUpload dfu = new DiskFileUpload();
String filename;
String fieldname;
String contentType;
String name = null;
String value = null ;
String value1 = null ;
boolean isFormField;
long size; // 设置允许用户上传文件大小,单位:字节
dfu.setSizeMax(104857600);
// 设置最多允许在内存中存储的数据,单位:字节
dfu.setSizeThreshold(10240);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
dfu.setRepositoryPath("c:/temp");

// 开始读取上传信息
try {
String s=null;
String newname=null;
String extname=null;
List filelist = dfu.parseRequest(request); // 依次处理每个上传的文件
Iterator i = filelist.iterator(); while (i.hasNext()) {
FileItem fitem = (FileItem) i.next();
// 获取文件名,对于非文件的返回null
filename = fitem.getName();
// 获取表单中对应的name fieldname = fitem.getFieldName();

// 获取其大小
size = fitem.getSize();
// 获取其类型
contentType = fitem.getContentType();
// 对于表单域中非file类型的返回true,即若为file类型的则返回false
isFormField = fitem.isFormField();
if(fitem.isFormField()){

     value = fitem.getString("name");
     value1 = fitem.getString("select");
}
out.println("asdasd: " + name + "<br>");
out.println("asdasdasd: " + value + "<br>");
out.println("asdasdasd: " + value1 + "<br>");
out.println("filename: " + filename + "<br>");
out.println("fieldname: " + fieldname + "<br>");
out.println("size: " + size + "<br>");
out.println("contentType: " + contentType + "<br>");
out.println("isFormField: " + isFormField + "<br>");
out.println("<hr>"); // 上方打印出所有表单信息,下面只对file类型的进行处理
if (!fitem.isFormField()) {
filename = fitem.getName();
size = fitem.getSize(); // 对于未上传文件的情况则跳过
if (filename == null || filename.equals("") || size == 0) {
continue;
} // 为获取文件的扩展名
int dot = filename.lastIndexOf(".");
// 扩展名有带前面的.号
extname = filename.substring(dot);
// 为避免文件覆盖,使用当前时间产生新文件名
newname = String.valueOf((new Date()).getTime()); // 获取应用程序的物理实际路径,以便保存文件
s = this.getServletContext().getRealPath("\\"); // 建立新文件夹files
File dir = new File(s + "files");
dir.mkdir();
// 将上传的文件保存到所建的文件夹中
File mf = new File(dir, newname + extname);
fitem.write(mf);
UploadDao dao = new UploadDao();
// dao.addupload(name, s + "files" + "\\" + newname + extname, type);
}
}

} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Initialization of the servlet. <br>
 * 
 * @throws ServletException
 *             if an error occure
 */
public void init() throws ServletException {
// Put your code here
}}
我想一下得到上边页面name和下拉框里的值
然后存入数据库

解决方案 »

  1.   

    以前好像研究过,request里好像有一个MAP,这个MAP就存了好多东西,你表单里所有的东西,你可以断点看看有什么东西.我现在忘了.
      

  2.   

    servlet里不是有request嘛  直接拿吧
      

  3.   

    //我新写的,你看看吧package com.sunguoan.drp.basedata.web;import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.*; 
    import java.util.*; 
    import java.util.regex.*;
    import java.io.*;
    import org.apache.commons.fileupload.servlet.*;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;import com.sunguoan.drp.basedata.manager.ItemService;
    import com.sunguoan.drp.util.AppException;
    public class ItemUploadServlet extends AbstractItemServlet { // 用于存放临时文件的目录
    private File tempPath = null; 

    //保存上传文件的目录
    private File uploadPath = null;

    @Override
    public void init() throws ServletException {
    //取得临时交换目录
    tempPath = new File(this.getServletContext().getRealPath("temp"));
    //如果目录不存在,创建一个
    if (!tempPath.exists()) {
    tempPath.mkdir();
    }
    //取得上传路径
    uploadPath = new File(this.getServletContext().getRealPath("upload"));
    //如果目录不存在,创建一个
    if (!uploadPath.exists()) {
    uploadPath.mkdir();
    }
    //必须显示调用父类的init方法来初始化ItemService
    super.init();
    } @SuppressWarnings({ "unused", "unchecked" })
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(4096);
    factory.setRepository(tempPath);
    ServletFileUpload upload = new ServletFileUpload(factory);
    //设置上传文件大小
    upload.setSizeMax(1000000);
    try {
    List fileItems = upload.parseRequest(req);
    Iterator iter = fileItems.iterator();
    String itemNo = "";
    while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    if (item.isFormField()) {
    if ("itemNo".equals(item.getFieldName())) {
    itemNo = item.getString();
    }
    }
    // 忽略其他不是文件域的所有表单信息
    if (!item.isFormField()) {
    String fileName = item.getName();
    fileName = fileName.substring(fileName.lastIndexOf("\\"), fileName.length());
    String fullFilePath = uploadPath + fileName;
    //将上传文件写入磁盘
    item.write(new File(fullFilePath));

    //将上传文件的名称保存到数据库中
    //ItemService itemService = (ItemService)BeanFactory.getInstance().getBean(ItemService.class);
    itemService.modifyUploadFileNameField(itemNo, fileName);
    res.sendRedirect(req.getContextPath() + "/servlet/basedata/SearchItemServlet");
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    throw new AppException("文件上传失败!");
    }
    }
    }