因为上传文件时需要进行编码,这里就是指定他的编码方式。
如果用了这个ENCTYPE="multipart/form-data"你取得参数的方法就需要改变了
如果用了这个ENCTYPE="multipart/form-data"你取得参数的方法就需要改变了
解决方案 »
- SSH下用ajax实现一个功能-求教
- 数据库连接池中数据库连接负载不均的问题
- 【求教】关于jsp模糊查询分页 异常的问题
- 奇怪的servlet路径配置
- 请教一下问卷调查最后给出反馈是怎么处理的?
- 请问如何动态赋值到<%=strI%>price?
- 在jsp页面中怎么才能实现控制页面的大小?
- 我的FCKeditor编辑器怎么上传不了MP3格式的文件,版本是2.6的,按照网上那些方法配置好像也没行,不知道是不是我配置错
- 出现了cannot resolve symbol(symbol : method split (java.lang.String))错误
- 如何判断某个日期是不是合法日期?还有怎么知道某年某月某日是星期几?
- 请教一个关于session的概念问题??
- 在JSP中实现文件上传的问题。
如果你要上传文件,就不要在这个页面中传递其它信息,
ENCTYPE="multipart/form-data" 是RFC1867中的规定,
如果要实现页面的上传,就要如此定义,但是,有了它,就不能够传递其它参数,
不知你的application server用的是什么 ?是IIS吧。
* Description: A Thread safe class for parsing a Servlet's InputStream. It is especially usefull when the HTML form that
* the servlet is processing is of multipart/form-data
* Copyright: Copyright (c) Dane S. Foster<p>
* @author Dane S. Foster <mailto:[email protected]>
* @version 1.1
*/
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class MultipartFormReader
{
private final int READY = 0;
private final int FILENAME = 1;
private final int NAME = 2;
private final int BINDATA = 3;
private final int TXTDATA = 4; /**
* Parses the ServletInputStream of encoding multipart/form-data and separates it into name value pairs. The name-value pairs
* are stored in the <code>map</code> argument. There are a couple of this to be aware of. This class is not a replacement for
* the ServletRequest class but augments it. It should only be used in the case where the client is issuing an HTTP POST request
* and the form-encoding is multipart/form-data.
*
* <p>In the event the HTTP POST request contains binary data --for example, when someone
* wants to upload data to your servlet-- it is stored as a byte array. You can retrieve the binary data by calling the get method
* on the <code>map</code> object you passed in as a parameter.
* <p>i.e.: byte[] uploadedImage = (byte[]) map.get( "fieldname" );<p>
* There is no limit to the amount of binary data that can be uploaded and retrieved.
*
* <p>For those situations where the HTTP POST request contains list data (i.e. checkboxes, multiple selection lists), the list data
* is stored in a <code>java.util.List</code> object. This is equivalent to the <code>javax.servlet.ServletRequest</code>'s
* <code>public String[] getParameterValues( java.lang.String )</code> method.
*<p>i.e.: java.util.List checkboxItems = (java.util.List)map.get( "fieldname" );
*
* @param request A <code>ServletRequest</code> object.
* @param map The <code>Map</code> will be populated with the name value pairs of the HTML form's content.
* It works pretty much like the <code>HttpServletRequest</code> class except it can handle multipart/form-data.
*/
public void read( ServletRequest request, Map map ) throws IOException
{
byte[] bytes = new byte[ 2048 ];
int state = 0;
int eof = 0; /* Get the separator for this request's form data. It is necessary for parsing this request's form data. */
String boundary = request.getContentType();
int pos = boundary.indexOf( "=" );
boundary = boundary.substring( pos + 1 );
boundary = "--" + boundary; String fieldName = null;
String fieldValue = null; ServletInputStream sStream = request.getInputStream(); ByteArrayOutputStream dataBuffer = new ByteArrayOutputStream(); eof = sStream.readLine( bytes, 0, 2048 ); map.clear();
state = 0; while( -1 != eof )
{
String filter = new String( bytes, 0, eof ); if( filter.startsWith( boundary ) )
{
state = READY; if( null != fieldName )
{
if( null != fieldValue )
{
Object o = map.get( fieldName );
Object val = fieldValue.substring( 0, fieldValue.length() - 2 );
if( null == o )
map.put( fieldName, val );
else
{
if( o instanceof List )
((List)o).add( val );
else
{
List list = new ArrayList();
list.add( o );
list.add( val );
map.put( fieldName, list );
}
}
}
else if( dataBuffer.size() > 2 )
{
map.put( fieldName, dataBuffer.toByteArray() );
} fieldName = null;
fieldValue = null;
dataBuffer.reset();
}
}
else if( filter.startsWith( "Content-Disposition: form-data" ) && state == READY )
{
StringTokenizer tokenizer = new StringTokenizer( filter, ";=\"" ); while( tokenizer.hasMoreTokens() )
{
String token = tokenizer.nextToken().trim(); if( token.startsWith( "name" ) )
{
fieldName = tokenizer.nextToken().trim();
state = NAME;
}
else if( token.startsWith( "filename" ) )
{
state = FILENAME;
break;
}
}
}
else if( filter.equals( "\r\n" ) && state == FILENAME ) state = BINDATA;
else if( filter.equals("\r\n" ) && state == NAME ) state = TXTDATA;
else if( state == TXTDATA ) fieldValue = fieldValue == null ? filter : fieldValue + filter;
else if( state == BINDATA ) dataBuffer.write( bytes, 0, eof ); eof = sStream.readLine( bytes, 0, 2048 );
}// Parsing stops here. The Map should now contain all of the form's data.
sStream.close();
} /**
* A utility method that saves you the trouble of having to create a Map object and passing it to the other read method.
*
* @param request The ServletRequest object
*
* @return A java.util.HashMap containing the name-value pairs of the HTTP POST's form data
*/
public HashMap read( ServletRequest request ) throws IOException
{
HashMap hash = new HashMap();
this.read( request, hash );
return hash;
}
}