赞
踩
在解决文件上传附带参数的时候,发现当form中添加enctype:"multipart/form-data",后台用request.getParameter(“XXX”)获取不到数据
原因分析:
1.get方式
get方式提交的话,表单项都保存在http header中,格式是
http://localhost:8080/hello.do?name1=value1&name2=value2这样的字符串。server端通过request.getParameter是可以取到值的。
2.post方式(enctype为缺省的application/x-www-form-urlencoded)
表单数据都保存在http的正文部分,格式类似于下面这样:用request.getParameter是可以取到数据的
name1=value1&name2=value2
3.post方式(enctype为multipart/form-data,多用于文件上传,对于只想传value的做法,显然使用application/json或者text/plain会好很多。可以把数据放到form的header或者body中,在后台使用相应的方法得到具体值)
表单数据都保存在http的正文部分,各个表单项之间用boundary隔开。格式类似于下面这样:用request.getParameter()是取不到数据的,这时需要通过request.getInputStream来取数据,不过取到的是个InputStream,所以无法直接获取指定的表单项(需要自己对取到的流进行解析,才能得到表单项以及上传的文件内容等信息)。这种需求属于比较共通的功能,所以有很多开源的组件可以直接利用。比如:apache的fileupload组件,smartupload等。通过这些开源的upload组件提供的API,就可以直接从request中取得指定的表单项了。
1、multipart/form-data的基础方法是post,也就是说是由post方法来组合实现的
2、multipart/form-data与post方法的不同之处:请求头,请求体。
3、multipart/form-data的请求头必须包含一个特殊的头信息:Content-Type,且其值也必须规定为multipart/form-data,同时还需要规定一个内容分割符用于分割请求体中的多个post的内容,如文件内容和文本内容自然需要分割开来,不然接收方就无法正常解析和还原这个文件了。具体的头信息如下:
Content-Type: multipart/form-data; boundary=${bound}
方案一:JS修改上传路径
- <form id="upload" name="upload" action="fileftp.jsp" method="post" ENCTYPE="multipart/form-data">
- <input type="hidden" name="otherName" id="otherName" value="abcdefg"/>
- <td nowrap>
- <input type="file" id="file1" name="file1" value="" size="40" class="sbttn"/>
- <input type="submit" value="上传" class="sbttn"/>
- </td>
- </form>
当表单提交到fileftp.jsp后,是无法使用request.getParameter("otherName")获得到abcdefg的值的。但是可以获得到file1的文件。
这里使用JS在表单提交的时候将参数拼接在表单action地址的后面。代码像这样的:
- <script language="javascript">
- function formSubmit(){
- var action="fileftp.jsp";
- action+="?otherName="+document.upload.otherName.value;
- document.upload.action=action;
- document.upload.submit();
- }
- </script>
第二个示例
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- 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>上传文件开通业务</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>
- <script type="text/javascript">
-
- function pidImport() {
- var url = "/lobby/view/product/doUpload.jsp?";
- var inputList = document.getElementsByTagName("input");
- var pid = document.upload.pid.value;
- url += "pid=" + pid;
- document.all.upload.action = url;
- document.all.upload.submit();
- }
- </script>
- <body>
- <form name="upload" action="/lobby/view/product/doUpload.jsp" method="post" enctype="multipart/form-data">
- 产品id: <input type="text" name="pid" size="10"/><br/>
- 选择文件:<input type="file" name="upfile" size="50"/><br/>
- <input type="button" value="提交" οnclick="pidImport();">
- </form>
- </body>
- </html>
-
- <html>
- <head> <title>Servlet 上传文件</title></head>
- <body >
- <form name="uploadForm" method="POST"
- enctype="MULTIPART/FORM-DATA"
- action="upload">
-
- User Name:<input type="text" name="username" size="30"/>
- Upload File1:<input type="file" name="file1" size="30"/>
- Upload File2:<input type="file" name="file2" size="30"/>
- <input type="submit" name="submit" value="上传">
- <input type="reset" name="reset" value="重置">
- </form>
- </body>
- </html>
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- import org.apache.commons.fileupload.*;
- import org.apache.commons.fileupload.servlet.*;
- import org.apache.commons.fileupload.disk.*;
-
- // Servlet 文件上传
- public class UploadServlet extends HttpServlet
- {
- private String filePath; // 文件存放目录
- private String tempPath; // 临时文件目录
-
- // 初始化
- public void init(ServletConfig config) throws ServletException
- {
- super.init(config);
- // 从配置文件中获得初始化参数
- filePath = config.getInitParameter("filepath");
- tempPath = config.getInitParameter("temppath");
-
- ServletContext context = getServletContext();
-
- filePath = context.getRealPath(filePath);
- tempPath = context.getRealPath(tempPath);
- System.out.println("文件存放目录、临时文件目录准备完毕 ...");
- }
-
- // doPost
- public void doPost(HttpServletRequest req, HttpServletResponse res)
- throws IOException, ServletException
- {
- res.setContentType("text/plain;charset=gbk");
- PrintWriter pw = res.getWriter();
- try{
- DiskFileItemFactory diskFactory = new DiskFileItemFactory();
- // threshold 极限、临界值,即硬盘缓存 1M
- diskFactory.setSizeThreshold(4 * 1024);
- // repository 贮藏室,即临时文件目录
- diskFactory.setRepository(new File(tempPath));
-
- ServletFileUpload upload = new ServletFileUpload(diskFactory);
- // 设置允许上传的最大文件大小 4M
- upload.setSizeMax(4 * 1024 * 1024);
- // 解析HTTP请求消息头
- List fileItems = upload.parseRequest(req);
- Iterator iter = fileItems.iterator();
- while(iter.hasNext())
- {
- FileItem item = (FileItem)iter.next();
- if(item.isFormField())
- {
- System.out.println("处理表单内容 ...");
- processFormField(item, pw);
- }else{
- System.out.println("处理上传的文件 ...");
- processUploadFile(item, pw);
- }
- }// end while()
-
- pw.close();
- }catch(Exception e){
- System.out.println("使用 fileupload 包时发生异常 ...");
- e.printStackTrace();
- }// end try ... catch ...
- }// end doPost()
-
- // 处理表单内容
- private void processFormField(FileItem item, PrintWriter pw)
- throws Exception
- {
- String name = item.getFieldName();
- String value = item.getString();
- pw.println(name + " : " + value + "\r\n");
- }
-
- // 处理上传的文件
- private void processUploadFile(FileItem item, PrintWriter pw)
- throws Exception
- {
- // 此时的文件名包含了完整的路径,得注意加工一下
- String filename = item.getName();
- System.out.println("完整的文件名:" + filename);
- int index = filename.lastIndexOf("\\");
- filename = filename.substring(index + 1, filename.length());
-
- long fileSize = item.getSize();
-
- if("".equals(filename) && fileSize == 0)
- {
- System.out.println("文件名为空 ...");
- return;
- }
- File uploadFile = new File(filePath + "/" + filename);
- item.write(uploadFile);
- pw.println(filename + " 文件保存完毕 ...");
- pw.println("文件大小为 :" + fileSize + "\r\n");
- }
- // doGet
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws IOException, ServletException
- {
- doPost(req, res);
- }
- }
web.xml 文件
- <?xml version="1.0" encoding="gb2312"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
- version="2.4">
- <servlet>
- <servlet-name>UploadServlet</servlet-name>
- <servlet-class>UploadServlet</servlet-class>
-
- <init-param>
- <param-name>filepath</param-name>
- <param-value>uploadFile</param-value>
- </init-param>
- <init-param>
- <param-name>temppath</param-name>
- <param-value>temp</param-value>
- </init-param>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>UploadServlet</servlet-name>
- <url-pattern>/upload</url-pattern>
- </servlet-mapping>
-
- </web-app>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。